Skip to content

Commit 09745e3

Browse files
committed
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 (204, 205, 304) and return a proper Web Response with null body before the SSE stream conversion check.
1 parent dd53302 commit 09745e3

1 file changed

Lines changed: 22 additions & 0 deletions

File tree

‎server/src/index.ts‎

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

341+
// Null body statuses (204, 205, 304) must not have a body per the
342+
// 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 === 204 ||
348+
response.status === 205 ||
349+
response.status === 304;
350+
351+
if (isNullBodyStatus) {
352+
const responseHeaders: Record<string, string> = {};
353+
response.headers.forEach((value: string, key: string) => {
354+
responseHeaders[key] = value;
355+
});
356+
return new Response(null, {
357+
status: response.status,
358+
statusText: response.statusText,
359+
headers: responseHeaders,
360+
});
361+
}
362+
341363
if (isSSE && response.body) {
342364
// For SSE requests, we need to convert the Node.js stream to a web ReadableStream
343365
// because the EventSource polyfill expects web-compatible streams

0 commit comments

Comments
 (0)