-
Notifications
You must be signed in to change notification settings - Fork 152
[Schema][Server] Preserve request id in JSON-RPC error responses instead of fabricating id:"" #379
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
base: main
Are you sure you want to change the base?
Changes from all commits
79e6632
40b4c6b
e78be29
262db1a
84fb7b2
1254d28
1091940
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
|
Contributor
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. 🟡 🟡 🔵 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the official PHP MCP SDK. | ||
| * | ||
| * A collaboration between Symfony and the PHP Foundation. | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace Mcp\Tests\Unit\Client; | ||
|
|
||
| use Mcp\Client\Protocol; | ||
| use Mcp\Schema\JsonRpc\Error; | ||
| use PHPUnit\Framework\TestCase; | ||
| use Psr\Log\AbstractLogger; | ||
|
|
||
| final class ProtocolTest extends TestCase | ||
| { | ||
| public function testNullIdErrorResponseIsLoggedAndDropped(): void | ||
| { | ||
| $logger = $this->createSpyLogger(); | ||
| $protocol = new Protocol(logger: $logger); | ||
|
|
||
| $protocol->processMessage('{"jsonrpc": "2.0", "id": null, "error": {"code": -32700, "message": "Parse error"}}'); | ||
|
|
||
| $warnings = array_values(array_filter($logger->records, static fn (array $record): bool => 'warning' === $record['level'])); | ||
|
|
||
| $this->assertCount(1, $warnings); | ||
| $this->assertNull($warnings[0]['context']['response']['id']); | ||
| $this->assertSame(Error::PARSE_ERROR, $warnings[0]['context']['response']['error']['code']); | ||
| } | ||
|
|
||
| public function testErrorResponseWithIdIsStoredForItsPendingRequest(): void | ||
| { | ||
| $protocol = new Protocol(); | ||
|
|
||
| $protocol->processMessage('{"jsonrpc": "2.0", "id": 7, "error": {"code": -32601, "message": "Method not found"}}'); | ||
|
|
||
| $response = $protocol->getState()->consumeResponse(7); | ||
|
|
||
| $this->assertInstanceOf(Error::class, $response); | ||
| $this->assertSame(7, $response->getId()); | ||
| $this->assertSame(Error::METHOD_NOT_FOUND, $response->code); | ||
| } | ||
|
|
||
| /** | ||
| * @return AbstractLogger&object{records: list<array{level: string, message: string, context: array<string, mixed>}>} | ||
| */ | ||
| private function createSpyLogger(): AbstractLogger | ||
| { | ||
| return new class extends AbstractLogger { | ||
| /** @var list<array{level: string, message: string, context: array<string, mixed>}> */ | ||
| public array $records = []; | ||
|
|
||
| public function log($level, mixed $message, array $context = []): void | ||
| { | ||
| $this->records[] = ['level' => (string) $level, 'message' => (string) $message, 'context' => $context]; | ||
| } | ||
| }; | ||
| } | ||
| } |
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.
❓ JSON-RPC 2.0 allows
id: nullin requests. Hereisset()drops it, so a null-id invalid request falls back to thenulldefault inforInvalidRequest()— same outcome, fine. Intentional? If so, worth a one-line comment so the next reader doesn't "fix" it by switching toarray_key_exists.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.
Intentional - isset + the type guard. We only recover valid
string|intids; a null or non-scalar id stays at the exception's null default.array_key_existswould converge to the same via the type guard. Leaving a comment in the code.