Skip to content

Commit 9a9ef76

Browse files
committed
fix: don't share mutex between tools/resources
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 3d1bfca commit 9a9ef76

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

@@ -876,13 +877,13 @@ func (s *MCPServer) handleReadResource(
876877
s.resourcesMu.RUnlock()
877878

878879
finalHandler := handler
879-
s.middlewareMu.RLock()
880+
s.resourceMiddlewareMu.RLock()
880881
mw := s.resourceHandlerMiddlewares
881882
// Apply middlewares in reverse order
882883
for i := len(mw) - 1; i >= 0; i-- {
883884
finalHandler = mw[i](finalHandler)
884885
}
885-
s.middlewareMu.RUnlock()
886+
s.resourceMiddlewareMu.RUnlock()
886887

887888
contents, err := finalHandler(ctx, request)
888889
if err != nil {
@@ -1138,14 +1139,14 @@ func (s *MCPServer) handleToolCall(
11381139

11391140
finalHandler := tool.Handler
11401141

1141-
s.middlewareMu.RLock()
1142+
s.toolMiddlewareMu.RLock()
11421143
mw := s.toolHandlerMiddlewares
11431144

11441145
// Apply middlewares in reverse order
11451146
for i := len(mw) - 1; i >= 0; i-- {
11461147
finalHandler = mw[i](finalHandler)
11471148
}
1148-
s.middlewareMu.RUnlock()
1149+
s.toolMiddlewareMu.RUnlock()
11491150

11501151
result, err := finalHandler(ctx, request)
11511152
if err != nil {

0 commit comments

Comments
 (0)