Repository navigation
mcpcompat/server: refuse dispatch of abandoned requests - #415
Merged
Merged
Conversation
When a client disconnects, go-sdk returns from servePOST but keeps the queued message, and ServeHTTP clears the per-request context entry. The dispatch middleware then fell back to the session's base context, which belongs to another request, so the call ran with that request's identity, audit and telemetry values. Refuse non-notification messages whose nonce entry is gone with an internal error instead. Notifications and messages without the nonce header are unchanged. Also correct the comments on the bridge and add why stateless mode cannot hit this. Refs #414 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Cover a local stateful session whose connection goroutine is stalled in OnRegisterSession: a tools/call POSTed with an already-cancelled context is queued, dispatched after release, and refused (DEBUG log line proves dispatch) without running the tool or before-call hook. The session stays usable afterwards and notifications are unaffected. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Add an unexported test-only seam (MCPServer.testReceivingMiddleware, installed by buildServer after the dispatch middleware; never set in production) and an internal test that stalls a rehydrated session's queue with a blocked notifications/cancelled, queues a tools/call with an already-cancelled request context, and asserts the call is dispatched and refused without running the tool or before-call hook. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Release the blocking hook/middleware inside the server-close cleanup: t.Cleanup runs last-in first-out, so registering the release first made it run after httptest.Server.Close, which waits for the blocked request. Also correct the seam comment (later middleware wraps and runs before the dispatch middleware) and the go-sdk TODO line reference. Co-Authored-By: Claude Opus 5.5 <[email protected]>
rdimitrov
approved these changes
Nov 19, 2026
rdimitrov
left a comment
Member
There was a problem hiding this comment.
LGTM. Refusing the abandoned request before fireBeforeHooks is the right fix. Running it on the session's base context leaked the initialize request's identity and audit values into another request, and running it with no values could look anonymous. The change is small and the e2e tests reproduce the real queue race against go-sdk.
Non-blocking follow-up: notifications that lose their entry (they get a 202 before dispatch, so this happens often) still run on the base context with the initialize request's values. It's the same bleed with a lower impact. Running them on a context with the bridged values removed would close it.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #414.
Problem
When a client disconnects after its POST is queued but before go-sdk dispatches it,
ServeHTTPhas already cleared the per-request context entry.sessionDispatchMiddlewarethen silently fell back to the session's base context. That context belongs to a different request: theinitializePOST, or the first request that rehydrated the session on this replica. The before-hooks and the handler ran with that request's identity, audit and telemetry values, for a client that would never receive the result.Change
In
sessionDispatchMiddleware, a request that carriesX-MCP-Req-Noncebut whose entry is gone is now refused with a JSON-RPC internal error (request context no longer available). The refusal happens beforefireBeforeHooks, so neither the hooks nor the handler run. It is logged at DEBUG.notifications/method prefix, because go-sdk exposes no JSON-RPC ID in middleware. Notification-only POSTs are acknowledged with 202 before dispatch, so they routinely lose the entry; this stays best-effort as before.initializecannot hit this. In both cases go-sdk closes the session and drains in-flight work insideServeHTTP, before the nonce is cleared. The code comments explain this.EventStoreis configured, so the client could not resume the stream and recover the result anyway (cf. the go-sdk TODO atstreamable.go:1722).testReceivingMiddlewareonMCPServeris set only by the rehydrated-session test, which uses it to stall the session queue.Tests
TestIsNotificationMethodandTestSessionDispatchMiddleware_AbandonedRequest: unit tests of the middleware. They cover refusal, notification pass-through, no header, and the bridged case.TestAbandonedRequest_LocalSession_{Refused,StillUsable}andTestAbandonedRequest_NotificationsUnaffected: end-to-end tests. They stall the queue inOnRegisterSession, then queue atools/callwith an already-cancelled context.TestAbandonedRequest_RehydratedSession_Refused: the same refusal on a session served throughserveRehydrated.taskandtask license-checkpass. The new tests pass with-race -count=10.Note:
go test -race -count=10 ./mcpcompat/server/...fails on macOS from ephemeral-port exhaustion (can't assign requested address).mainfails the same way, so this predates the branch.Behaviour change (release note)
Follow-up
After release, bump
toolhive-coreinstacklok/toolhiveand run the vMCP tests (task test,LABEL_FILTER=vmcp task test-e2e).🤖 Generated with Claude Code