Repository navigation
[Server] Answer arguments the input schema refuses with a tool error from 2025-11-25 on #764
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
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -82,11 +82,24 @@ public function handle(Request $request, SessionInterface $session): Response|Er | |
| return Error::forInvalidParams($e->getMessage(), $request->getId()); | ||
| } | ||
|
|
||
| $context = new RequestContext($session, $request); | ||
|
|
||
| $inputSchema = $reference->tool->inputSchema; | ||
| $validationErrors = $this->schemaValidator->validateAgainstJsonSchema((object) $arguments, $inputSchema); | ||
| if (!empty($validationErrors)) { | ||
| $summaryMessage = "Invalid parameters for tool '{$toolName}': ".self::summarizeValidationErrors($validationErrors); | ||
|
|
||
| $this->logger->debug('Tool arguments do not match the input schema', [ | ||
| 'name' => $toolName, | ||
| 'validation_errors' => $validationErrors, | ||
| ]); | ||
|
|
||
| // SEP-1303: an input validation error is a tool execution error, | ||
| // which the model reads and can answer with adjusted arguments. | ||
| if ($context->getProtocolVersion()->reportsInvalidToolArgumentsAsToolError()) { | ||
|
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. SEP-1303 lists "value out of range" as a tool execution error too, but the |
||
| return new Response($request->getId(), CallToolResult::error([new TextContent($summaryMessage)])); | ||
| } | ||
|
|
||
| return Error::forInvalidParams($summaryMessage, $request->getId(), ['validation_errors' => $validationErrors]); | ||
| } | ||
|
|
||
|
|
@@ -95,8 +108,6 @@ public function handle(Request $request, SessionInterface $session): Response|Er | |
| $arguments['_session'] = $session; | ||
| $arguments['_request'] = $request; | ||
|
|
||
| $context = new RequestContext($session, $request); | ||
|
|
||
| try { | ||
| $result = $this->referenceHandler->handle($reference, $arguments); | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -333,15 +333,15 @@ public function testToolNotFoundIsLoggedAsCalledButNotAsExecuted(): void | |
| { | ||
| $this->registry->method('getTool')->willThrowException(new ToolNotFoundException('unknown')); | ||
|
|
||
| $this->assertCalledButNotExecuted($this->createCallToolRequest('unknown', [])); | ||
| $this->assertCalledButNotExecuted($this->createCallToolRequest('unknown', []), Error::class); | ||
| } | ||
|
|
||
| public function testInvalidArgumentsAreLoggedAsCalledButNotAsExecuted(): void | ||
| { | ||
| $toolReference = $this->createToolReference('strict', static fn () => 'unused'); | ||
| $this->registry->method('getTool')->willReturn($toolReference); | ||
|
|
||
| $this->assertCalledButNotExecuted($this->createCallToolRequest('strict', ['example' => 123])); | ||
| $this->assertCalledButNotExecuted($this->createCallToolRequest('strict', ['example' => 123]), Response::class); | ||
| } | ||
|
|
||
| public function testHandleWithNullResult(): void | ||
|
|
@@ -805,41 +805,71 @@ public function testDeclaredOutputSchemaWithoutStructuredContentIsLogged(): void | |
| $this->assertNull($response->result->structuredContent); | ||
| } | ||
|
|
||
| public function testValidationError(): void | ||
| /** | ||
| * @dataProvider provideRevisionsReportingRefusedArgumentsAsToolErrors | ||
|
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. can you use |
||
| */ | ||
| public function testArgumentsRefusedByTheInputSchemaAreReportedAsAToolError(?string $negotiated): void | ||
| { | ||
| $schema = [ | ||
| 'type' => 'object', | ||
| 'properties' => [ | ||
| 'favorite_number' => [ | ||
| 'type' => 'number', | ||
| 'description' => 'Your favorite number', | ||
| ], | ||
| ], | ||
| 'required' => [ | ||
| 'favorite_number', | ||
| ], | ||
| ]; | ||
| $request = $this->createCallToolRequest('strict', ['example' => 123]); | ||
| $toolReference = $this->createToolReference('strict', static fn () => 'unused'); | ||
|
|
||
| $request = $this->createCallToolRequest('result_tool', ['query' => 'php']); | ||
| $toolReference = $this->getMockBuilder(ToolReference::class) | ||
| ->setConstructorArgs([new Tool('simple_tool', null, $schema, null, null), static function () {}]) | ||
| ->getMock(); | ||
| $this->session->method('get')->with('protocol_version')->willReturn($negotiated); | ||
| $this->registry->method('getTool')->willReturn($toolReference); | ||
| $this->referenceHandler->expects($this->never())->method('handle'); | ||
|
|
||
| $this->registry | ||
| ->expects($this->once()) | ||
| ->method('getTool') | ||
| ->with('result_tool') | ||
| ->willReturn($toolReference); | ||
| $response = $this->handler->handle($request, $this->session); | ||
|
|
||
| $this->referenceHandler | ||
| ->expects($this->never()) | ||
| ->method('handle'); | ||
| $this->assertInstanceOf(Response::class, $response); | ||
| $this->assertEquals($request->getId(), $response->id); | ||
| $this->assertInstanceOf(CallToolResult::class, $response->result); | ||
| $this->assertTrue($response->result->isError); | ||
| $this->assertFalse($response->result->hasStructuredContent()); | ||
| $this->assertSame( | ||
| "Invalid parameters for tool 'strict': Property '/example': Invalid type. Expected `string`, but received `integer`.", | ||
| $this->firstText($response->result), | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * @return iterable<string, array{?string}> | ||
| */ | ||
| public static function provideRevisionsReportingRefusedArgumentsAsToolErrors(): iterable | ||
| { | ||
| yield '2025-11-25' => ['2025-11-25']; | ||
| yield '2026-07-28' => ['2026-07-28']; | ||
| yield 'no revision negotiated' => [null]; | ||
| } | ||
|
|
||
| /** | ||
| * @dataProvider provideRevisionsReportingRefusedArgumentsAsProtocolErrors | ||
| */ | ||
| public function testArgumentsRefusedByTheInputSchemaStayAProtocolErrorBeforeRevision20251125(string $negotiated): void | ||
| { | ||
| $request = $this->createCallToolRequest('strict', ['example' => 123]); | ||
| $toolReference = $this->createToolReference('strict', static fn () => 'unused'); | ||
|
|
||
| $this->session->method('get')->with('protocol_version')->willReturn($negotiated); | ||
| $this->registry->method('getTool')->willReturn($toolReference); | ||
| $this->referenceHandler->expects($this->never())->method('handle'); | ||
|
|
||
| $response = $this->handler->handle($request, $this->session); | ||
|
|
||
| $this->assertInstanceOf(Error::class, $response); | ||
| $this->assertEquals($request->getId(), $response->id); | ||
| $this->assertEquals(Error::INVALID_PARAMS, $response->code); | ||
| $this->assertSame("Invalid parameters for tool 'strict': Property '/example': Invalid type. Expected `string`, but received `integer`.", $response->message); | ||
| $this->assertIsArray($response->data); | ||
| $this->assertArrayHasKey('validation_errors', $response->data); | ||
| } | ||
|
|
||
| /** | ||
| * @return iterable<string, array{string}> | ||
| */ | ||
| public static function provideRevisionsReportingRefusedArgumentsAsProtocolErrors(): iterable | ||
| { | ||
| yield '2024-11-05' => ['2024-11-05']; | ||
| yield '2025-03-26' => ['2025-03-26']; | ||
| yield '2025-06-18' => ['2025-06-18']; | ||
| } | ||
|
|
||
| public function testStructuredContentMissingARequiredPropertyIsReportedAsAToolError(): void | ||
|
|
@@ -1087,7 +1117,10 @@ private function createCallToolRequest(string $name, array $arguments): CallTool | |
| ]); | ||
| } | ||
|
|
||
| private function assertCalledButNotExecuted(CallToolRequest $request): void | ||
| /** | ||
| * @param class-string $answer | ||
| */ | ||
| private function assertCalledButNotExecuted(CallToolRequest $request, string $answer): void | ||
| { | ||
| $logger = new class extends AbstractLogger { | ||
| public array $records = []; | ||
|
|
@@ -1102,7 +1135,7 @@ public function log($level, $message, array $context = []): void | |
|
|
||
| $this->referenceHandler->expects($this->never())->method('handle'); | ||
|
|
||
| $this->assertInstanceOf(Error::class, $handler->handle($request, $this->session)); | ||
| $this->assertInstanceOf($answer, $handler->handle($request, $this->session)); | ||
|
|
||
| $messages = array_column($logger->records, 'message'); | ||
| $this->assertContains('Calling tool', $messages); | ||
|
|
||
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.
the predicate name already says it, can we drop this comment?