Skip to content

fix(StatelessHTTPServerTransport): complete a cancelled request's HTTP exchange (#255) - #268

Open
jpurnell wants to merge 1 commit into
modelcontextprotocol:mainfrom
jpurnell:fix/stateless-cancelled-hangs
Open

fix(StatelessHTTPServerTransport): complete a cancelled request's HTTP exchange (#255)#268
jpurnell wants to merge 1 commit into
modelcontextprotocol:mainfrom
jpurnell:fix/stateless-cancelled-hangs

Conversation

@jpurnell

Copy link
Copy Markdown

Summary

A request cancelled via notifications/cancelled leaves its original HTTP POST hanging
indefinitely
on StatelessHTTPServerTransport.

The transport's response waiter (registered in handleJSONRPCRequest) is only ever resumed
by a matching JSON-RPC response through send(_:) or by full terminate(). Per
base-protocol cancellation semantics the server correctly sends no response for a
cancelled request, so nothing resumes the waiter and the withCheckedThrowingContinuation
never returns.

This also violates the Streamable HTTP requirement (2025-11-25, Sending Messages to the
Server
#5): a POST carrying a JSON-RPC request MUST receive either an SSE stream or a
JSON object. A spec-conformant client that cancels (same section, #6: "To cancel, the client
SHOULD explicitly send an MCP CancelledNotification") drives the server into violating that
MUST.

Fix

When a CancelledNotification for an in-flight request arrives, complete that request's HTTP
exchange with a JSON-RPC error — implementation-defined server-error code -32002,
echoing the request id so the client can correlate — so the POST returns a JSON object
instead of hanging. The notification is still forwarded to the server so it can cancel the
underlying work. No-op when the cancellation references an unknown/absent request id.

Tests

Adds testCancelledRequestCompletesHTTPExchange:

  • the cancelled request's POST completes (no hang) with a 200 carrying a JSON-RPC error for
    its id,
  • the cancellation notification returns 202,
  • both the request and the cancellation still reach the server.

Full MCP suite green (552 tests).

Fixes #255

…exchange

A request cancelled via notifications/cancelled left its original HTTP POST hanging
indefinitely. The transport's response waiter is only ever resumed by a matching JSON-RPC
response or by terminate(); a cancelled request correctly produces no response (the server
must stay silent), so nothing resumed the waiter and the withCheckedThrowingContinuation in
handleJSONRPCRequest never returned. This also violates the Streamable HTTP requirement that
a POST carrying a request MUST receive either an SSE stream or a JSON object.

When a CancelledNotification for an in-flight request arrives, complete that request's HTTP
exchange with a JSON-RPC error (implementation-defined server-error code -32002, echoing the
request id) so the POST returns a JSON object. The notification is still forwarded to the
server so it can cancel the underlying work.

Adds a regression test: the cancelled request's POST completes with a JSON-RPC error for its
id (not a hang), the cancellation returns 202, and both messages still reach the server.

Fixes modelcontextprotocol#255
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.

StatelessHTTPServerTransport: notifications/cancelled leaves the original POST hanging (HTTP exchange never completed)

1 participant