Skip to content

fix(server): prime the standalone GET stream - #3212

Open
kai0310 wants to merge 2 commits into
modelcontextprotocol:mainfrom
kai0310:fix/standalone-get-priming-event
Open

kai0310 wants to merge 2 commits into
modelcontextprotocol:mainfrom
kai0310:fix/standalone-get-priming-event

Conversation

@kai0310

@kai0310 kai0310 commented Nov 19, 2026 •

Copy link
Copy Markdown

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 without Last-Event-ID and 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 eventStore is configured and the request's MCP-Protocol-Version is 2025-11-25 or later.

Details:

  • The stream is registered before the priming event is stored, and 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.
  • If the store throws, the stream opens without a priming event, as it did before. If the client disconnects or 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.
  • Every GET connection shares the _GET_stream ID, so a session that opens GET more than once has several stored priming events ({}). replayEvents() now skips them instead of writing data: {}.

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?

  • New tests in packages/server/test/server/streamableHttp.test.ts: priming with retry and resume after closeStandaloneSSEStream(), no priming for 2025-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, and close() doesn't leave send() waiting.
  • New e2e requirement hosting:resume:priming-get: the GET stream is primed, closed by a tool, and a notification sent while disconnected arrives on resume.
  • Existing tests that read the first chunk of a GET stream as a message now read the priming event first (middleware/node, e2e close-stream).
  • With the source change reverted, the new unit and e2e tests fail. With the send() wait removed, the concurrent-notification test fails. The tests for the second commit fail against the first commit.
  • With StreamableHTTPClientTransport against an in-process server: on main the reconnect GET has no Last-Event-ID and 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 test and pnpm test:conformance:server (73/73) pass. protocol:timeout:max-total [sse] in e2e fails intermittently on main as well.

Breaking Changes

None in the API. Behavior changes, also listed in the changeset:

  • GET SSE streams start with a priming frame when the conditions above hold.
  • Opening the GET stream waits for eventStore.storeEvent(); if it throws, the stream opens without a priming event.
  • Each GET stream stores one {} event under the standalone stream ID.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Checklist

  • I have read the MCP Documentation
  • My code follows the repository style guidelines
  • New and existing tests pass locally
  • I have added tests that prove my fix is effective
  • I have added a changeset

Additional context

The Python SDK primes resumed GET streams but not fresh ones, so it has the same gap on the first connection.

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
@kai0310
kai0310 requested a review from a team as a code owner November 19, 2026 18:25
@changeset-bot

changeset-bot Bot commented Nov 19, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 2d2c5b5

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 Nov 19, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

@modelcontextprotocol/client

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

@modelcontextprotocol/codemod

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

@modelcontextprotocol/core

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

@modelcontextprotocol/server

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

@modelcontextprotocol/server-legacy

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

@modelcontextprotocol/express

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

@modelcontextprotocol/fastify

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

@modelcontextprotocol/hono

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

@modelcontextprotocol/node

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

commit: 6fa9909

…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

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.

[v2] Standalone GET stream is never primed, so notifications sent across closeStandaloneSSEStream() are lost

1 participant