@@ -109,7 +109,7 @@ func TestSessionWithTools_Integration(t *testing.T) {
109
109
// Check if the session can be cast to SessionWithTools
110
110
swt , ok := s .(SessionWithTools )
111
111
require .True (t , ok , "Session should implement SessionWithTools" )
112
-
112
+
113
113
// Check if the tools are accessible
114
114
tools := swt .GetSessionTools ()
115
115
require .NotNil (t , tools , "Session tools should be available" )
@@ -121,13 +121,13 @@ func TestSessionWithTools_Integration(t *testing.T) {
121
121
tool , exists := tools ["session-tool" ]
122
122
require .True (t , exists , "Session tool should exist in the map" )
123
123
require .NotNil (t , tool , "Session tool should not be nil" )
124
-
124
+
125
125
// Now test calling directly with the handler
126
126
result , err := tool .Handler (sessionCtx , testReq )
127
127
require .NoError (t , err , "No error calling session tool handler directly" )
128
128
require .NotNil (t , result , "Result should not be nil" )
129
129
require .Len (t , result .Content , 1 , "Result should have one content item" )
130
-
130
+
131
131
textContent , ok := result .Content [0 ].(mcp.TextContent )
132
132
require .True (t , ok , "Content should be TextContent" )
133
133
assert .Equal (t , "session-tool result" , textContent .Text , "Result text should match" )
@@ -137,13 +137,13 @@ func TestSessionWithTools_Integration(t *testing.T) {
137
137
func TestMCPServer_ToolsWithSessionTools (t * testing.T ) {
138
138
// Basic test to verify that session-specific tools are returned correctly in a tools list
139
139
server := NewMCPServer ("test-server" , "1.0.0" , WithToolCapabilities (true ))
140
-
140
+
141
141
// Add global tools
142
142
server .AddTools (
143
143
ServerTool {Tool : mcp .NewTool ("global-tool-1" )},
144
144
ServerTool {Tool : mcp .NewTool ("global-tool-2" )},
145
145
)
146
-
146
+
147
147
// Create a session with tools
148
148
session := & fakeSessionWithTools {
149
149
sessionID : "session-1" ,
@@ -154,28 +154,28 @@ func TestMCPServer_ToolsWithSessionTools(t *testing.T) {
154
154
"global-tool-1" : {Tool : mcp .NewTool ("global-tool-1" , mcp .WithDescription ("Overridden" ))},
155
155
},
156
156
}
157
-
157
+
158
158
// Register the session
159
159
err := server .RegisterSession (context .Background (), session )
160
160
require .NoError (t , err )
161
-
161
+
162
162
// List tools with session context
163
163
sessionCtx := server .WithContext (context .Background (), session )
164
164
resp := server .HandleMessage (sessionCtx , []byte (`{
165
165
"jsonrpc": "2.0",
166
166
"id": 1,
167
167
"method": "tools/list"
168
168
}` ))
169
-
169
+
170
170
jsonResp , ok := resp .(mcp.JSONRPCResponse )
171
171
require .True (t , ok , "Response should be a JSONRPCResponse" )
172
-
172
+
173
173
result , ok := jsonResp .Result .(mcp.ListToolsResult )
174
174
require .True (t , ok , "Result should be a ListToolsResult" )
175
-
175
+
176
176
// Should have 3 tools - 2 global tools (one overridden) and 1 session-specific tool
177
177
assert .Len (t , result .Tools , 3 , "Should have 3 tools" )
178
-
178
+
179
179
// Find the overridden tool and verify its description
180
180
var found bool
181
181
for _ , tool := range result .Tools {
@@ -205,7 +205,7 @@ func TestMCPServer_AddSessionTools(t *testing.T) {
205
205
require .NoError (t , err )
206
206
207
207
// Add session-specific tools
208
- err = server .AddSessionTools (session .SessionID (),
208
+ err = server .AddSessionTools (session .SessionID (),
209
209
ServerTool {Tool : mcp .NewTool ("session-tool" )},
210
210
)
211
211
require .NoError (t , err )
@@ -321,57 +321,57 @@ func TestMCPServer_ToolFiltering(t *testing.T) {
321
321
}` ))
322
322
resp , ok := response .(mcp.JSONRPCResponse )
323
323
require .True (t , ok )
324
-
324
+
325
325
result , ok := resp .Result .(mcp.ListToolsResult )
326
326
require .True (t , ok )
327
-
327
+
328
328
// Should only include tools with the "allow-" prefix
329
329
assert .Len (t , result .Tools , 3 )
330
-
330
+
331
331
// Verify all tools start with "allow-"
332
332
for _ , tool := range result .Tools {
333
- assert .True (t , len (tool .Name ) >= 6 && tool .Name [:6 ] == "allow-" ,
333
+ assert .True (t , len (tool .Name ) >= 6 && tool .Name [:6 ] == "allow-" ,
334
334
"Tool should start with 'allow-', got: %s" , tool .Name )
335
335
}
336
336
}
337
337
338
338
func TestMCPServer_SendNotificationToSpecificClient (t * testing.T ) {
339
339
server := NewMCPServer ("test-server" , "1.0.0" )
340
-
340
+
341
341
session1Chan := make (chan mcp.JSONRPCNotification , 10 )
342
342
session1 := & fakeSession {
343
343
sessionID : "session-1" ,
344
344
notificationChannel : session1Chan ,
345
345
initialized : true ,
346
346
}
347
-
347
+
348
348
session2Chan := make (chan mcp.JSONRPCNotification , 10 )
349
349
session2 := & fakeSession {
350
350
sessionID : "session-2" ,
351
351
notificationChannel : session2Chan ,
352
352
initialized : true ,
353
353
}
354
-
354
+
355
355
session3 := & fakeSession {
356
356
sessionID : "session-3" ,
357
357
notificationChannel : make (chan mcp.JSONRPCNotification , 10 ),
358
358
initialized : false , // Not initialized
359
359
}
360
-
360
+
361
361
// Register sessions
362
362
err := server .RegisterSession (context .Background (), session1 )
363
363
require .NoError (t , err )
364
364
err = server .RegisterSession (context .Background (), session2 )
365
365
require .NoError (t , err )
366
366
err = server .RegisterSession (context .Background (), session3 )
367
367
require .NoError (t , err )
368
-
368
+
369
369
// Send notification to session 1
370
370
err = server .SendNotificationToSpecificClient (session1 .SessionID (), "test-method" , map [string ]any {
371
371
"data" : "test-data" ,
372
372
})
373
373
require .NoError (t , err )
374
-
374
+
375
375
// Check that only session 1 received the notification
376
376
select {
377
377
case notification := <- session1Chan :
@@ -380,22 +380,22 @@ func TestMCPServer_SendNotificationToSpecificClient(t *testing.T) {
380
380
case <- time .After (100 * time .Millisecond ):
381
381
t .Error ("Expected notification not received by session 1" )
382
382
}
383
-
383
+
384
384
// Verify session 2 did not receive notification
385
385
select {
386
386
case notification := <- session2Chan :
387
387
t .Errorf ("Unexpected notification received by session 2: %v" , notification )
388
388
case <- time .After (100 * time .Millisecond ):
389
389
// Expected, no notification for session 2
390
390
}
391
-
391
+
392
392
// Test sending to non-existent session
393
393
err = server .SendNotificationToSpecificClient ("non-existent" , "test-method" , nil )
394
394
assert .Error (t , err )
395
395
assert .Contains (t , err .Error (), "not found" )
396
-
396
+
397
397
// Test sending to uninitialized session
398
398
err = server .SendNotificationToSpecificClient (session3 .SessionID (), "test-method" , nil )
399
399
assert .Error (t , err )
400
400
assert .Contains (t , err .Error (), "not properly initialized" )
401
- }
401
+ }
0 commit comments