Skip to content

mcp: return errors with omitted IDs for rejected request identities - #1392

Open
skosovsky wants to merge 4 commits into
modelcontextprotocol:mainfrom
skosovsky:fix/invalid-request-id-framing
Open

skosovsky wants to merge 4 commits into
modelcontextprotocol:mainfrom
skosovsky:fix/invalid-request-id-framing

Conversation

@skosovsky

@skosovsky skosovsky commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #1391.

A single request such as {"jsonrpc":"2.0","id":true,"method":"ping"} currently produces HTTP 400 text/plain or a terminal IO read error without a response. Return a JSON-RPC -32600 error with the unknown id omitted; the IO transport then continues to the next message without recursive reads. No rejected request reaches application dispatch.

The decoder marks rejected request IDs separately from response-ID errors. Error serialization omits unknown IDs, as permitted by the MCP 2025-11-25 and 2026-07-28 schemas. DecodeMessage also accepts error-only responses with no id so SDK clients can consume these envelopes: HTTP preserves per-call rejection without closing the session, and IO can read subsequent messages. Explicit null response IDs, success responses without an ID, and unknown-ID responses containing both result and error remain rejected. A presence check is confined to the unknown-ID error path, leaving ordinary message decoding unchanged. Uncorrelated IO responses retain the existing connection behavior; no response is assigned to an arbitrary outstanding call.

Existing batch handling, malformed JSON recovery, and absent/null request-ID behavior remain unchanged. This is independent of #1375/#1253: it handles IDs the decoder already rejects and also composes with their numeric validation. #1209/#1210 address a separate syntax-error/resynchronization path.

Validation (Go 1.27.1, darwin/arm64):

  • Original HTTP and IO rejection regressions fail on unmodified upstream main d1a225e.
  • Earlier null-ID versions of the codec round-trip, IO response, and HTTP session-survival regressions failed on PR head 4779e9b. The updated omitted-ID regressions pass on 6b17534.
  • go test ./... — PASS.
  • go test -race ./... — PASS.
  • go vet ./..., go build ./..., gofmt, and git diff --check — PASS.
  • Coverage includes boolean/object/array rejection, no HTTP tool dispatch, 1000 invalid IO frames followed by a valid request, omitted-ID serialization/decoding, rejection of explicit null response IDs and invalid unknown-ID responses, and request/response error classification.

External conformance scripts were not run locally. CI for 6b17534fdc8f739178cbb908fd1eed1dee36b1d2 passed both server and client conformance, tests on Go 1.25 and 1.26, race tests, lint, and CodeQL.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 12, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-12T15:36:54.858186Z 4779e9b PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 4779e9b02d

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread internal/jsonrpc2/messages.go Outdated
Comment on lines +125 to +126
if !msg.ID.IsValid() && msg.Error != nil {
to.ID = json.RawMessage("null")

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Teach DecodeMessage to accept the emitted null ID

This branch makes unknown-ID errors encode with "id":null, but DecodeMessage cannot consume that wire form: after MakeID(nil), the unchanged if !id.IsValid() branch at internal/jsonrpc2/messages.go:220 returns ErrInvalidRequest even when msg.Error is populated. Consequently the IO reader and the streamable client's non-2xx-body parser treat the new -32600 response as a transport/read failure (potentially tearing down the session) rather than a JSON-RPC error; accept explicit null-ID error responses and cover an encode/decode round trip.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confirmed and fixed in 3e9420c. DecodeMessage now accepts an explicit null-ID error response while still rejecting missing response IDs, null-ID success responses, and null-ID responses with both result and error. Added encode/decode round-trip coverage, an IO response-followed-by-response regression, and an HTTP client regression proving the session survives the rejection and a subsequent call succeeds. All three regressions failed on the previous head; full tests, race for jsonrpc2/mcp, vet and build now pass. The presence check is confined to the unknown-ID error path; ordinary decoding and unmatched-response correlation are unchanged.

{"null", `{"jsonrpc":"2.0","method":"ping","id":null}`, false, false},
} {
t.Run(tt.name, func(t *testing.T) {
// Arrange.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Remove the redundant Arrange/Act/Assert labels

The // Arrange., // Act., and // Assert. labels merely restate the immediately following test structure, and the same labels are repeated throughout both new test files. Remove these phase labels; the root guidance explicitly asks contributors not to add comments unless they are necessary and to prefer self-documenting code.

AGENTS.md reference: AGENTS.md:L45-L47

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 10b0d70: removed the redundant Arrange/Act/Assert phase comments from both new test files. Test structure and behavior are unchanged. The affected tests and git diff --check pass.

}
if !bytes.Equal(bytes.TrimSpace(raw.ID), []byte("null")) {
return nil, ErrInvalidRequest
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would not modify anything here.
MCP Spec & Schema (JSONRPCErrorResponse): Unlike base JSON-RPC 2.0, the MCP specification explicitly forbids null IDs (RequestId is string | integer) and specifies id?: RequestId on JSONRPCErrorResponse (omitting id when it cannot be read, rather than emitting "id": null, which fails MCP JSON Schema validation).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right — I conflated base JSON-RPC with MCP. Fixed in 6b17534.

Unknown-ID errors now omit id. The decoder accepts an error-only response with no id, while still rejecting explicit id: null, success responses without an ID, and mixed result/error responses without an ID. Simply reverting the decoder would have broken round-tripping of the corrected response.

I updated the wire expectations and retained the HTTP session-continuation and IO recovery regressions. Verified against the 2025-11-25 and 2026-07-28 JSON schemas. Local checks passed on Go 1.27.1: focused regressions, go test ./..., go test -race ./..., go vet ./..., go build ./..., gofmt, and git diff --check.

@skosovsky skosovsky changed the title mcp: return null-ID errors for rejected request identities mcp: return errors with omitted IDs for rejected request identities Oct 19, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

mcp: rejected request IDs receive no JSON-RPC error envelope

2 participants