Skip to content

Commit 77273a0

Browse files
committed
fix(client/streamableHttp): handle 404 by clearing session ID per MCP spec
Per MCP spec §Session Management (2025-03-26), when a client receives HTTP 404 in response to a request containing an Mcp-Session-Id, it MUST clear the session ID. Previously, both send() (POST) and _startOrAuthSse() (GET) would throw StreamableHTTPError without clearing _sessionId, permanently breaking the client with stale session IDs. This change: - Adds SdkErrorCode.ClientHttpNotFound - On 404, clears this._sessionId before throwing, so subsequent calls do not keep failing with the same stale session ID - Leaves full re-initialization to the caller (as InitializeRequest involves capability negotiation owned by the Client layer) Fixes modelcontextprotocol#1708.
1 parent 984a27e commit 77273a0

2 files changed

Lines changed: 12 additions & 0 deletions

File tree

‎packages/client/src/client/streamableHttp.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,17 @@ export class StreamableHTTPClientTransport implements Transport {
578578
}
579579
}
580580

581+
// Per MCP spec §Session Management: on 404 with an Mcp-Session-Id
582+
// header, the client MUST clear the session ID and re-initialize.
583+
// Clear the stale session so subsequent calls don't keep failing.
584+
if (response.status === 404) {
585+
this._sessionId = undefined;
586+
throw new SdkError(SdkErrorCode.ClientHttpNotFound, `Session not found (HTTP 404). Clear and re-initialize. ${text}`.trim(), {
587+
status: 404,
588+
text
589+
});
590+
}
591+
581592
throw new SdkError(SdkErrorCode.ClientHttpNotImplemented, `Error POSTing to endpoint: ${text}`, {
582593
status: response.status,
583594
text

‎packages/core/src/errors/sdkErrors.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export enum SdkErrorCode {
3131
ClientHttpNotImplemented = 'CLIENT_HTTP_NOT_IMPLEMENTED',
3232
ClientHttpAuthentication = 'CLIENT_HTTP_AUTHENTICATION',
3333
ClientHttpForbidden = 'CLIENT_HTTP_FORBIDDEN',
34+
ClientHttpNotFound = 'CLIENT_HTTP_NOT_FOUND',
3435
ClientHttpUnexpectedContent = 'CLIENT_HTTP_UNEXPECTED_CONTENT',
3536
ClientHttpFailedToOpenStream = 'CLIENT_HTTP_FAILED_TO_OPEN_STREAM',
3637
ClientHttpFailedToTerminateSession = 'CLIENT_HTTP_FAILED_TO_TERMINATE_SESSION'

0 commit comments

Comments
 (0)