@@ -3,10 +3,10 @@ package server
33import  (
44	"context" 
55	"encoding/json" 
6- 	"testing" 
7- 
86	"github.com/mark3labs/mcp-go/mcp" 
97	"github.com/stretchr/testify/assert" 
8+ 	"testing" 
9+ 	"time" 
1010)
1111
1212func  TestMCPServer_NewMCPServer (t  * testing.T ) {
@@ -105,6 +105,109 @@ func TestMCPServer_Capabilities(t *testing.T) {
105105		})
106106	}
107107}
108+ func  TestMCPServer_Tools (t  * testing.T ) {
109+ 	tests  :=  []struct  {
110+ 		name                   string 
111+ 		action                 func (* MCPServer )
112+ 		expectedNotifications  int 
113+ 		validate               func (* testing.T , []ServerNotification , mcp.JSONRPCMessage )
114+ 	}{
115+ 		{
116+ 			name : "SetTools sends single notifications/tools/list_changed" ,
117+ 			action : func (server  * MCPServer ) {
118+ 				server .SetTools (ServerTool {
119+ 					Tool : mcp .NewTool ("test-tool-1" ),
120+ 					Handler : func (ctx  context.Context , request  mcp.CallToolRequest ) (* mcp.CallToolResult , error ) {
121+ 						return  & mcp.CallToolResult {}, nil 
122+ 					},
123+ 				}, ServerTool {
124+ 					Tool : mcp .NewTool ("test-tool-2" ),
125+ 					Handler : func (ctx  context.Context , request  mcp.CallToolRequest ) (* mcp.CallToolResult , error ) {
126+ 						return  & mcp.CallToolResult {}, nil 
127+ 					},
128+ 				})
129+ 			},
130+ 			expectedNotifications : 1 ,
131+ 			validate : func (t  * testing.T , notifications  []ServerNotification , toolsList  mcp.JSONRPCMessage ) {
132+ 				assert .Equal (t , "notifications/tools/list_changed" , notifications [0 ].Notification .Method )
133+ 				tools  :=  toolsList .(mcp.JSONRPCResponse ).Result .(mcp.ListToolsResult ).Tools 
134+ 				assert .Len (t , tools , 2 )
135+ 				assert .Equal (t , "test-tool-1" , tools [0 ].Name )
136+ 				assert .Equal (t , "test-tool-2" , tools [1 ].Name )
137+ 			},
138+ 		},
139+ 		{
140+ 			name : "AddTool sends multiple notifications/tools/list_changed" ,
141+ 			action : func (server  * MCPServer ) {
142+ 				server .AddTool (mcp .NewTool ("test-tool-1" ),
143+ 					func (ctx  context.Context , request  mcp.CallToolRequest ) (* mcp.CallToolResult , error ) {
144+ 						return  & mcp.CallToolResult {}, nil 
145+ 					})
146+ 				server .AddTool (mcp .NewTool ("test-tool-2" ),
147+ 					func (ctx  context.Context , request  mcp.CallToolRequest ) (* mcp.CallToolResult , error ) {
148+ 						return  & mcp.CallToolResult {}, nil 
149+ 					})
150+ 			},
151+ 			expectedNotifications : 2 ,
152+ 			validate : func (t  * testing.T , notifications  []ServerNotification , toolsList  mcp.JSONRPCMessage ) {
153+ 				assert .Equal (t , "notifications/tools/list_changed" , notifications [0 ].Notification .Method )
154+ 				tools  :=  toolsList .(mcp.JSONRPCResponse ).Result .(mcp.ListToolsResult ).Tools 
155+ 				assert .Len (t , tools , 2 )
156+ 				assert .Equal (t , "test-tool-1" , tools [0 ].Name )
157+ 				assert .Equal (t , "test-tool-2" , tools [1 ].Name )
158+ 			},
159+ 		},
160+ 		{
161+ 			name : "DeleteTools sends single notifications/tools/list_changed" ,
162+ 			action : func (server  * MCPServer ) {
163+ 				server .SetTools (
164+ 					ServerTool {Tool : mcp .NewTool ("test-tool-1" )},
165+ 					ServerTool {Tool : mcp .NewTool ("test-tool-2" )})
166+ 				server .DeleteTools ("test-tool-1" , "test-tool-2" )
167+ 			},
168+ 			expectedNotifications : 2 ,
169+ 			validate : func (t  * testing.T , notifications  []ServerNotification , toolsList  mcp.JSONRPCMessage ) {
170+ 				// One for SetTools 
171+ 				assert .Equal (t , "notifications/tools/list_changed" , notifications [0 ].Notification .Method )
172+ 				// One for DeleteTools 
173+ 				assert .Equal (t , "notifications/tools/list_changed" , notifications [1 ].Notification .Method )
174+ 				assert .Equal (t , "Tools not supported" , toolsList .(mcp.JSONRPCError ).Error .Message )
175+ 			},
176+ 		},
177+ 	}
178+ 	for  _ , tt  :=  range  tests  {
179+ 		t .Run (tt .name , func (t  * testing.T ) {
180+ 			ctx  :=  context .Background ()
181+ 			server  :=  NewMCPServer ("test-server" , "1.0.0" )
182+ 			_  =  server .HandleMessage (ctx , []byte (`{ 
183+               "jsonrpc": "2.0", 
184+               "id": 1, 
185+               "method": "initialize" 
186+             }` ))
187+ 			notifications  :=  make ([]ServerNotification , 0 )
188+ 			tt .action (server )
189+ 			for  done  :=  false ; ! done ; {
190+ 				select  {
191+ 				case  serverNotification  :=  <- server .notifications :
192+ 					notifications  =  append (notifications , serverNotification )
193+ 					if  len (notifications ) ==  tt .expectedNotifications  {
194+ 						done  =  true 
195+ 					}
196+ 				case  <- time .After (1  *  time .Second ):
197+ 					done  =  true 
198+ 				}
199+ 			}
200+ 			assert .Len (t , notifications , tt .expectedNotifications )
201+ 			toolsList  :=  server .HandleMessage (ctx , []byte (`{ 
202+               "jsonrpc": "2.0", 
203+               "id": 1, 
204+               "method": "tools/list" 
205+             }` ))
206+ 			tt .validate (t , notifications , toolsList .(mcp.JSONRPCMessage ))
207+ 		})
208+ 
209+ 	}
210+ }
108211
109212func  TestMCPServer_HandleValidMessages (t  * testing.T ) {
110213	server  :=  NewMCPServer ("test-server" , "1.0.0" ,
0 commit comments