Fix JSON-RPC error response ID matching #1720
Draft
+213
−2
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.
Motivation and Context
When an MCP server returns a JSON-RPC response with the request ID as a string (e.g.,
"id": "0") but the client sent the request with an integer ID (e.g.,"id": 0), the response fails to match the pending request. This causes the client to timeout waiting for a response that was already received but couldn't be correlated.The root cause is in
src/mcp/shared/session.py:256-260where the client stores pending requests using an integer counter:When a response arrives, the lookup
self._response_streams.pop(response_id, None)fails silently ifresponse_idis"0"(string) because Python dict lookups are type-sensitive (0 != "0").This is valid per the JSON-RPC spec and the SDK's own type definition at
src/mcp/types.py:40:How Has This Been Tested?
Added three new unit tests in
tests/shared/test_session.py:test_response_id_type_mismatch_string_to_int- success responses with string IDs match integer request IDstest_error_response_id_type_mismatch_string_to_int- error responses with string IDs match integer request IDstest_response_id_non_numeric_string_no_match- non-numeric string IDs (like"abc") don't incorrectly matchBreaking Changes
None. This is a backwards-compatible fix that makes the client more tolerant of server responses.
Types of changes
Bug fix (non-breaking change which fixes an issue)
New feature (non-breaking change which adds functionality)
Breaking change (fix or feature that would cause existing functionality to change)
Documentation update
Checklist
I have read the MCP Documentation
My code follows the repository's style guidelines
New and existing tests pass locally
I have added appropriate error handling
I have added or updated documentation as needed
Additional context
The fix adds a
_pop_response_stream()helper method that:First tries an exact match lookup
If the ID is a string, tries converting to int and looks up again
If the ID is an int, tries converting to string and looks up again
This handles the common case where servers echo back IDs in a different but semantically equivalent format.