Repository navigation
fix(server): leave the Connection header to the HTTP server on SSE responses - #3279
Open
bhargavjoshi1237 wants to merge 1 commit into
Open
bhargavjoshi1237 wants to merge 1 commit into
bhargavjoshi1237 wants to merge 1 commit into
Conversation
…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.
🦋 Changeset detectedLatest commit: 62c643c The changes in this PR will be included in the next version bump. This PR includes changesets to release 6 packages
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 |
@modelcontextprotocol/client
@modelcontextprotocol/codemod
@modelcontextprotocol/core
@modelcontextprotocol/server
@modelcontextprotocol/server-legacy
@modelcontextprotocol/express
@modelcontextprotocol/fastify
@modelcontextprotocol/hono
@modelcontextprotocol/node
commit: |
This branch has not been deployed
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.
A client that sends
Connection: closeand gets an SSE response has its next request on that connection rejected with a bodyless, unlogged400 Bad Request. In #2730 this hitsnotifications/initializedright afterinitialize, so the client never reachestools/list.The SSE responses set
Connection: keep-alive. Node uses aConnectionheader set by the application over the client's request, so the socket stays open and the client reuses it. Node's parser has already seenConnection: closeon that socket, so it rejects the new request ("Data afterConnection: close"), and the defaultclientErrorhandler writesHTTP/1.1 400 Bad Request\r\nConnection: close\r\n\r\n. No SDK code runs for that request. A plainnode:httphandler that setsconnection: keep-aliveon aConnection: closerequest 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 SSEperRequestTransport.ts: modern SSE upgradelistenRouter.ts:subscriptions/listenThe other SSE headers, including
X-Accel-Buffering, are unchanged. Node still sends its ownConnection: keep-alive/Keep-Alivewhen the client allows it.How it was tested: the new
packages/middleware/node/test/connectionClose.test.tsdrives a real Node server over a raw socket the way an HTTP/1.1 client does. The first request is sent withConnection: close, and the second reuses the socket unless the first response saidConnection: close. It covers:NodeStreamableHTTPServerTransport:initialize, thennotifications/initialized, expecting 202createMcpHandler+toNodeHandler: a streamedtools/calltwice, expecting 200subscriptions/listentwice, expecting 200Without 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 andcreateMcpHandlersuites 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.tsand reproduces the same 400; I can send a backport after this lands.Fixes #2730