Skip to content

Commit f7ef6bc

Browse files
committed
Fixes
1 parent ccb5cdb commit f7ef6bc

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

server/session.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@ type ClientSession interface {
2323
type SessionWithTools interface {
2424
ClientSession
2525
// GetSessionTools returns the tools specific to this session, if any
26+
// This method must be thread-safe for concurrent access
2627
GetSessionTools() map[string]ServerTool
2728
// SetSessionTools sets tools specific to this session
29+
// This method must be thread-safe for concurrent access
2830
SetSessionTools(tools map[string]ServerTool)
2931
}
3032

@@ -222,6 +224,7 @@ func (s *MCPServer) AddSessionTools(sessionID string, tools ...ServerTool) error
222224
return ErrSessionDoesNotSupportTools
223225
}
224226

227+
// Get existing tools (this should return a thread-safe copy)
225228
sessionTools := session.GetSessionTools()
226229

227230
// Create a new map to avoid concurrent modification issues
@@ -239,6 +242,7 @@ func (s *MCPServer) AddSessionTools(sessionID string, tools ...ServerTool) error
239242
newSessionTools[tool.Tool.Name] = tool
240243
}
241244

245+
// Set the tools (this should be thread-safe)
242246
session.SetSessionTools(newSessionTools)
243247

244248
// Send notification only to this session
@@ -272,6 +276,7 @@ func (s *MCPServer) DeleteSessionTools(sessionID string, names ...string) error
272276
return ErrSessionDoesNotSupportTools
273277
}
274278

279+
// Get existing tools (this should return a thread-safe copy)
275280
sessionTools := session.GetSessionTools()
276281
if sessionTools == nil {
277282
return nil
@@ -290,6 +295,7 @@ func (s *MCPServer) DeleteSessionTools(sessionID string, names ...string) error
290295
delete(newSessionTools, name)
291296
}
292297

298+
// Set the tools (this should be thread-safe)
293299
session.SetSessionTools(newSessionTools)
294300

295301
// Send notification only to this session

server/session_test.go

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ type sessionTestClientWithTools struct {
4545
notificationChannel chan mcp.JSONRPCNotification
4646
initialized bool
4747
sessionTools map[string]ServerTool
48+
mu sync.RWMutex // Mutex to protect concurrent access to sessionTools
4849
}
4950

5051
func (f *sessionTestClientWithTools) SessionID() string {
@@ -64,11 +65,36 @@ func (f *sessionTestClientWithTools) Initialized() bool {
6465
}
6566

6667
func (f *sessionTestClientWithTools) GetSessionTools() map[string]ServerTool {
67-
return f.sessionTools
68+
f.mu.RLock()
69+
defer f.mu.RUnlock()
70+
71+
// Return a copy of the map to prevent concurrent modification
72+
if f.sessionTools == nil {
73+
return nil
74+
}
75+
76+
toolsCopy := make(map[string]ServerTool, len(f.sessionTools))
77+
for k, v := range f.sessionTools {
78+
toolsCopy[k] = v
79+
}
80+
return toolsCopy
6881
}
6982

7083
func (f *sessionTestClientWithTools) SetSessionTools(tools map[string]ServerTool) {
71-
f.sessionTools = tools
84+
f.mu.Lock()
85+
defer f.mu.Unlock()
86+
87+
// Create a copy of the map to prevent concurrent modification
88+
if tools == nil {
89+
f.sessionTools = nil
90+
return
91+
}
92+
93+
toolsCopy := make(map[string]ServerTool, len(tools))
94+
for k, v := range tools {
95+
toolsCopy[k] = v
96+
}
97+
f.sessionTools = toolsCopy
7298
}
7399

74100
// Verify that both implementations satisfy their respective interfaces

0 commit comments

Comments
 (0)