Skip to content

Error responses emit "id":null, which the TS SDK client rejects #6038

Closed

Description

@jhrozek

Discovered while fixing #5950 and confirmed against the official TypeScript SDK working copy. This is not the capitalization bug from #5950 — these envelopes are correctly lowercase. It is the adjacent question of what an absent request id serializes to.

The rule

MCP narrows base JSON-RPC 2.0 here, and the narrowing is deliberate. schema/2025-11-25/schema.ts types the error response as:

export type RequestId = string | number;   // null is NOT in the union

export interface JSONRPCErrorResponse {
  jsonrpc: typeof JSONRPC_VERSION;
  id?: RequestId;                          // optional
  error: Error;
}

id is optional on the error response specifically (it is required on JSONRPCResultResponse), and null is not representable. Both transport pages say the body "has no id" — twice each, once for the 403 Origin-check case specifically. Base JSON-RPC 2.0 §5 says the id "MUST be Null" when it cannot be determined; MCP overrides that, because its schema cannot express null.

So the conformant encoding of an absent id is to omit the key, not to emit "id":null.

Confirmed consequence, not theoretical

From the TS SDK working copy at packages/core/src/schemas.ts:168-187:

export const JSONRPCErrorResponseSchema = z
    .object({
        jsonrpc: z.literal(JSONRPC_VERSION),
        id: RequestIdSchema.optional(),   // RequestIdSchema = z.union([z.string(), z.number().int()])
        error: z.object({ code, message, data })
    })
    .strict();

.optional() admits undefined, not null. So "id":null fails validation and an omitted id passes.

And that schema gates inbound messages using parse(), the throwing variant rather than safeParse:

  • packages/client/src/client/streamableHttp.ts:1055 — unary JSON response
  • packages/client/src/client/streamableHttp.ts:742 — SSE event
  • packages/client/src/client/streamableHttp.ts:1112-1113 — batch
  • packages/client/src/client/sse.ts:261
  • packages/core-internal/src/shared/stdio.ts:57

So one of these responses makes the SDK client throw in its transport layer, before the application ever sees the error. A readable error becomes a protocol crash.

Sites

Three hand-built envelopes, all of which put the raw request id straight into the map so a nil id marshals to null, and all of whose hardcoded fallbacks spell "id":null literally:

  • pkg/transport/session/jsonrpc_errors.go — NotFoundBody, fallback at line 42
  • pkg/ratelimit/middleware.go — rateLimitedBody, fallback at line 180
  • pkg/mcp/classification_response.go — classificationErrorBody, fallback at line 94

The session-not-found one is the most consequential: clients use that code to trigger automatic session recovery, so a client that throws on the envelope loses recovery rather than merely losing an error message.

Fix

Omit the key when the id is nil, rather than emitting null:

env := map[string]any{"jsonrpc": "2.0", "error": errObj}
if requestID != nil {
    env["id"] = requestID
}

Same for the three hardcoded fallback literals. Worth a test row per site asserting the key is absent rather than null — assert.JSONEq catches this, a tagged-struct decode does not.

Note the paths fixed in #5950 do not have this problem: they encode via jsonrpc2.EncodeMessage, whose wireCombined.ID is tagged json:"id,omitempty", and because that field is an interface{} the omitempty check tests IsNil() rather than zero-ness — so an absent id is omitted while a legitimate id: 0 survives.

Activity

  1. added
    bug
    on Jul 27, 2026
  2. added
    needs-triage
    on Jul 27, 2026
  3. removed
    needs-triage
    on Jul 28, 2026
  4. self-assigned this
    on Jul 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions