fix: remove JSON-RPC ID type coercion for spec-compliant strict matching#2428
Open
KeWang0622 wants to merge 1 commit intomodelcontextprotocol:mainfrom
Open
fix: remove JSON-RPC ID type coercion for spec-compliant strict matching#2428KeWang0622 wants to merge 1 commit intomodelcontextprotocol:mainfrom
KeWang0622 wants to merge 1 commit intomodelcontextprotocol:mainfrom
Conversation
Remove the _normalize_request_id method that was introduced in PR modelcontextprotocol#1720 to coerce string response IDs to integers. Per JSON-RPC 2.0 spec, the response ID MUST be the same as the value of the id member in the Request Object, which implies exact matching including type. The type coercion made request ID 1 and "1" interchangeable, which violates the spec. Servers that echo back IDs in a different type are non-compliant and should not be worked around in the SDK. Update tests to verify that type-mismatched IDs are correctly rejected rather than silently coerced. Github-Issue: modelcontextprotocol#1795
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
Remove the
_normalize_request_idmethod that coerces string response IDs to integers. This restores spec-compliant strict ID matching as requested in #1795.Problem
PR #1720 introduced automatic type coercion for JSON-RPC response IDs, allowing string IDs like
"0"to match integer request IDs like0. While this made the client more tolerant of non-compliant servers, it violates the JSON-RPC 2.0 and MCP specifications:As @Kludex noted in #1795: "#1720 is not compliant" and "This should not be configurable!"
The coercion made request ID
1and"1"interchangeable, which are semantically different values per the spec. Servers that echo back IDs in a different type are non-compliant and should not be worked around in the SDK.Changes
src/mcp/shared/session.py:_normalize_request_idmethod entirelytests/shared/test_session.py:test_response_id_type_mismatch_string_to_intandtest_error_response_id_type_mismatch_string_to_intto verify that type-mismatched IDs are now correctly rejected (request times out) rather than silently coercedtest_response_id_non_numeric_string_no_matchtest is unchanged since it already expected rejectionVerification
uv run --frozen ruff format .— no changes neededuv run --frozen ruff check .— all checks passeduv run --frozen pyright src/mcp/shared/session.py— 0 errorsuv run --frozen pytest tests/shared/test_session.py— 8/8 passedstrict-no-cover— no pragma issuesCloses #1795