11package server
22
33import (
4+ "bytes"
45 "context"
56 "encoding/base64"
67 "encoding/json"
@@ -12,6 +13,7 @@ import (
1213 "time"
1314
1415 "github.com/mark3labs/mcp-go/mcp"
16+ "github.com/mark3labs/mcp-go/testutil"
1517 "github.com/stretchr/testify/assert"
1618 "github.com/stretchr/testify/require"
1719)
@@ -342,11 +344,50 @@ func TestMCPServer_Tools(t *testing.T) {
342344 assert .Equal (t , "test-tool-2" , tools [1 ].Name )
343345 },
344346 },
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+
345382 }
346383 for _ , tt := range tests {
347384 t .Run (tt .name , func (t * testing.T ) {
348385 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 ))
350391 _ = server .HandleMessage (ctx , []byte (`{
351392 "jsonrpc": "2.0",
352393 "id": 1,
@@ -373,6 +414,11 @@ func TestMCPServer_Tools(t *testing.T) {
373414 "method": "tools/list"
374415 }` ))
375416 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+ }
376422 })
377423 }
378424}
0 commit comments