Skip to content

mcpcompat/server: refuse dispatch of abandoned requests - #415

Merged
rdimitrov merged 4 commits into
mainfrom
fix/414-refuse-abandoned-requests
Nov 20, 2026
Merged

rdimitrov merged 4 commits into
mainfrom
fix/414-refuse-abandoned-requests

Conversation

@jhrozek

@jhrozek jhrozek commented Oct 26, 2026

Copy link
Copy Markdown
Contributor

Fixes #414.

Problem

When a client disconnects after its POST is queued but before go-sdk dispatches it, ServeHTTP has already cleared the per-request context entry. sessionDispatchMiddleware then silently fell back to the session's base context. That context belongs to a different request: the initialize POST, 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 carries X-MCP-Req-Nonce but whose entry is gone is now refused with a JSON-RPC internal error (request context no longer available). The refusal happens before fireBeforeHooks, so neither the hooks nor the handler run. It is logged at DEBUG.

  • Notifications are unchanged. They are told apart by the 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.
  • Messages without the nonce are unchanged. This covers stdio, in-process use and SSE.
  • Stateless mode and initialize cannot hit this. In both cases go-sdk closes the session and drains in-flight work inside ServeHTTP, before the nonce is cleared. The code comments explain this.
  • Deliberate spec deviation. MCP 2025-11-25 says disconnection SHOULD NOT be interpreted as cancellation. The comment at the change states the deviation. No EventStore is configured, so the client could not resume the stream and recover the result anyway (cf. the go-sdk TODO at streamable.go:1722).
  • Test seam. The unexported testReceivingMiddleware on MCPServer is set only by the rehydrated-session test, which uses it to stall the session queue.

Tests

  • TestIsNotificationMethod and TestSessionDispatchMiddleware_AbandonedRequest: unit tests of the middleware. They cover refusal, notification pass-through, no header, and the bridged case.
  • TestAbandonedRequest_LocalSession_{Refused,StillUsable} and TestAbandonedRequest_NotificationsUnaffected: end-to-end tests. They stall the queue in OnRegisterSession, then queue a tools/call with an already-cancelled context.
  • TestAbandonedRequest_RehydratedSession_Refused: the same refusal on a session served through serveRehydrated.
  • The tests assert a positive "dispatched and refused" signal (the DEBUG line) before asserting that the tool and hook counters are zero. With the refusal branch removed, the four refusal tests fail.

task and task license-check pass. 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). main fails the same way, so this predates the branch.

Behaviour change (release note)

mcpcompat/server: a request whose client disconnected before the request was dispatched is no longer executed. This includes the server cancelling the request first, for example a proxy timeout or a shutdown. Previously such a request ran using the identity, audit and telemetry values of a different request on the same session. A request whose dispatch had already started still runs to completion. Notifications, stateless servers, the SSE transport, and in-process/stdio use are unaffected. Clients that send a request and disconnect without waiting for the result may find it was not executed.

Follow-up

After release, bump toolhive-core in stacklok/toolhive and run the vMCP tests (task test, LABEL_FILTER=vmcp task test-e2e).

🤖 Generated with Claude Code

jhrozek and others added 4 commits October 26, 2026 14:31
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 rdimitrov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@rdimitrov
rdimitrov merged commit aac3802 into main Nov 20, 2026
6 checks passed
@rdimitrov
rdimitrov deleted the fix/414-refuse-abandoned-requests branch November 20, 2026 09:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

mcpcompat/server: refuse dispatch of a request whose per-request context is gone

2 participants