Skip to content

Commit 976972b

Browse files
evan-choiclaude
andcommitted
fix: handle null body status in createCustomFetch proxy
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 (101, 204, 205, 304) and return a proper Web Response with null body before the SSE stream conversion check. Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
1 parent dd53302 commit 976972b

1 file changed

Lines changed: 23 additions & 0 deletions

File tree

‎server/src/index.ts‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,29 @@ const createCustomFetch = (headerHolder: { headers: HeadersInit }) => {
338338
const acceptHeader = finalHeaders.get("Accept");
339339
const isSSE = acceptHeader?.includes("text/event-stream");
340340

341+
// Null body statuses (101, 204, 205, 304) must not have a body per
342+
// the Fetch spec. node-fetch may still provide a non-null body stream,
343+
// but the Web Response constructor rejects a body for these statuses.
344+
// Return a proper Web Response with null body so the SDK's
345+
// response.body?.cancel() call works correctly.
346+
const isNullBodyStatus =
347+
response.status === 101 ||
348+
response.status === 204 ||
349+
response.status === 205 ||
350+
response.status === 304;
351+
352+
if (isNullBodyStatus) {
353+
const responseHeaders: Record<string, string> = {};
354+
response.headers.forEach((value: string, key: string) => {
355+
responseHeaders[key] = value;
356+
});
357+
return new Response(null, {
358+
status: response.status,
359+
statusText: response.statusText,
360+
headers: responseHeaders,
361+
});
362+
}
363+
341364
if (isSSE && response.body) {
342365
// For SSE requests, we need to convert the Node.js stream to a web ReadableStream
343366
// because the EventSource polyfill expects web-compatible streams

0 commit comments

Comments
 (0)