|
| 1 | +// Copyright 2025 The Go MCP SDK Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by an MIT-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +//go:build go1.25 |
| 6 | + |
| 7 | +package mcp |
| 8 | + |
| 9 | +import ( |
| 10 | + "context" |
| 11 | + "sync/atomic" |
| 12 | + "testing" |
| 13 | + "testing/synctest" |
| 14 | + |
| 15 | + "github.com/google/jsonschema-go/jsonschema" |
| 16 | +) |
| 17 | + |
| 18 | +// TestServerListChangedNotifications verifies that listChanged notifications |
| 19 | +// are correctly sent or suppressed based on capability configuration. |
| 20 | +func TestServerListChangedNotifications(t *testing.T) { |
| 21 | + tool := &Tool{Name: "test-tool", InputSchema: &jsonschema.Schema{Type: "object"}} |
| 22 | + |
| 23 | + testCases := []struct { |
| 24 | + name string |
| 25 | + serverOpts *ServerOptions |
| 26 | + wantNotifyCount int64 |
| 27 | + }{ |
| 28 | + { |
| 29 | + name: "Default: notification sent", |
| 30 | + serverOpts: nil, |
| 31 | + wantNotifyCount: 1, |
| 32 | + }, |
| 33 | + { |
| 34 | + name: "ListChanged false: notification suppressed", |
| 35 | + serverOpts: &ServerOptions{ |
| 36 | + Capabilities: &ServerCapabilities{ |
| 37 | + Tools: &ToolCapabilities{ListChanged: false}, |
| 38 | + }, |
| 39 | + }, |
| 40 | + wantNotifyCount: 0, |
| 41 | + }, |
| 42 | + { |
| 43 | + name: "ListChanged true: notification sent", |
| 44 | + serverOpts: &ServerOptions{ |
| 45 | + Capabilities: &ServerCapabilities{ |
| 46 | + Tools: &ToolCapabilities{ListChanged: true}, |
| 47 | + }, |
| 48 | + }, |
| 49 | + wantNotifyCount: 1, |
| 50 | + }, |
| 51 | + } |
| 52 | + |
| 53 | + for _, tc := range testCases { |
| 54 | + t.Run(tc.name, func(t *testing.T) { |
| 55 | + synctest.Test(t, func(t *testing.T) { |
| 56 | + ctx := context.Background() |
| 57 | + |
| 58 | + // Create server. |
| 59 | + impl := &Implementation{Name: "testServer", Version: "v1.0.0"} |
| 60 | + server := NewServer(impl, tc.serverOpts) |
| 61 | + |
| 62 | + // Track notifications. |
| 63 | + var notifyCount atomic.Int64 |
| 64 | + |
| 65 | + // Connect client and server. |
| 66 | + cTransport, sTransport := NewInMemoryTransports() |
| 67 | + ss, err := server.Connect(ctx, sTransport, nil) |
| 68 | + if err != nil { |
| 69 | + t.Fatal(err) |
| 70 | + } |
| 71 | + defer ss.Close() |
| 72 | + |
| 73 | + client := NewClient(&Implementation{Name: "testClient", Version: "v1.0.0"}, &ClientOptions{ |
| 74 | + ToolListChangedHandler: func(ctx context.Context, req *ToolListChangedRequest) { |
| 75 | + notifyCount.Add(1) |
| 76 | + }, |
| 77 | + }) |
| 78 | + cs, err := client.Connect(ctx, cTransport, nil) |
| 79 | + if err != nil { |
| 80 | + t.Fatal(err) |
| 81 | + } |
| 82 | + defer cs.Close() |
| 83 | + |
| 84 | + // Add a tool, which may or may not trigger notification. |
| 85 | + server.AddTool(tool, nil) |
| 86 | + |
| 87 | + // Wait for all goroutines to be blocked (notification delivered). |
| 88 | + synctest.Wait() |
| 89 | + |
| 90 | + if got, want := notifyCount.Load(), tc.wantNotifyCount; got != want { |
| 91 | + t.Errorf("notification count: got %d, want %d", got, want) |
| 92 | + } |
| 93 | + }) |
| 94 | + }) |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +// TestClientListChangedNotifications verifies that roots listChanged notifications |
| 99 | +// are correctly sent or suppressed based on client capability configuration. |
| 100 | +func TestClientListChangedNotifications(t *testing.T) { |
| 101 | + root := &Root{URI: "file:///test"} |
| 102 | + |
| 103 | + testCases := []struct { |
| 104 | + name string |
| 105 | + clientOpts *ClientOptions |
| 106 | + wantNotifyCount int64 |
| 107 | + }{ |
| 108 | + { |
| 109 | + name: "Default: notification sent", |
| 110 | + clientOpts: nil, |
| 111 | + wantNotifyCount: 1, |
| 112 | + }, |
| 113 | + { |
| 114 | + name: "ListChanged false: notification suppressed", |
| 115 | + clientOpts: &ClientOptions{ |
| 116 | + Capabilities: &ClientCapabilities{ |
| 117 | + RootsV2: &RootCapabilities{ListChanged: false}, |
| 118 | + }, |
| 119 | + }, |
| 120 | + wantNotifyCount: 0, |
| 121 | + }, |
| 122 | + { |
| 123 | + name: "ListChanged true: notification sent", |
| 124 | + clientOpts: &ClientOptions{ |
| 125 | + Capabilities: &ClientCapabilities{ |
| 126 | + RootsV2: &RootCapabilities{ListChanged: true}, |
| 127 | + }, |
| 128 | + }, |
| 129 | + wantNotifyCount: 1, |
| 130 | + }, |
| 131 | + } |
| 132 | + |
| 133 | + for _, tc := range testCases { |
| 134 | + t.Run(tc.name, func(t *testing.T) { |
| 135 | + synctest.Test(t, func(t *testing.T) { |
| 136 | + ctx := context.Background() |
| 137 | + |
| 138 | + // Track notifications. |
| 139 | + var notifyCount atomic.Int64 |
| 140 | + |
| 141 | + // Create server with roots list changed handler. |
| 142 | + server := NewServer(&Implementation{Name: "testServer", Version: "v1.0.0"}, &ServerOptions{ |
| 143 | + RootsListChangedHandler: func(ctx context.Context, req *RootsListChangedRequest) { |
| 144 | + notifyCount.Add(1) |
| 145 | + }, |
| 146 | + }) |
| 147 | + |
| 148 | + // Connect client and server. |
| 149 | + cTransport, sTransport := NewInMemoryTransports() |
| 150 | + ss, err := server.Connect(ctx, sTransport, nil) |
| 151 | + if err != nil { |
| 152 | + t.Fatal(err) |
| 153 | + } |
| 154 | + defer ss.Close() |
| 155 | + |
| 156 | + client := NewClient(&Implementation{Name: "testClient", Version: "v1.0.0"}, tc.clientOpts) |
| 157 | + cs, err := client.Connect(ctx, cTransport, nil) |
| 158 | + if err != nil { |
| 159 | + t.Fatal(err) |
| 160 | + } |
| 161 | + defer cs.Close() |
| 162 | + |
| 163 | + // Add a root, which may or may not trigger notification. |
| 164 | + client.AddRoots(root) |
| 165 | + |
| 166 | + // Wait for all goroutines to be blocked (notification delivered). |
| 167 | + synctest.Wait() |
| 168 | + |
| 169 | + if got, want := notifyCount.Load(), tc.wantNotifyCount; got != want { |
| 170 | + t.Errorf("notification count: got %d, want %d", got, want) |
| 171 | + } |
| 172 | + }) |
| 173 | + }) |
| 174 | + } |
| 175 | +} |
0 commit comments