Correlate invalid JSON-RPC envelope errors with the original request id#2857
Open
GauthamPrabhuM wants to merge 1 commit into
Conversation
Per JSON-RPC 2.0, a message that is valid JSON but not a valid request object must be answered with an Invalid Request (-32600) error that echoes the original request id when it is detectable, so clients can correlate the failure. Previously: - Streamable HTTP replied 400 with id null and code -32602 (Invalid params), breaking client-side request/response correlation. - stdio sent no response at all; the validation exception was forwarded into the read stream and silently dropped by the dispatcher. Both transports now extract the id from the raw payload (via the new mcp.types.jsonrpc.extract_request_id helper) and reply with a correlated -32600 error. On stdio, lines without a detectable id (parse errors, malformed notifications, ids of an invalid type) keep the previous behavior of forwarding the exception without a response. Fixes modelcontextprotocol#2848 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
d98117c to
e3f1cfd
Compare
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
Fixes #2848.
A JSON-RPC message that is valid JSON but not a valid request object (wrong
jsonrpcversion, missingjsonrpc, non-stringmethod, etc.) was not answered with an error that a client could correlate back to its request. Per JSON-RPC 2.0, the server should reply with an Invalid Request (-32600) error that echoes the original requestidwhen it is detectable (andnullotherwise).Previously the behavior was inconsistent and uncorrelatable:
400withid: nulland code-32602(Invalid params). A client that pipelines requests1, 2, 3and trips this on#2saw a response with a non-matching id and no way to know which request failed.Changes
mcp.types.jsonrpc.extract_request_id()— best-effort extraction of theidfrom a raw payload that failed envelope validation, returningNonewhen no valid id is present (booland fractional values are rejected, since they are not validRequestIdtypes).server/streamable_http.py): invalid envelopes now reply-32600with the correlatedidinstead of-32602/null._create_error_responsegained an optionalrequest_idparameter.server/stdio.py): an id-bearing line that fails validation now emits a correlated-32600error response on the write stream. Lines without a detectable id (parse errors, malformed notifications, invalid id types) keep the previous behavior of forwarding the exception without a response.Testing
bool, fractional).test_hosting_http.pyto assert the new-32600code for the batched-request case.-32600errors and a follow-uppingstill succeeds../scripts/test(1788 passed), 100% coverage,ruff,pyrightall clean.Migration / breaking change
The error code returned for an invalid JSON-RPC envelope over both transports changes from
-32602(Invalid params) to-32600(Invalid Request), and the responseidis now populated when detectable. This is a behavioral change to error responses; documented here for reviewers since it may affect clients that string-matched on the prior code.🤖 Generated with Claude Code