Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
"pint --test",
"rector --dry-run"
],
"test:unit": "pest --ci --coverage --min=91.2",
"test:unit": "pest --ci --coverage --min=91.4",
"test:types": "phpstan",
"test": [
"@test:lint",
Expand Down
9 changes: 6 additions & 3 deletions src/Server/Tool.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,17 @@ public function toMethodCall(): array
public function toArray(): array
{
$annotations = $this->annotations();
$schema = JsonSchema::object(
$this->schema(...),
)->toArray();

$schema['properties'] ??= (object) [];

return [
'name' => $this->name(),
'title' => $this->title(),
'description' => $this->description(),
'inputSchema' => JsonSchema::object(
$this->schema(...),
)->toArray(),
'inputSchema' => $schema,
'annotations' => $annotations === [] ? (object) [] : $annotations,
];
}
Expand Down
32 changes: 32 additions & 0 deletions tests/Unit/Tools/ToolTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,28 @@
]);
});

it('includes an empty properties object when the schema has no properties', function (): void {
$tool = new TestTool;
$array = $tool->toArray();

expect($array['inputSchema'])
->toHaveKey('type', 'object')
->toHaveKey('properties')
->and($array['inputSchema']['properties'])->toEqual((object) []);
});

it('includes schema properties when defined', function (): void {
$tool = new ToolWithSchema;
$array = $tool->toArray();

expect($array['inputSchema']['properties'])
->toHaveKey('message')
->and($array['inputSchema']['properties']['message'])
->toHaveKey('type', 'string')
->toHaveKey('description', 'The message to echo')
->and($array['inputSchema']['required'])->toEqual(['message']);
});

class TestTool extends Tool
{
public function description(): string
Expand Down Expand Up @@ -123,3 +145,13 @@ class CustomToolName extends TestTool
{
protected string $name = 'my_custom_tool_name';
}

class ToolWithSchema extends TestTool
{
public function schema(\Illuminate\JsonSchema\JsonSchema $schema): array
{
return [
'message' => $schema->string()->description('The message to echo')->required(),
];
}
}