Debian Codegraph @

gopls/internal/mcp/mcp_test.go

raw symbols SCIP
1 // Copyright 2025 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package mcp_test
6
7 import (
8 "context"
9 "errors"
10 "net/http"
11 "testing"
12 "time"
13
14 "github.com/modelcontextprotocol/go-sdk/mcp"
15 "golang.org/x/tools/gopls/internal/cache"
16 internalmcp "golang.org/x/tools/gopls/internal/mcp"
17 "golang.org/x/tools/gopls/internal/protocol"
18 )
19
20 type emptySessions struct {
21 }
22
23 // FirstSession implements mcp.Sessions.
24 func (e emptySessions) FirstSession() (*cache.Session, protocol.Server) {
25 return nil, nil
26 }
27
28 // Session implements mcp.Sessions.
29 func (e emptySessions) Session(string) (*cache.Session, protocol.Server) {
30 return nil, nil
31 }
32
33 // SetSessionExitFunc implements mcp.Sessions.
34 func (e emptySessions) SetSessionExitFunc(func(string)) {
35 }
36
37 func TestContextCancellation(t *testing.T) {
38 ctx, cancel := context.WithCancel(context.Background())
39
40 res := make(chan error)
41 go func() {
42 res <- internalmcp.Serve(ctx, "localhost:0", emptySessions{}, true, nil)
43 }()
44
45 time.Sleep(1 * time.Second)
46 cancel()
47
48 select {
49 case err := <-res:
50 if !errors.Is(err, http.ErrServerClosed) {
51 t.Errorf("mcp server unexpected return got %v, want: %v", err, http.ErrServerClosed)
52 }
53 case <-time.After(5 * time.Second):
54 t.Errorf("mcp server did not terminate after 5 seconds of context cancellation")
55 }
56 }
57
58 func TestClientRootChange(t *testing.T) {
59 foundError := make(chan error, 1)
60 foundAll := []chan struct{}{
61 make(chan struct{}), // after initialized
62 make(chan struct{}), // after roots/list_changed
63 }
64
65 wantRoots := []map[string]struct{}{
66 { // after initialized
67 "file:///path/to/one": struct{}{},
68 "file:///path/to/two": struct{}{},
69 },
70 { // after roots/list_changed
71 "file:///path/to/one": struct{}{},
72 "file:///path/to/two": struct{}{},
73 "file:///path/to/three": struct{}{},
74 "file:///path/to/four": struct{}{},
75 },
76 }
77
78 var callCount int
79
80 server := internalmcp.NewServer(nil, nil, func(res *mcp.ListRootsResult, err error) {
81 if err != nil {
82 foundError <- err
83 return
84 }
85
86 if callCount >= len(wantRoots) {
87 t.Errorf("Handler called more times than expected: %d", callCount)
88 return
89 }
90
91 expected := wantRoots[callCount]
92
93 if len(res.Roots) != len(expected) {
94 t.Errorf("Phase %d: expected %d roots, got %d", callCount+1, len(expected), len(res.Roots))
95 return
96 }
97
98 for _, r := range res.Roots {
99 if _, ok := expected[r.URI]; !ok {
100 t.Errorf("Phase %d: unexpected root %s", callCount+1, r)
101 }
102 }
103
104 callCount++
105 close(foundAll[callCount-1]) // Signal that this phase is complete
106 })
107
108 client := mcp.NewClient(&mcp.Implementation{Name: "test-client"}, nil)
109 client.AddRoots(&mcp.Root{
110 Name: "one",
111 URI: "file:///path/to/one",
112 }, &mcp.Root{
113 Name: "two",
114 URI: "file:///path/to/two",
115 })
116
117 clientTransport, serverTransport := mcp.NewInMemoryTransports()
118
119 ctx := t.Context()
120
121 // Connect server and client
122 serverSession, _ := server.Connect(ctx, serverTransport, nil)
123 defer serverSession.Close()
124
125 clientSession, _ := client.Connect(ctx, clientTransport, nil)
126 defer clientSession.Close()
127
128 // Phase 1: Wait for the initial handshake and first root fetch
129 select {
130 case <-foundAll[0]:
131 t.Log("Phase 1: Initial roots received successfully.")
132 case err := <-foundError:
133 t.Fatalf("Server handler error during initialization: %v", err)
134 case <-time.After(20 * time.Second):
135 t.Fatal("Timeout waiting for initial roots.")
136 }
137
138 // Trigger the root change
139 client.AddRoots(&mcp.Root{
140 Name: "three",
141 URI: "file:///path/to/three",
142 }, &mcp.Root{
143 Name: "four",
144 URI: "file:///path/to/four",
145 })
146
147 // Phase 2: Wait for the server to catch the notification and fetch the updated roots
148 select {
149 case <-foundAll[1]:
150 t.Log("Phase 2: Updated roots received successfully.")
151 case err := <-foundError:
152 t.Fatalf("Server handler error after root change: %v", err)
153 case <-time.After(20 * time.Second):
154 t.Fatal("Timeout waiting for updated roots.")
155 }
156 }