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