Skip to content

fix: reject request IDs exceeding Number.MAX_SAFE_INTEGER#1801

Open
kai-agent-free wants to merge 2 commits intomodelcontextprotocol:mainfrom
kai-agent-free:fix/request-id-max-safe-integer
Open

fix: reject request IDs exceeding Number.MAX_SAFE_INTEGER#1801
kai-agent-free wants to merge 2 commits intomodelcontextprotocol:mainfrom
kai-agent-free:fix/request-id-max-safe-integer

Conversation

@kai-agent-free
Copy link
Copy Markdown

Summary

Fixes #1765 — Server hangs indefinitely when receiving a JSON-RPC request with an id exceeding Number.MAX_SAFE_INTEGER.

Problem

When a client sends a request with id: 9007199254740992 (MAX_SAFE_INTEGER + 1), JSON.parse silently loses precision on the integer. The server accepts the request but the response never gets properly routed, causing a permanent deadlock with no error output.

A single malformed request is enough to freeze the server — this is a denial-of-service vector.

Fix

Added a .refine() check to RequestIdSchema that validates numeric IDs fall within Number.MIN_SAFE_INTEGER to Number.MAX_SAFE_INTEGER. IDs outside this range now fail Zod validation, causing the server to return a proper JSON-RPC parse error (400) instead of silently hanging.

Changes

  • packages/core/src/types/schemas.ts: Added refine() to RequestIdSchema for safe integer range validation
  • packages/core/test/types.test.ts: Added tests for RequestIdSchema (string IDs, safe integers, unsafe integers, non-integers)

Testing

All existing tests pass (484 tests in core). New tests verify:

  • String IDs accepted ✅
  • Safe integer IDs accepted ✅
  • IDs > MAX_SAFE_INTEGER rejected ✅
  • Non-integer numeric IDs rejected ✅

Adds a validation refinement to RequestIdSchema to reject numeric
request IDs outside the safe integer range. Previously, a single
request with an ID > MAX_SAFE_INTEGER (e.g. 9007199254740992) would
cause the server to hang indefinitely with no error response, as
JSON.parse silently loses precision on large integers.

The fix validates that numeric IDs fall within Number.MIN_SAFE_INTEGER
to Number.MAX_SAFE_INTEGER, causing the Zod parse to fail and return
a proper JSON-RPC error response instead of silently hanging.

Fixes modelcontextprotocol#1765
@kai-agent-free kai-agent-free requested a review from a team as a code owner March 28, 2026 12:44
@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Mar 28, 2026

🦋 Changeset detected

Latest commit: 3d65ed9

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@modelcontextprotocol/core Patch

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

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new bot commented Mar 28, 2026

Open in StackBlitz

@modelcontextprotocol/client

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/client@1801

@modelcontextprotocol/server

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/server@1801

@modelcontextprotocol/express

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/express@1801

@modelcontextprotocol/hono

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/hono@1801

@modelcontextprotocol/node

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/node@1801

commit: 3d65ed9

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Server hangs indefinitely when JSON-RPC request ID exceeds

1 participant