fix(transport): validate JSON-RPC request ID is a safe integer#1809
Open
Aboudjem wants to merge 3 commits intomodelcontextprotocol:mainfrom
Open
fix(transport): validate JSON-RPC request ID is a safe integer#1809Aboudjem wants to merge 3 commits intomodelcontextprotocol:mainfrom
Aboudjem wants to merge 3 commits intomodelcontextprotocol:mainfrom
Conversation
🦋 Changeset detectedLatest commit: 0a62c4c The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
@modelcontextprotocol/client
@modelcontextprotocol/server
@modelcontextprotocol/express
@modelcontextprotocol/hono
@modelcontextprotocol/node
commit: |
When a JSON-RPC request arrives with a numeric ID exceeding Number.MAX_SAFE_INTEGER, Zod schema validation correctly rejects it (z.number().int() enforces safe integer range in Zod v4). However, the rejected message falls through to the "Unknown message type" error handler, which silently drops it without sending any response back to the client. This causes the server to appear permanently hung. This fix detects request-like messages that fail schema validation in the onmessage handler and responds with a JSON-RPC -32600 (Invalid Request) error instead of silently ignoring them. Closes modelcontextprotocol#1765 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
tsgo flags `mock.calls[0][0]` as "Object is possibly undefined" — add `!` after the first index access to match the convention used everywhere else in this test file. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
17f6f8c to
d18ce5f
Compare
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
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 - Server hangs indefinitely when JSON-RPC request ID exceeds
Number.MAX_SAFE_INTEGER.Root cause: When a request arrives with a numeric ID like
9007199254740992(MAX_SAFE_INTEGER + 1), Zod v4'sz.number().int()correctly rejects it (.int()enforces the safe integer range). However, the rejected message falls through to theelsebranch in theonmessagehandler, which calls_onerrorwith "Unknown message type" and silently drops the message without ever sending a response. The client waits forever for a response that never comes.Fix: In the
onmessagehandler withinProtocol.connect(), detect request-like messages that fail schema validation (havejsonrpc,id, andmethodfields but didn't passisJSONRPCRequest) and respond with a JSON-RPC-32600(Invalid Request) error instead of silently dropping them.Changes
packages/core/src/shared/protocol.ts- Added validation in theonmessagehandler to detect and respond to malformed request messagespackages/core/test/shared/protocol.test.ts- Added 3 tests: unsafe integer ID returns error, MAX_SAFE_INTEGER works normally, string IDs work normallyTest plan