Repository navigation
Conversation
…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
|
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 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. |
|
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 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 Thank you again for contributing. We would love to see more from you on the new stack! |
Summary
A request cancelled via
notifications/cancelledleaves its original HTTP POST hangingindefinitely on
StatelessHTTPServerTransport.The transport's response waiter (registered in
handleJSONRPCRequest) is only ever resumedby a matching JSON-RPC response through
send(_:)or by fullterminate(). Perbase-protocol cancellation semantics the server correctly sends no response for a
cancelled request, so nothing resumes the waiter and the
withCheckedThrowingContinuationnever 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 thatMUST.
Fix
When a
CancelledNotificationfor an in-flight request arrives, complete that request's HTTPexchange 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:its id,
Full MCP suite green (552 tests).
Fixes #255