Skip to content

fix(server): leave the Connection header to the HTTP server on SSE responses - #3279

Open
bhargavjoshi1237 wants to merge 1 commit into
modelcontextprotocol:mainfrom
bhargavjoshi1237:fix/notification-connection-close
Open

bhargavjoshi1237 wants to merge 1 commit into
modelcontextprotocol:mainfrom
bhargavjoshi1237:fix/notification-connection-close

Conversation

@bhargavjoshi1237

Copy link
Copy Markdown

A client that sends Connection: close and gets an SSE response has its next request on that connection rejected with a bodyless, unlogged 400 Bad Request. In #2730 this hits notifications/initialized right after initialize, so the client never reaches tools/list.

The SSE responses set Connection: keep-alive. Node uses a Connection header set by the application over the client's request, so the socket stays open and the client reuses it. Node's parser has already seen Connection: close on that socket, so it rejects the new request ("Data after Connection: close"), and the default clientError handler writes HTTP/1.1 400 Bad Request\r\nConnection: close\r\n\r\n. No SDK code runs for that request. A plain node:http handler that sets connection: keep-alive on a Connection: close request does the same.

What changed: Connection: 'keep-alive' is removed from all five SSE responses, so the HTTP server decides:

  • streamableHttp.ts: standalone GET, Last-Event-ID replay, POST SSE
  • perRequestTransport.ts: modern SSE upgrade
  • listenRouter.ts: subscriptions/listen

The other SSE headers, including X-Accel-Buffering, are unchanged. Node still sends its own Connection: keep-alive / Keep-Alive when the client allows it.

How it was tested: the new packages/middleware/node/test/connectionClose.test.ts drives a real Node server over a raw socket the way an HTTP/1.1 client does. The first request is sent with Connection: close, and the second reuses the socket unless the first response said Connection: close. It covers:

  • NodeStreamableHTTPServerTransport: initialize, then notifications/initialized, expecting 202
  • createMcpHandler + toNodeHandler: a streamed tools/call twice, expecting 200
  • subscriptions/listen twice, expecting 200

Without the source change all 3 fail with HTTP/1.1 400 Bad Request; with it all 3 pass. Putting back one site at a time fails only that site's test. The node middleware suite passes 108/108, the server's streamable HTTP, per-request and createMcpHandler suites pass 185/185, and typecheck and lint are clean.

#2554 removed the same header from the three legacy-transport sites in August; it no longer applies cleanly and predates perRequestTransport/listenRouter. Happy to close this in favour of it if you'd rather revive that one.

v1.x has the same three lines in src/server/webStandardStreamableHttp.ts and reproduces the same 400; I can send a backport after this lands.

Fixes #2730

…sponses

SSE responses set Connection: keep-alive, which overrode a client's
Connection: close and kept the socket open. The client's next request on
it was rejected by Node's HTTP parser with a bare 400 Bad Request.
Drop the header at all five SSE sites so the HTTP server decides.
@bhargavjoshi1237
bhargavjoshi1237 requested a review from a team as a code owner December 5, 2026 19:06
@changeset-bot

changeset-bot Bot commented Dec 5, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 62c643c

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 6 packages
Name Type
@modelcontextprotocol/server Patch
@modelcontextprotocol/client Patch
@modelcontextprotocol/codemod Patch
@modelcontextprotocol/core Patch
@modelcontextprotocol/server-legacy Patch
@modelcontextprotocol/core-internal Patch

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

@pkg-pr-new

pkg-pr-new Bot commented Dec 5, 2026

Copy link
Copy Markdown

Open in StackBlitz

@modelcontextprotocol/client

npm i https://pkg.pr.new/@modelcontextprotocol/client@3279

@modelcontextprotocol/codemod

npm i https://pkg.pr.new/@modelcontextprotocol/codemod@3279

@modelcontextprotocol/core

npm i https://pkg.pr.new/@modelcontextprotocol/core@3279

@modelcontextprotocol/server

npm i https://pkg.pr.new/@modelcontextprotocol/server@3279

@modelcontextprotocol/server-legacy

npm i https://pkg.pr.new/@modelcontextprotocol/server-legacy@3279

@modelcontextprotocol/express

npm i https://pkg.pr.new/@modelcontextprotocol/express@3279

@modelcontextprotocol/fastify

npm i https://pkg.pr.new/@modelcontextprotocol/fastify@3279

@modelcontextprotocol/hono

npm i https://pkg.pr.new/@modelcontextprotocol/hono@3279

@modelcontextprotocol/node

npm i https://pkg.pr.new/@modelcontextprotocol/node@3279

commit: 62c643c

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

StreamableHTTP server transport returns bare 400 for a notification when the request has Connection: close

1 participant