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

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
@varun06

varun06 commented Oct 22, 2026

Copy link
Copy Markdown
Contributor

Hi @jpurnell, thank you for this contribution. We're consolidating overlapping PRs.

#298 covers the same issue (#255) and also isolates exchanges per HTTP request, so a cancel can't complete a different client's request that uses the same id. We'd also prefer not to reuse -32002, which MCP defines as "Resource not found".

We plan to close this PR once #298 is merged, so the fix isn't lost in between. If you think something here isn't covered by #298, please comment there or here and we'll make sure it's carried over.

@varun06

varun06 commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Hi @jpurnell, thank you for this PR, and for the care you put into the cancellation fix!

We have just opened the Swift SDK 1.0.0 release (#372, branch rel/1.0.0), a large rework of the SDK. It conflicts with this PR, and as far as we can tell it already fixes the same problem: an honored notifications/cancelled now ends the original exchange, with 202 for JSON or an ended stream for SSE (tests "An honored notifications/cancelled ends a JSON exchange with 202" / "...ends an SSE exchange's stream normally").

Could you check whether 1.0.0 covers your case? If it does, we would suggest closing this PR. If something is still missing, please open a new PR against rel/1.0.0 (or main once 1.0.0 merges).

Thank you again for contributing. We would love to see more from you on the new stack!

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)

2 participants