Repository navigation
Conversation
POST SSE streams start with a priming event, but the standalone GET stream did not. A client whose GET stream was closed with closeStandaloneSSEStream() before it had received an event reconnected without Last-Event-ID, and notifications sent in between were stored but never delivered. The stream is registered before the priming event is stored, and send() waits for the priming event to be written, so a concurrent notification goes out on the new stream right after it. Replays skip stored priming events, since every GET connection shares the standalone stream ID. Fixes modelcontextprotocol#3211
🦋 Changeset detectedLatest commit: 2d2c5b5 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: |
…ored Storing the priming event opened a window the GET handler didn't cover: - a client that disconnected during the store left the stream registered, so every later GET got 409 - closeStandaloneSSEStream() during the store made the GET answer 404, which tells a compliant client to drop a live session - a failing store turned the GET into a 500, where before the stream opened without touching the store - a store call that never settled blocked send() even after close() Now a failed store opens the stream without a priming event, a closed or aborted stream comes back as an ended 200 response, and the primed promise also settles on cleanup. send() re-checks it in a loop, so a stream reopened during the wait is waited on too.
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.
Motivation and Context
Fixes #3211.
POST SSE streams open with a priming event, but the standalone GET stream doesn't. Until the server sends something on it, the client has no event ID, so after
closeStandaloneSSEStream()it reconnects withoutLast-Event-IDand loses whatever was sent while it was disconnected. The spec asks GET streams to follow the same polling behavior as POST when the server closes the connection (transports, listening for messages).This change primes the GET stream under the same conditions as POST: an
eventStoreis configured and the request'sMCP-Protocol-Versionis2025-11-25or later.Details:
send()waits for the priming event to be written. A notification sent during that store call goes out on the new stream right after the priming event instead of only landing in the store. Registering first also keeps the existing 409 check synchronous.closeStandaloneSSEStream()is called while the priming event is being stored, the stream is unregistered and the GET gets an ended 200 response rather than leaving a stale registration (later GETs would get 409) or answering 404 for a live session._GET_streamID, so a session that opens GET more than once has several stored priming events ({}).replayEvents()now skips them instead of writingdata: {}.Resumed streams are not primed here. A resumed stream that is closed again before it delivers an event has the same problem, but that is the case #2499 covers, and priming before the replay can move the client's cursor past events that haven't been delivered yet, so I left it out.
How Has This Been Tested?
packages/server/test/server/streamableHttp.test.ts: priming withretryand resume aftercloseStandaloneSSEStream(), no priming for2025-06-18, replay skips priming events, a notification sent during the priming store is written right after it, a failing store still opens the stream, a concurrent second GET still gets 409, and while the priming event is being stored: a client disconnect frees the stream for the next GET,closeStandaloneSSEStream()ends it with 200 instead of 404, andclose()doesn't leavesend()waiting.hosting:resume:priming-get: the GET stream is primed, closed by a tool, and a notification sent while disconnected arrives on resume.middleware/node, e2eclose-stream).send()wait removed, the concurrent-notification test fails. The tests for the second commit fail against the first commit.StreamableHTTPClientTransportagainst an in-process server: onmainthe reconnect GET has noLast-Event-IDand the notification sent during the gap never arrives; on this branch it reconnects with the priming ID and receives it.pnpm typecheck:all,pnpm lint:all,pnpm -r testandpnpm test:conformance:server(73/73) pass.protocol:timeout:max-total [sse]in e2e fails intermittently onmainas well.Breaking Changes
None in the API. Behavior changes, also listed in the changeset:
eventStore.storeEvent(); if it throws, the stream opens without a priming event.{}event under the standalone stream ID.Types of changes
Checklist
Additional context
The Python SDK primes resumed GET streams but not fresh ones, so it has the same gap on the first connection.