fix: handle JSON-RPC request IDs exceeding MAX_SAFE_INTEGER#1775
Open
cyphercodes wants to merge 1 commit intomodelcontextprotocol:mainfrom
Open
fix: handle JSON-RPC request IDs exceeding MAX_SAFE_INTEGER#1775cyphercodes wants to merge 1 commit intomodelcontextprotocol:mainfrom
cyphercodes wants to merge 1 commit intomodelcontextprotocol:mainfrom
Conversation
Previously, the SDK used Number() to convert request/response IDs, which causes precision loss for values exceeding Number.MAX_SAFE_INTEGER (9007199254740991). This led to server hangs when receiving requests with large IDs because the response handler lookup would fail. Changes: - Changed Map key types from number to RequestId (string | number) - Removed Number() conversions on request/response IDs - Added undefined check for response.id in _onresponse - Updated TaskManagerHost.removeProgressHandler signature - Updated processInboundResponse and related methods Fixes modelcontextprotocol#1765
|
@modelcontextprotocol/client
@modelcontextprotocol/server
@modelcontextprotocol/express
@modelcontextprotocol/hono
@modelcontextprotocol/node
commit: |
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 #1765
Previously, the SDK used
Number()to convert request/response IDs, which causes precision loss for values exceedingNumber.MAX_SAFE_INTEGER(9007199254740991). This led to server hangs when receiving requests with large IDs because the response handler lookup would fail.Changes
numbertoRequestId(string | number) in Protocol classNumber()conversions on request/response IDs in_onresponseand_onprogressresponse.idin_onresponseto handle error responses without IDsTaskManagerHost.removeProgressHandlersignature to useRequestIdTaskManager.processInboundResponseand related methods to useRequestIdTaskManager._taskProgressTokensMap value type fromnumbertoRequestIdTesting
All 454 existing tests pass. The fix ensures that request IDs are preserved as-is (whether string or number) throughout the request/response lifecycle, preventing precision loss issues with large numeric IDs.
Root Cause
When a client sends a JSON-RPC request with an ID like
9007199254740992(MAX_SAFE_INTEGER + 1), converting it to Number would result in a different value due to floating-point precision limits. This caused the response handler lookup to fail, leaving the request pending indefinitely (server hang).