-
Notifications
You must be signed in to change notification settings - Fork 1.8k
[1.x] fix(client): retry SSE stream after receiving session ID #1520
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
LucaButBoring
wants to merge
9
commits into
modelcontextprotocol:v1.x
Choose a base branch
from
LucaButBoring:fix/roots-list-1.x
base: v1.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+419
−0
Open
Changes from 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
328becc
test(server): add roots/list test coverage
LucaButBoring c65e3d1
fix(client): retry SSE stream after receiving session ID
LucaButBoring c54826c
Merge branch 'v1.x' into fix/roots-list-1.x
LucaButBoring f0bd954
Merge branch 'v1.x' into fix/roots-list-1.x
LucaButBoring c7b18a0
Merge branch 'v1.x' into fix/roots-list-1.x
KKonstantinov d0eeff6
fix(client): await SSE stream open in connect() to prevent race condi…
LucaButBoring 6fa4688
Merge branch 'v1.x' into fix/roots-list-1.x
LucaButBoring 8719716
Revert "fix(client): await SSE stream open in connect() to prevent ra…
LucaButBoring 5a0842e
Merge branch 'v1.x' into fix/roots-list-1.x
LucaButBoring File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔴 Bug: During normal MCP initialization, two concurrent GET SSE streams are opened. The new session-ID path (line 493) fires
_startOrAuthSsewhen theinitializeresponse returns a session ID, and then the existingisInitializedNotificationpath (line 574) fires_startOrAuthSseagain unconditionally when theinitializednotification gets a 202 response -- it does not check_sseStreamOpened. The server rejects the duplicate with 409 Conflict, causing a spurious error surfaced viaonerroron every stateful initialization. Fix: add&& !this._sseStreamOpenedto the guard at line 574.Extended reasoning...
What the bug is
This PR adds a new code path at line 493 that opens an SSE stream via
_startOrAuthSse()when a session ID is received for the first time duringsend(). However, there is already an existing code path at line 574 that opens an SSE stream whenisInitializedNotification(message)is true and the server responds with 202. During normal MCP initialization, both paths fire, creating two concurrent GET SSE connections.The specific triggering flow
Step-by-step proof with the standard MCP handshake:
initializePOST -- Server responds with 200 +mcp-session-idheader.hadSessionIdisfalse(no prior session). At line 488,this._sessionIdis set.sessionId && !hadSessionId && !this._sseStreamOpenedistrue, so_startOrAuthSse()fires (fire-and-forget). This opens SSE stream Better validation of data coming over the wire #1.initialize), so the GET SSE succeeds, and_sseStreamOpenedis set totrueat line 244.initializednotification POST -- Server responds with 202.isInitializedNotification(message)returnstrue. This path calls_startOrAuthSse()without checking_sseStreamOpened. This opens SSE stream Race condition between transport start and server connection #2.Why existing code does not prevent it
The
_sseStreamOpenedflag is only checked at line 493 (the new path) and set at line 244. TheisInitializedNotificationpath at line 574 never reads this flag, so it unconditionally opens a second stream even after the first one succeeded. The_startOrAuthSsemethod itself has no internal guard against being called multiple times.Impact
The server explicitly rejects duplicate GET SSE streams with 409 Conflict (
webStandardStreamableHttp.tslines 410-414: "Only one SSE stream is allowed per session"). This means:StreamableHTTPError(409)is thrown and surfaced viaonerror, creating a spurious error on every normal initHow to fix
Add
&& !this._sseStreamOpenedto the guard at line 574:Alternatively, skip the new session-ID path (line 493) when the message being sent is the
initializerequest, since theinitializednotification path will handle opening the stream.