fix: treat parsedBody = null as no pre-parsed body (v1.x)#2204
Open
aicayzer wants to merge 1 commit into
Open
Conversation
Some body parsers — notably the synthetic objects produced by serverless-express on AWS Lambda Function URLs — set req.body = null after draining the request stream. The existing guard only checked for undefined, so null fell through to JSONRPCMessageSchema.parse and broke every Lambda-hosted MCP server. Treat null and undefined alike and fall through to req.json(). Fixes modelcontextprotocol#1417.
|
commit: |
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
WebStandardStreamableHTTPServerTransport.handleRequestto treat aparsedBodyvalue ofnullthe same asundefinedserverless-expressnullas theparsedBodyargumentProblem
After the fetch-transport refactor in #1209,
handleRequestguards onoptions?.parsedBody !== undefinedbefore falling through toreq.json(). Some body parsers — notably the syntheticreq/resobjects produced by@codegenie/serverless-expresson AWS Lambda Function URLs — drain the request stream and setreq.body = nullrather than leaving itundefined. With the existing guard, thatnullis then handed toJSONRPCMessageSchema.parse, which rejects with a Zodinvalid_unionerror and the request fails asParse error: Invalid JSON-RPC message.This affects every MCP server using the SDK's recommended Lambda deployment pattern (
StreamableHTTPServerTransport+serverless-express).Fixes #1417.
Fix
webStandardStreamableHttp.ts— extend the guard to covernullso the transport falls through to reading the body itself when no usable pre-parsed body is supplied.Test plan
StreamableHTTPServerTransport with parsedBody = null (Lambda regression #1417)that callstransport.handleRequest(req, res, null)and verifies an initialize request succeedsnpm run lintandnpm run typecheckclean