@@ -217,15 +217,23 @@ func (s *MCPServer) AddSessionTools(sessionID string, tools ...ServerTool) error
217217 }
218218
219219 sessionTools := session .GetSessionTools ()
220- if sessionTools == nil {
221- sessionTools = make (map [string ]ServerTool )
220+
221+ // Create a new map to avoid concurrent modification issues
222+ newSessionTools := make (map [string ]ServerTool , len (sessionTools )+ len (tools ))
223+
224+ // Copy existing tools
225+ if sessionTools != nil {
226+ for k , v := range sessionTools {
227+ newSessionTools [k ] = v
228+ }
222229 }
223230
231+ // Add new tools
224232 for _ , tool := range tools {
225- sessionTools [tool .Tool .Name ] = tool
233+ newSessionTools [tool .Tool .Name ] = tool
226234 }
227235
228- session .SetSessionTools (sessionTools )
236+ session .SetSessionTools (newSessionTools )
229237
230238 // Send notification only to this session
231239 s .SendNotificationToSpecificClient (sessionID , "notifications/tools/list_changed" , nil )
@@ -250,11 +258,20 @@ func (s *MCPServer) DeleteSessionTools(sessionID string, names ...string) error
250258 return nil
251259 }
252260
261+ // Create a new map to avoid concurrent modification issues
262+ newSessionTools := make (map [string ]ServerTool , len (sessionTools ))
263+
264+ // Copy existing tools except those being deleted
265+ for k , v := range sessionTools {
266+ newSessionTools [k ] = v
267+ }
268+
269+ // Remove specified tools
253270 for _ , name := range names {
254- delete (sessionTools , name )
271+ delete (newSessionTools , name )
255272 }
256273
257- session .SetSessionTools (sessionTools )
274+ session .SetSessionTools (newSessionTools )
258275
259276 // Send notification only to this session
260277 s .SendNotificationToSpecificClient (sessionID , "notifications/tools/list_changed" , nil )
0 commit comments