-
Notifications
You must be signed in to change notification settings - Fork 2.1k
fix(server): read HTTP request bodies with a size limit and bound JSON-RPC batch length #2698
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
maxisbey
wants to merge
3
commits into
main
Choose a base branch
from
streamable-http-body-limits
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| --- | ||
| '@modelcontextprotocol/server': patch | ||
| '@modelcontextprotocol/node': patch | ||
| '@modelcontextprotocol/hono': patch | ||
| '@modelcontextprotocol/express': patch | ||
| --- | ||
|
|
||
| Read Streamable HTTP request bodies with a size limit. Every SDK-owned body read — | ||
| `WebStandardStreamableHTTPServerTransport` (and the Node transport built on it), | ||
| `createMcpHandler`, `toNodeHandler`, and `createMcpHonoApp`'s JSON pre-parse — now stops at | ||
| 4 MiB by default (the limit the legacy SSE transport already uses; the Express adapter and stdio | ||
| bound their reads too) and answers `413 Payload Too Large` before anything is parsed. | ||
| `toWebRequest` (when it reads the Node stream itself) now rejects once the body exceeds the | ||
| limit with an error whose `name` is `'RequestBodyTooLargeError'` and `status` is `413`, and | ||
| `toNodeHandler` answers that with `413`; hand-wired callers of `toWebRequest` should handle the | ||
| rejection or pass a pre-parsed body, and `isLegacyRequest` reports such a request as non-legacy | ||
| so the modern handler answers it. JSON-RPC batch arrays are limited to 100 messages; a longer | ||
| batch is answered `400` / `-32600` and none of it is dispatched. | ||
|
|
||
| The limit is configurable with a new `maxRequestBodySize` option (bytes, default | ||
| `DEFAULT_MAX_REQUEST_BODY_SIZE` = 4 MiB, exported from `@modelcontextprotocol/server`) on | ||
| `WebStandardStreamableHTTPServerTransportOptions`, `CreateMcpHandlerOptions` (forwarded to its | ||
| stateless legacy leg; `isLegacyRequest` and `legacyStatelessFallback` take the same option), | ||
| `CreateMcpHonoAppOptions`, and `ToNodeHandlerOptions` / `ToWebRequestOptions` (the adapter's | ||
| bound applies before the handler's, so raise both). The bounded reader is exported as | ||
| `readRequestBody` for adapter authors. Hosts that pre-parse the body and pass it as | ||
| `parsedBody` skip the SDK's read and its size limit entirely; the batch bound applies either way. | ||
|
|
||
| `createMcpHonoApp` and `createMcpExpressApp` now run their Host/Origin validation before the | ||
| JSON body parser, so a request from a disallowed Host or Origin with an invalid JSON body is | ||
| answered `403` rather than `400`, and its body is not read. | ||
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
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
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟡 nit: new user-facing behavior (default 4 MiB body cap answering 413, 100-message batch cap answering 400) and the new maxRequestBodySize option on four public surfaces are documented only in this changeset and JSDoc — no docs/ guide page is updated. REVIEW.md's checklist explicitly requires prose documentation (not just JSDoc) for new features and a check that existing docs don't omit new behavior; the serving guides are exactly where the sibling knobs already live: docs/serving/legacy-clients.md:96 tells users to raise jsonLimit to '4mb' because "the SSE transport itself accepts messages up to 4mb", and docs/serving/express.md… [also at: packages/server/src/index.ts:78 - nit: new public feature ships with zero prose documentation under docs/ — the
maxRequestBodySizeoption (added to…]Extended reasoning...
An operator whose MCP server accepts large tool arguments (e.g. base64 images) follows docs/serving/express.md or docs/serving/http.md to deploy after upgrading; clients start receiving 413 for >4 MiB POSTs. The guides they were told to follow never mention the limit, the maxRequestBodySize option, or that on the toNodeHandler path both the adapter's and the handler's bound must be raised in step, so they either misdiagnose the 413 as a proxy problem or raise only CreateMcpHandlerOptions.maxRequestBodySize and still get 413 from the adapter's default bound. The only discoverable documentation is a CHANGELOG entry and per-symbol JSDoc.
Verification: nit. The omission is real. The diff stat shows no
docs/**file is touched (13 changed files, all under.changeset/andpackages/), yet the change introduces user-facing behavior and a new public option on four surfaces:.changeset/request-body-size-limit.mdstates "now stops at 4 MiB by default ... and answers413 Payload Too Large", "JSON-RPC batch arrays are limited to 100 messages;