Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 2.1k
fix(client): recover streamable HTTP session on 404 for session-bound requests#1718
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
Closed
Maverick-666
wants to merge
10
commits into
modelcontextprotocol:main
from
Maverick-666:codex/issue-1708-404-session-reset
Uh oh!
There was an error while loading. Please reload this page.
Closed
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
147ca2a
fix(client): clear stale session on HTTP 404 in streamable transport
Maverick-666 c9a7459
chore(changeset): add patch release note for client 404 session recovery
Maverick-666 6078c54
test(client): align 404 session reset assertions with minimal flow
Maverick-666 26f11a3
chore(changeset): align 404 session recovery note with implementation
Maverick-666 0b7f219
fix(client): guard 404 session clear by request session header
Maverick-666 8f88769
Merge main and resolve conflicts with #1655
felixweinberger 7158bd5
fix(client): avoid stale-session 404 race and stop retry loop on sess…
Maverick-666 454ac33
style(client): format streamable HTTP 404 session recovery changes
Maverick-666 962d470
Merge branch 'main' into codex/issue-1708-404-session-reset
Maverick-666 2c432c9
Merge branch 'main' into codex/issue-1708-404-session-reset
Maverick-666 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@modelcontextprotocol/client': patch | ||
| --- | ||
| Clear stale Streamable HTTP client sessions when a session-bound request receives HTTP 404 by clearing the stored session ID, so the next initialize flow can proceed without an MCP session header. |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -25,6 +25,19 @@ const DEFAULT_STREAMABLE_HTTP_RECONNECTION_OPTIONS: StreamableHTTPReconnectionOp | ||
| maxRetries: 2 | ||
| }; | ||
| const SESSION_BOUND_404_ERROR = Symbol('sessionBound404Error'); | ||
| type SessionBound404Error = Error & { [SESSION_BOUND_404_ERROR]?: true }; | ||
| function markSessionBound404Error(error: Error): Error { | ||
| (error as SessionBound404Error)[SESSION_BOUND_404_ERROR] = true; | ||
| return error; | ||
| } | ||
| function isSessionBound404Error(error: unknown): boolean { | ||
| return Boolean(error && typeof error === 'object' && (error as SessionBound404Error)[SESSION_BOUND_404_ERROR] === true); | ||
| } | ||
| /** | ||
| * Options for starting or authenticating an SSE connection | ||
| */ | ||
| @@ -237,6 +250,7 @@ export class StreamableHTTPClientTransport implements Transport { | ||
| // Try to open an initial SSE stream with GET to listen for server messages | ||
| // This is optional according to the spec - server may not support it | ||
| const headers = await this._commonHeaders(); | ||
| const sentSessionId = headers.get('mcp-session-id'); | ||
| const userAccept = headers.get('accept'); | ||
| const types = [...(userAccept?.split(',').map(s => s.trim().toLowerCase()) ?? []), 'text/event-stream']; | ||
| headers.set('accept', [...new Set(types)].join(', ')); | ||
| @@ -254,6 +268,11 @@ export class StreamableHTTPClientTransport implements Transport { | ||
| }); | ||
| if (!response.ok) { | ||
| const shouldClearSessionFor404 = response.status === 404 && sentSessionId !== null && this._sessionId === sentSessionId; | ||
| if (shouldClearSessionFor404) { | ||
| this._sessionId = undefined; | ||
| } | ||
| if (response.status === 401 && this._authProvider) { | ||
| if (response.headers.has('www-authenticate')) { | ||
| const { resourceMetadataUrl, scope } = extractWWWAuthenticateParams(response); | ||
| @@ -288,10 +307,11 @@ export class StreamableHTTPClientTransport implements Transport { | ||
| return; | ||
| } | ||
| throw new SdkError(SdkErrorCode.ClientHttpFailedToOpenStream, `Failed to open SSE stream: ${response.statusText}`, { | ||
| const error = new SdkError(SdkErrorCode.ClientHttpFailedToOpenStream, `Failed to open SSE stream: ${response.statusText}`, { | ||
| status: response.status, | ||
| statusText: response.statusText | ||
| }); | ||
| throw shouldClearSessionFor404 ? markSessionBound404Error(error) : error; | ||
| } | ||
| this._handleSseStream(response.body, options, true); | ||
| @@ -345,7 +365,11 @@ export class StreamableHTTPClientTransport implements Transport { | ||
| this._cancelReconnection = undefined; | ||
| if (this._abortController?.signal.aborted) return; | ||
| this._startOrAuthSse(options).catch(error => { | ||
| this.onerror?.(new Error(`Failed to reconnect SSE stream: ${error instanceof Error ? error.message : String(error)}`)); | ||
| const reconnectError = error instanceof Error ? error : new Error(String(error)); | ||
| this.onerror?.(new Error(`Failed to reconnect SSE stream: ${reconnectError.message}`)); | ||
| if (isSessionBound404Error(reconnectError)) { | ||
| return; | ||
| } | ||
| try { | ||
| this._scheduleReconnection(options, attemptCount + 1); | ||
| } catch (scheduleError) { | ||
| @@ -539,6 +563,7 @@ export class StreamableHTTPClientTransport implements Transport { | ||
| } | ||
| const headers = await this._commonHeaders(); | ||
| const sentSessionId = headers.get('mcp-session-id'); | ||
| headers.set('content-type', 'application/json'); | ||
| const userAccept = headers.get('accept'); | ||
| const types = [...(userAccept?.split(',').map(s => s.trim().toLowerCase()) ?? []), 'application/json', 'text/event-stream']; | ||
| @@ -561,6 +586,10 @@ export class StreamableHTTPClientTransport implements Transport { | ||
| } | ||
| if (!response.ok) { | ||
| if (response.status === 404 && sentSessionId !== null && this._sessionId === sentSessionId) { | ||
| this._sessionId = undefined; | ||
| } | ||
Maverick-666 marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| if (response.status === 401 && this._authProvider) { | ||
| // Store WWW-Authenticate params for interactive finishAuth() path | ||
| if (response.headers.has('www-authenticate')) { | ||
Maverick-666 marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
Uh oh!
There was an error while loading. Please reload this page.