Repository navigation
fix(server): read request bodies where Request.body is not implemented - #3028
littleKitchen wants to merge 1 commit into
Conversation
Firefox does not implement `Request.body` yet, so `readRequestBody` threw on `request.body.getReader()` and every POST to a browser-hosted server was answered with a JSON-RPC parse error. When `request.body` is undefined, read the body with `arrayBuffer()` and check its size against `maxBytes` afterwards, so oversized bodies still get `413`. The streaming path is unchanged for runtimes that expose the stream. Fixes modelcontextprotocol#3025 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
🦋 Changeset detectedLatest commit: 49cb43e The changes in this PR will be included in the next version bump. This PR includes changesets to release 6 packages
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/codemod
@modelcontextprotocol/core
@modelcontextprotocol/server
@modelcontextprotocol/server-legacy
@modelcontextprotocol/express
@modelcontextprotocol/fastify
@modelcontextprotocol/hono
@modelcontextprotocol/node
commit: |
koriyoshi2041
left a comment
There was a problem hiding this comment.
Reviewed exact head 49cb43e. The fallback keeps the limit byte-based: an exact 4-byte body is accepted, 5 bytes is rejected, and a 4-byte multibyte UTF-8 body decodes intact when Request.body is unavailable. A consumed fallback request still rejects on a second read, matching normal Fetch body semantics.
I also ran the server package suite (578/578), server typecheck, Prettier on the changed files, and git diff --check; all passed. The Firefox fallback looks sound to me.
|
Verified the fix locally: reverting |
Fixes #3025
Problem
Firefox does not implement
Request.body(it isundefined), soreadRequestBodythrows onrequest.body.getReader().Every POST to a browser-hosted server is then answered
400with a JSON-RPC parse error:createMcpHandleranswersParse error: the request body could not be read, andWebStandardStreamableHTTPServerTransportanswersParse error: Invalid JSON.Before #2698 these paths read the body with
request.text()andreq.json(), which work in Firefox.Change
When
request.bodyisundefined, read the body witharrayBuffer()and compare its byte length withmaxBytes, so an oversized body still gets413.With no stream there is nothing to stop early, so in this fallback the limit is checked after the body is read; the
Content-Lengthpre-check still runs first.Runtimes that expose the stream keep the streaming reader unchanged. No new API.
One test in
createMcpHandler.test.tshidesRequest.bodythe way Firefox does, then checks that a moderntools/callis served (200) and that an oversized body gets413. Includes a patch changeset for@modelcontextprotocol/server.Validation
main(400) and passes with the fix.pnpm build:all,pnpm typecheck:allandpnpm lint:allpass.pnpm test:allpasses in every package excepttest/e2e, whereprotocol:timeout:max-total [sse …]fails intermittently on my machine (expected 0 to be greater than or equal to 3on the tick count). It fails the same way on unchangedmain: 3 runs each with and without this change, 1–2 failures per run.AI disclosure: this PR was written primarily by Claude Code (the fix, the test, the changeset and this description), under my direction.
🤖 Generated with Claude Code