fix: handle JSON-RPC request IDs exceeding MAX_SAFE_INTEGER#1775
fix: handle JSON-RPC request IDs exceeding MAX_SAFE_INTEGER#1775cyphercodes 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: |
|
Per discussion on #1765: numeric IDs above The |
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).