fix: handle null body status in createCustomFetch proxy#1180
Open
evan-choi wants to merge 1 commit intomodelcontextprotocol:mainfrom
Open
fix: handle null body status in createCustomFetch proxy#1180evan-choi wants to merge 1 commit intomodelcontextprotocol:mainfrom
evan-choi wants to merge 1 commit intomodelcontextprotocol:mainfrom
Conversation
When an MCP server returns HTTP 204 No Content (e.g., on DELETE session termination), the proxy's createCustomFetch crashes with: TypeError: Response constructor: Invalid response status code 204 This happens because node-fetch returns a non-null body stream even for 204 responses, and the Web Response constructor rejects a body for null body statuses per the Fetch spec. Additionally, returning the raw node-fetch response (path modelcontextprotocol#3) causes `response.body?.cancel is not a function` because node-fetch uses Node.js Readable streams, not Web ReadableStreams. Fix: detect null body statuses (204, 205, 304) and return a proper Web Response with null body before the SSE stream conversion check.
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
TypeError: Response constructor: Invalid response status code 204when disconnecting from MCP servers that return HTTP 204 No Content on DELETE session terminationProblem
createCustomFetchwrapsnode-fetchresponses for the SDK, but fails on null body statuses (204, 205, 304):headerHolderretainsAccept: text/event-streamfrom prior GET/POST requests. When DELETE fires, the stale header makesisSSE = true, andnew Response(webStream, {status: 204})throws because the Web Response constructor rejects a body for null body statuses.node-fetchresponse is returned viaas unknown as Response. The SDK then callsresponse.body?.cancel()which doesn't exist on Node.js Readable streams (node-fetch), causingTypeError: response.body?.cancel is not a function.Fix
Detect null body statuses before the SSE stream conversion check and return a proper Web
Responsewithnullbody:Known limitations / Follow-up
Acceptheader leaking fromheaderHolderinto unrelated requests (e.g., DELETE) is a separate root cause that could be addressed in a follow-up PR by scoping header merging per request type.return response as unknown as Response) is a pre-existing issue where non-SSE, non-null-body responses are not properly converted from node-fetch to Web Response. This could cause silent failures forresponse.body?.cancel()on other status codes.Test plan
npm run prettier-checkpassesnpm testpasses (487 tests)