Skip to content

Commit 71a5805

Browse files
authored
fix: don't share mutex between tools/resources (#574)
I noticed that #569 was merged (thanks!) and looked at the diff again with fresh eyes and noticed that I reused the existing mutex for tool middlewares within the resource middlewares. This means that, at least while processing middlewares, it's possible a resource call could be blocked waiting on a lock because of a tool call or vice-versa. Since there's a separate mutex for tools, resources, etc, it seems there's a desire to not block each other. This commit renames the existing middleware mutex to better clarify it's specifically for tool middlewares, and adds a new mutex for use specifically with resource middlewares. Signed-off-by: TJ Hoplock <[email protected]>
1 parent 2300366 commit 71a5805

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

server/server.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,10 @@ type NotificationHandlerFunc func(ctx context.Context, notification mcp.JSONRPCN
147147
type MCPServer struct {
148148
// Separate mutexes for different resource types
149149
resourcesMu sync.RWMutex
150+
resourceMiddlewareMu sync.RWMutex
150151
promptsMu sync.RWMutex
151152
toolsMu sync.RWMutex
152-
middlewareMu sync.RWMutex
153+
toolMiddlewareMu sync.RWMutex
153154
notificationHandlersMu sync.RWMutex
154155
capabilitiesMu sync.RWMutex
155156
toolFiltersMu sync.RWMutex
@@ -221,9 +222,9 @@ func WithToolHandlerMiddleware(
221222
toolHandlerMiddleware ToolHandlerMiddleware,
222223
) ServerOption {
223224
return func(s *MCPServer) {
224-
s.middlewareMu.Lock()
225+
s.toolMiddlewareMu.Lock()
225226
s.toolHandlerMiddlewares = append(s.toolHandlerMiddlewares, toolHandlerMiddleware)
226-
s.middlewareMu.Unlock()
227+
s.toolMiddlewareMu.Unlock()
227228
}
228229
}
229230

@@ -233,9 +234,9 @@ func WithResourceHandlerMiddleware(
233234
resourceHandlerMiddleware ResourceHandlerMiddleware,
234235
) ServerOption {
235236
return func(s *MCPServer) {
236-
s.middlewareMu.Lock()
237+
s.resourceMiddlewareMu.Lock()
237238
s.resourceHandlerMiddlewares = append(s.resourceHandlerMiddlewares, resourceHandlerMiddleware)
238-
s.middlewareMu.Unlock()
239+
s.resourceMiddlewareMu.Unlock()
239240
}
240241
}
241242

@@ -900,13 +901,13 @@ func (s *MCPServer) handleReadResource(
900901
s.resourcesMu.RUnlock()
901902

902903
finalHandler := handler
903-
s.middlewareMu.RLock()
904+
s.resourceMiddlewareMu.RLock()
904905
mw := s.resourceHandlerMiddlewares
905906
// Apply middlewares in reverse order
906907
for i := len(mw) - 1; i >= 0; i-- {
907908
finalHandler = mw[i](finalHandler)
908909
}
909-
s.middlewareMu.RUnlock()
910+
s.resourceMiddlewareMu.RUnlock()
910911

911912
contents, err := finalHandler(ctx, request)
912913
if err != nil {
@@ -1162,14 +1163,14 @@ func (s *MCPServer) handleToolCall(
11621163

11631164
finalHandler := tool.Handler
11641165

1165-
s.middlewareMu.RLock()
1166+
s.toolMiddlewareMu.RLock()
11661167
mw := s.toolHandlerMiddlewares
11671168

11681169
// Apply middlewares in reverse order
11691170
for i := len(mw) - 1; i >= 0; i-- {
11701171
finalHandler = mw[i](finalHandler)
11711172
}
1172-
s.middlewareMu.RUnlock()
1173+
s.toolMiddlewareMu.RUnlock()
11731174

11741175
result, err := finalHandler(ctx, request)
11751176
if err != nil {

0 commit comments

Comments
 (0)