1
1
package server
2
2
3
3
import (
4
+ "bytes"
4
5
"context"
5
6
"encoding/base64"
6
7
"encoding/json"
@@ -12,6 +13,7 @@ import (
12
13
"time"
13
14
14
15
"github.com/mark3labs/mcp-go/mcp"
16
+ "github.com/mark3labs/mcp-go/testutil"
15
17
"github.com/stretchr/testify/assert"
16
18
"github.com/stretchr/testify/require"
17
19
)
@@ -342,11 +344,50 @@ func TestMCPServer_Tools(t *testing.T) {
342
344
assert .Equal (t , "test-tool-2" , tools [1 ].Name )
343
345
},
344
346
},
347
+ {
348
+ name : "AddTools overwrites tool with same name" ,
349
+ action : func (t * testing.T , server * MCPServer , notificationChannel chan mcp.JSONRPCNotification ) {
350
+ err := server .RegisterSession (context .TODO (), & fakeSession {
351
+ sessionID : "test" ,
352
+ notificationChannel : notificationChannel ,
353
+ initialized : true ,
354
+ })
355
+ require .NoError (t , err )
356
+ server .AddTool (
357
+ mcp .NewTool ("test-tool-dup" ),
358
+ func (ctx context.Context , request mcp.CallToolRequest ) (* mcp.CallToolResult , error ) {
359
+ return & mcp.CallToolResult {}, nil
360
+ },
361
+ )
362
+ // Add same tool name with different handler or data to test overwrite
363
+ server .AddTool (
364
+ mcp .NewTool ("test-tool-dup" ),
365
+ func (ctx context.Context , request mcp.CallToolRequest ) (* mcp.CallToolResult , error ) {
366
+ return & mcp.CallToolResult {}, nil
367
+ },
368
+ )
369
+ },
370
+ expectedNotifications : 2 , // one per AddTool with active session
371
+ validate : func (t * testing.T , notifications []mcp.JSONRPCNotification , toolsList mcp.JSONRPCMessage ) {
372
+ // Both adds must have triggered notifications
373
+ assert .Equal (t , mcp .MethodNotificationToolsListChanged , notifications [0 ].Method )
374
+ assert .Equal (t , mcp .MethodNotificationToolsListChanged , notifications [1 ].Method )
375
+
376
+ tools := toolsList .(mcp.JSONRPCResponse ).Result .(mcp.ListToolsResult ).Tools
377
+ assert .Len (t , tools , 1 , "Expected only one tool after overwrite" )
378
+ assert .Equal (t , "test-tool-dup" , tools [0 ].Name )
379
+ },
380
+ },
381
+
345
382
}
346
383
for _ , tt := range tests {
347
384
t .Run (tt .name , func (t * testing.T ) {
348
385
ctx := context .Background ()
349
- server := NewMCPServer ("test-server" , "1.0.0" , WithToolCapabilities (true ))
386
+
387
+ var buf bytes.Buffer
388
+ logger := & testutil.TestLogger {Buf : & buf }
389
+
390
+ server := NewMCPServer ("test-server" , "1.0.0" , WithToolCapabilities (true ), WithMCPLogger (logger ))
350
391
_ = server .HandleMessage (ctx , []byte (`{
351
392
"jsonrpc": "2.0",
352
393
"id": 1,
@@ -373,6 +414,11 @@ func TestMCPServer_Tools(t *testing.T) {
373
414
"method": "tools/list"
374
415
}` ))
375
416
tt .validate (t , notifications , toolsList )
417
+
418
+ if tt .name == "AddTools overwrites tool with same name" {
419
+ logOutput := buf .String ()
420
+ assert .Contains (t , logOutput , "already exists" )
421
+ }
376
422
})
377
423
}
378
424
}
0 commit comments