Skip to content

Refuse integer request ids beyond 128 bits - #688

Open
koic wants to merge 1 commit into
modelcontextprotocol:mainfrom
koic:refuse_integer_request_ids_beyond_128_bits
Open

koic wants to merge 1 commit into
modelcontextprotocol:mainfrom
koic:refuse_integer_request_ids_beyond_128_bits

Conversation

@koic

@koic koic commented Nov 15, 2026

Copy link
Copy Markdown
Member

Motivation and Context

A JSON-RPC id is written back into the response, and JsonRpcHandler and the transports accepted an integer id of any size. Writing an integer out in decimal takes time that grows faster than its length: on a Ruby built without GMP, a request whose id has 400,000 digits took 0.6 seconds to answer and one that fills the default 4 MiB body about a minute, most of it spent generating the refusal that echoes the id. Any client can send one, and the id is echoed whether the request is served or refused.

JsonRpcHandler now answers a request whose integer id is beyond 128 bits with -32600 Invalid Request and no id, before the method runs, and the Streamable HTTP transport answers such a body 400 before any of its own refusals echoes the id. 128 bits leaves room for any counter and for a UUID carried as a number. On the client, a server-to-client request with such an id is dropped unanswered over HTTP, and a ping with one is not answered over stdio, as for the other ids that cannot be written back.

How Has This Been Tested?

test/json_rpc_handler_test.rb, test/mcp/server/transports/streamable_http_transport_test.rb, test/mcp/client/http_test.rb and test/mcp/client/stdio_test.rb. Against the previous library the handler runs the method, the transport echoes the id, and both clients answer the request. The 400,000-digit request above is now refused in 0.07 seconds.

Breaking Changes

A request whose integer id does not fit in 128 bits is refused where it was served. Such an id is beyond what a JSON number can carry between implementations, so no client is expected to send one.

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

## Motivation and Context

A JSON-RPC id is written back into the response, and `JsonRpcHandler` and the transports accepted an integer id of any size.
Writing an integer out in decimal takes time that grows faster than its length: on a Ruby built without GMP,
a request whose id has 400,000 digits took 0.6 seconds to answer and one that fills the default 4 MiB body about a minute,
most of it spent generating the refusal that echoes the id. Any client can send one, and the id is echoed whether the request
is served or refused.

`JsonRpcHandler` now answers a request whose integer id is beyond 128 bits with `-32600` Invalid Request and no id,
before the method runs, and the Streamable HTTP transport answers such a body 400 before any of its own refusals echoes the id.
128 bits leaves room for any counter and for a UUID carried as a number. On the client, a server-to-client request with such an id
is dropped unanswered over HTTP, and a ping with one is not answered over stdio, as for the other ids that cannot be written back.

## How Has This Been Tested?

`test/json_rpc_handler_test.rb`, `test/mcp/server/transports/streamable_http_transport_test.rb`,
`test/mcp/client/http_test.rb` and `test/mcp/client/stdio_test.rb`. Against the previous library the handler runs the method,
the transport echoes the id, and both clients answer the request. The 400,000-digit request above is now refused in 0.07 seconds.

## Breaking Changes

A request whose integer id does not fit in 128 bits is refused where it was served. Such an id is beyond what a JSON number
can carry between implementations, so no client is expected to send one.

This branch has not been deployed

No deployments
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.

1 participant