From 01e62f387d638c7ac076136409074f23ed64a2d5 Mon Sep 17 00:00:00 2001 From: Kazuki Yamada Date: Thu, 27 Nov 2025 11:11:48 +0900 Subject: [PATCH] fix(client): use StreamableHTTPError instead of plain Error in send() Use StreamableHTTPError instead of plain Error when throwing HTTP errors in the send() method. This allows clients to programmatically access the HTTP status code via error.code, which is required for implementing proper SSE fallback behavior per MCP spec (only fallback for 400/404/405). Fixes #1176 --- src/client/streamableHttp.test.ts | 2 +- src/client/streamableHttp.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/client/streamableHttp.test.ts b/src/client/streamableHttp.test.ts index 7c6895416..91223c48d 100644 --- a/src/client/streamableHttp.test.ts +++ b/src/client/streamableHttp.test.ts @@ -209,7 +209,7 @@ describe('StreamableHTTPClientTransport', () => { const errorSpy = vi.fn(); transport.onerror = errorSpy; - await expect(transport.send(message)).rejects.toThrow('Error POSTing to endpoint (HTTP 404)'); + await expect(transport.send(message)).rejects.toThrow('Streamable HTTP error: Error POSTing to endpoint: Session not found'); expect(errorSpy).toHaveBeenCalled(); }); diff --git a/src/client/streamableHttp.ts b/src/client/streamableHttp.ts index aa52ec732..39a0640f4 100644 --- a/src/client/streamableHttp.ts +++ b/src/client/streamableHttp.ts @@ -529,7 +529,7 @@ export class StreamableHTTPClientTransport implements Transport { } } - throw new Error(`Error POSTing to endpoint (HTTP ${response.status}): ${text}`); + throw new StreamableHTTPError(response.status, `Error POSTing to endpoint: ${text}`); } // Reset auth loop flag on successful response