feat: Add output schema support to MCP tools (#153)
This commit is contained in:
parent
4c3e5e61c7
commit
c0938a19bd
1 changed files with 605 additions and 55 deletions
|
|
@ -527,7 +527,91 @@ class RegistryTest extends TestCase
|
|||
$this->assertEquals('second', ($toolRef->handler)());
|
||||
}
|
||||
|
||||
private function createValidTool(string $name): Tool
|
||||
public function testExtractStructuredContentReturnsNullWhenOutputSchemaIsNull(): void
|
||||
{
|
||||
$tool = $this->createValidTool('test_tool', null);
|
||||
$this->registry->registerTool($tool, fn () => 'result');
|
||||
|
||||
$toolRef = $this->registry->getTool('test_tool');
|
||||
$this->assertNull($toolRef->extractStructuredContent('result'));
|
||||
}
|
||||
|
||||
public function testExtractStructuredContentReturnsArrayMatchingSchema(): void
|
||||
{
|
||||
$tool = $this->createValidTool('test_tool', [
|
||||
'type' => 'object',
|
||||
'properties' => [
|
||||
'param' => ['type' => 'string'],
|
||||
],
|
||||
'required' => ['param'],
|
||||
]);
|
||||
$this->registry->registerTool($tool, fn () => [
|
||||
'param' => 'test',
|
||||
]);
|
||||
|
||||
$toolRef = $this->registry->getTool('test_tool');
|
||||
$this->assertEquals([
|
||||
'param' => 'test',
|
||||
], $toolRef->extractStructuredContent([
|
||||
'param' => 'test',
|
||||
]));
|
||||
}
|
||||
|
||||
public function testExtractStructuredContentReturnsArrayDirectlyForAdditionalProperties(): void
|
||||
{
|
||||
$tool = $this->createValidTool('test_tool', [
|
||||
'type' => 'object',
|
||||
'additionalProperties' => true,
|
||||
]);
|
||||
$this->registry->registerTool($tool, fn () => ['success' => true, 'message' => 'done']);
|
||||
|
||||
$toolRef = $this->registry->getTool('test_tool');
|
||||
$this->assertEquals(['success' => true, 'message' => 'done'], $toolRef->extractStructuredContent(['success' => true, 'message' => 'done']));
|
||||
}
|
||||
|
||||
public function testExtractStructuredContentReturnsArrayDirectlyForArrayOutputSchema(): void
|
||||
{
|
||||
// Arrange
|
||||
$outputSchema = [
|
||||
'type' => 'array',
|
||||
'items' => [
|
||||
'type' => 'object',
|
||||
'properties' => [
|
||||
'foo' => [
|
||||
'type' => 'string',
|
||||
'description' => 'A static value',
|
||||
],
|
||||
],
|
||||
'required' => ['foo'],
|
||||
],
|
||||
];
|
||||
|
||||
$tool = $this->createValidTool('list_static_data', $outputSchema);
|
||||
$toolReturnValue = [
|
||||
['foo' => 'bar'],
|
||||
['foo' => 'bar'],
|
||||
['foo' => 'bar'],
|
||||
['foo' => 'bar'],
|
||||
];
|
||||
|
||||
$this->registry->registerTool($tool, fn () => $toolReturnValue);
|
||||
|
||||
// Act
|
||||
$toolRef = $this->registry->getTool('list_static_data');
|
||||
$structuredContent = $toolRef->extractStructuredContent($toolReturnValue);
|
||||
|
||||
// Assert
|
||||
$this->assertNotNull($structuredContent);
|
||||
$this->assertCount(4, $structuredContent);
|
||||
$this->assertEquals([
|
||||
['foo' => 'bar'],
|
||||
['foo' => 'bar'],
|
||||
['foo' => 'bar'],
|
||||
['foo' => 'bar'],
|
||||
], $structuredContent);
|
||||
}
|
||||
|
||||
private function createValidTool(string $name, ?array $outputSchema = null): Tool
|
||||
{
|
||||
return new Tool(
|
||||
name: $name,
|
||||
|
|
@ -540,6 +624,9 @@ class RegistryTest extends TestCase
|
|||
],
|
||||
description: "Test tool: {$name}",
|
||||
annotations: null,
|
||||
icons: null,
|
||||
meta: null,
|
||||
outputSchema: $outputSchema
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue