-
Notifications
You must be signed in to change notification settings - Fork 167
[Schema] Omit the id of an error response that never had one #442
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
chr-hertel
merged 2 commits into
modelcontextprotocol:main
from
chr-hertel:pr/error-null-id
Aug 18, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -57,12 +57,15 @@ class Error implements MessageInterface | |
| public const UNSUPPORTED_PROTOCOL_VERSION = -32022; | ||
|
|
||
| /** | ||
| * @param int $code the error type that occurred | ||
| * @param string $message a short description of the error | ||
| * @param mixed|null $data additional information about the error | ||
| * @param string|int|null $id The id of the request this answers. `null` only when it could not be | ||
| * read — a malformed body, or a notification that was refused — in which | ||
| * case the member is omitted rather than sent as an id nobody issued. | ||
| * @param int $code the error type that occurred | ||
| * @param string $message a short description of the error | ||
| * @param mixed|null $data additional information about the error | ||
| */ | ||
| public function __construct( | ||
| public readonly string|int $id, | ||
| public readonly string|int|null $id, | ||
| public readonly int $code, | ||
| public readonly string $message, | ||
| public readonly mixed $data = null, | ||
|
|
@@ -77,10 +80,9 @@ final public static function fromArray(array $data): self | |
| if (!isset($data['jsonrpc']) || MessageInterface::JSONRPC_VERSION !== $data['jsonrpc']) { | ||
| throw new InvalidArgumentException('Invalid or missing "jsonrpc" in Error data.'); | ||
| } | ||
| if (!isset($data['id'])) { | ||
| throw new InvalidArgumentException('Invalid or missing "id" in Error data.'); | ||
| } | ||
| if (!\is_string($data['id']) && !\is_int($data['id'])) { | ||
| // An error response carrying no id is well-formed: it is what a | ||
| // receiver sends when the id could not be read off the request. | ||
| if (isset($data['id']) && !\is_string($data['id']) && !\is_int($data['id'])) { | ||
| throw new InvalidArgumentException('Invalid "id" type in Error data.'); | ||
| } | ||
| if (!isset($data['error']) || !\is_array($data['error'])) { | ||
|
|
@@ -93,45 +95,45 @@ final public static function fromArray(array $data): self | |
| throw new InvalidArgumentException('Invalid or missing "message" in Error data.'); | ||
| } | ||
|
|
||
| return new self($data['id'], $data['error']['code'], $data['error']['message'], $data['error']['data'] ?? null); | ||
| return new self($data['id'] ?? null, $data['error']['code'], $data['error']['message'], $data['error']['data'] ?? null); | ||
| } | ||
|
|
||
| final public static function forParseError(string $message, string|int $id = ''): self | ||
| final public static function forParseError(string $message, string|int|null $id = null): self | ||
| { | ||
| return new self($id, self::PARSE_ERROR, $message); | ||
| } | ||
|
|
||
| final public static function forInvalidRequest(string $message, string|int $id = ''): self | ||
| final public static function forInvalidRequest(string $message, string|int|null $id = null): self | ||
| { | ||
| return new self($id, self::INVALID_REQUEST, $message); | ||
| } | ||
|
|
||
| final public static function forMethodNotFound(string $message, string|int $id = ''): self | ||
| final public static function forMethodNotFound(string $message, string|int|null $id = null): self | ||
| { | ||
| return new self($id, self::METHOD_NOT_FOUND, $message); | ||
| } | ||
|
|
||
| final public static function forInvalidParams(string $message, string|int $id = '', mixed $data = null): self | ||
| final public static function forInvalidParams(string $message, string|int|null $id = null, mixed $data = null): self | ||
| { | ||
| return new self($id, self::INVALID_PARAMS, $message, $data); | ||
| } | ||
|
|
||
| final public static function forInternalError(string $message, string|int $id = ''): self | ||
| final public static function forInternalError(string $message, string|int|null $id = null): self | ||
| { | ||
| return new self($id, self::INTERNAL_ERROR, $message); | ||
| } | ||
|
|
||
| final public static function forServerError(string $message, string|int $id = ''): self | ||
| final public static function forServerError(string $message, string|int|null $id = null): self | ||
| { | ||
| return new self($id, self::SERVER_ERROR, $message); | ||
| } | ||
|
|
||
| final public static function forResourceNotFound(string $message, string|int $id = ''): self | ||
| final public static function forResourceNotFound(string $message, string|int|null $id = null): self | ||
| { | ||
| return new self($id, self::RESOURCE_NOT_FOUND, $message); | ||
| } | ||
|
|
||
| final public static function forHeaderMismatch(string $message, string|int $id = ''): self | ||
| final public static function forHeaderMismatch(string $message, string|int|null $id = null): self | ||
| { | ||
| return new self($id, self::HEADER_MISMATCH, $message); | ||
| } | ||
|
|
@@ -142,7 +144,7 @@ final public static function forHeaderMismatch(string $message, string|int $id = | |
| final public static function forMissingRequiredClientCapability( | ||
| string $message, | ||
| ClientCapabilities $requiredCapabilities, | ||
| string|int $id = '', | ||
| string|int|null $id = null, | ||
| ): self { | ||
| return new self($id, self::MISSING_REQUIRED_CLIENT_CAPABILITY, $message, [ | ||
| 'requiredCapabilities' => $requiredCapabilities, | ||
|
|
@@ -159,28 +161,28 @@ final public static function forMissingRequiredClientCapability( | |
| final public static function forUnsupportedProtocolVersion( | ||
| string $requested, | ||
| array $supported, | ||
| string|int $id = '', | ||
| string|int|null $id = null, | ||
| ): self { | ||
| return new self($id, self::UNSUPPORTED_PROTOCOL_VERSION, 'Unsupported protocol version', [ | ||
| 'requested' => $requested, | ||
| 'supported' => array_values(array_map(static fn (ProtocolVersion $v): string => $v->value, $supported)), | ||
| ]); | ||
| } | ||
|
|
||
| public function getId(): string|int | ||
| public function getId(): string|int|null | ||
| { | ||
| return $this->id; | ||
| } | ||
|
|
||
| /** | ||
| * @return array{ | ||
| * jsonrpc: string, | ||
| * id: string|int, | ||
| * id?: string|int, | ||
| * error: array{ | ||
| * code: int, | ||
| * message: string, | ||
|
Comment on lines
179
to
183
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in e3ff482 — moved |
||
| * data?: mixed, | ||
| * }, | ||
| * data?: mixed, | ||
| * } | ||
| */ | ||
| public function jsonSerialize(): array | ||
|
|
@@ -194,10 +196,17 @@ public function jsonSerialize(): array | |
| $error['data'] = $this->data; | ||
| } | ||
|
|
||
| return [ | ||
| 'jsonrpc' => MessageInterface::JSONRPC_VERSION, | ||
| 'id' => $this->id, | ||
| 'error' => $error, | ||
| ]; | ||
| $data = ['jsonrpc' => MessageInterface::JSONRPC_VERSION]; | ||
|
|
||
| // Omitted, not empty: `"id": ""` claims the sender issued a request | ||
| // with an empty-string id, which is a different statement from "the | ||
| // id could not be read". | ||
| if (null !== $this->id) { | ||
| $data['id'] = $this->id; | ||
| } | ||
|
|
||
| $data['error'] = $error; | ||
|
|
||
| return $data; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.