Skip to content

[Schema] Omit the id of an error response that never had one - #442

Merged
chr-hertel merged 2 commits into
modelcontextprotocol:mainfrom
chr-hertel:pr/error-null-id
Aug 18, 2026
Merged

[Schema] Omit the id of an error response that never had one#442
chr-hertel merged 2 commits into
modelcontextprotocol:mainfrom
chr-hertel:pr/error-null-id

Conversation

@chr-hertel

Copy link
Copy Markdown
Member

An error response whose id could not be read sent "id": "", which claims the peer issued a request with an empty-string id — a different statement from "the id could not be read". JSON-RPC spells the latter as a null id, and a receiver that cannot read the id off a malformed request is exactly the case the code covers.

[BC Break] Mcp\Schema\JsonRpc\Error accepts null as its $id, and getId() may return it:

  • all the for*() factories default to null instead of ''
  • fromArray() accepts a missing or explicitly-null id
  • MessageFactory decodes both as an id-less error rather than rejecting them
  • the id member is omitted from the serialized form when absent

Closes #333 and the id half of #381.

An error response whose id could not be read sent `"id": ""`, which claims
the peer issued a request with an empty-string id - a different statement
from "the id could not be read". JSON-RPC spells the latter as a null id,
and a receiver that cannot read the id is exactly the case the code covers.

[BC Break] Error::$id accepts null and getId() may return it. All the for*()
factories default to null, fromArray() accepts a missing or explicitly-null
id, and the member is omitted from the serialized form when absent.

Copilot AI left a comment

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.

Pull request overview

This PR updates the JSON-RPC error schema so that an “unknown/unreadable request id” is represented as an absent/null id (instead of the fabricated empty string ""), and ensures the serialized error omits the id member when it’s unknown. This aligns error behavior with JSON-RPC conventions and the MCP spec’s allowance for id-less error responses when correlation is impossible.

Changes:

  • Allow Mcp\Schema\JsonRpc\Error to carry a null id, accept missing/null ids in fromArray(), and default all for*() factories to null.
  • Omit id during Error serialization when the id is unknown.
  • Update MessageFactoryTest expectations for missing/null error ids and document the BC break in CHANGELOG.md.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
tests/Unit/JsonRpc/MessageFactoryTest.php Updates parsing expectations so id-less / id:null error responses decode to Error with null id.
src/Schema/JsonRpc/Error.php Changes the Error schema to support null ids and omits id from JSON output when unknown.
CHANGELOG.md Documents the BC break and the behavioral changes around error ids.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +172 to 175
public function getId(): string|int|null
{
return $this->id;
}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed in e3ff482 — both Client and Server Protocol now guard the null-id case instead of passing it into storeResponse()/the session key.

Comment on lines 179 to 183
* jsonrpc: string,
* id: string|int,
* id?: string|int,
* error: array{
* code: int,
* message: string,

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed in e3ff482 — moved data back inside error in the phpdoc shape.

Also fix Error::jsonSerialize()'s phpdoc: data belongs inside error, not beside it.
@chr-hertel
chr-hertel merged commit 5041caf into modelcontextprotocol:main Aug 18, 2026
23 checks passed
@chr-hertel
chr-hertel deleted the pr/error-null-id branch August 18, 2026 13:24
@chr-hertel chr-hertel added this to the 0.8.0 milestone Aug 18, 2026
@chr-hertel chr-hertel added 2026-07-28 All issues and PRs related to the spec release 2026-07-28 Schema Issues & PRs related to the Schema component labels Aug 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

2026-07-28 All issues and PRs related to the spec release 2026-07-28 Schema Issues & PRs related to the Schema component

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Server] stdio parse errors use id:"" instead of preserving the request id or returning null

2 participants