fix: stop sending JSON-RPC response on request cancellation#2481
Open
flobo3 wants to merge 1 commit intomodelcontextprotocol:mainfrom
Open
fix: stop sending JSON-RPC response on request cancellation#2481flobo3 wants to merge 1 commit intomodelcontextprotocol:mainfrom
flobo3 wants to merge 1 commit intomodelcontextprotocol:mainfrom
Conversation
|
This is the right fix — sending an |
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.
Summary
RequestResponder.cancel()inmcp/shared/session.pysent anErrorData(code=0, message="Request cancelled")JSON-RPC response back to the sender after anotifications/cancelledwas received. The MCP cancellation spec explicitly states:This unexpected response caused strict clients (e.g. Claude Code) to treat it as an unknown message ID, close the transport, and reconnect — making every cancellation of a long-running tool cost a full reconnect cycle.
Changes
src/mcp/shared/session.py: Removed_send_response()call fromRequestResponder.cancel(). Added spec reference comment.src/mcp/server/lowlevel/server.py: Updated log message and comment to reflect that no response is sent (was "duplicate response suppressed", now "no response sent per spec").tests/shared/test_session.py: Rewrotetest_request_cancellationto verify the server handler's cancel scope is cancelled, rather than expecting anMCPErrorresponse.tests/server/test_cancel_handling.py: Addedrequest_read_timeout_secondsto the cancelled request so it times out cleanly (no response is sent).Fixes #2480