Skip to content

send() should use StreamableHTTPError instead of plain Error for HTTP errors #1176

@yamadashy

Description

@yamadashy

Problem

In src/client/streamableHttp.ts#L532, the send() method throws a plain Error for non-OK HTTP responses when no authProvider is configured:

if (response.status === 401 && this._authProvider) {
    // auth handling...
}
// Falls through to:
throw new Error(`Error POSTing to endpoint (HTTP ${response.status}): ${text}`);

When authProvider is not set (common in many use cases), all HTTP errors including 401/403 result in a plain Error. This makes it impossible to programmatically access the HTTP status code, as the error only has stack and message properties.

Impact

Per MCP spec, clients should fallback to SSE transport for status codes 400, 404, and 405. However, without access to the status code, clients cannot distinguish between these fallback-worthy errors and others (like 401 Unauthorized).

Suggested Fix

Use StreamableHTTPError (already defined in this file) instead:

throw new StreamableHTTPError(response.status, `Error POSTing to endpoint: ${text}`);

This allows clients to access the status code via error.code.

Related Issues

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions