Skip to content

Commit dc722a4

Browse files
authored
Ensure the property field exists in tool input schemas (#97)
* Ensure the property field exists in tool input schemas * Update the minimum code coverage threshold to 91.4%
1 parent 2c0ad03 commit dc722a4

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@
8080
"pint --test",
8181
"rector --dry-run"
8282
],
83-
"test:unit": "pest --ci --coverage --min=91.2",
83+
"test:unit": "pest --ci --coverage --min=91.4",
8484
"test:types": "phpstan",
8585
"test": [
8686
"@test:lint",

src/Server/Tool.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,17 @@ public function toMethodCall(): array
5757
public function toArray(): array
5858
{
5959
$annotations = $this->annotations();
60+
$schema = JsonSchema::object(
61+
$this->schema(...),
62+
)->toArray();
63+
64+
$schema['properties'] ??= (object) [];
6065

6166
return [
6267
'name' => $this->name(),
6368
'title' => $this->title(),
6469
'description' => $this->description(),
65-
'inputSchema' => JsonSchema::object(
66-
$this->schema(...),
67-
)->toArray(),
70+
'inputSchema' => $schema,
6871
'annotations' => $annotations === [] ? (object) [] : $annotations,
6972
];
7073
}

tests/Unit/Tools/ToolTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,28 @@
7272
]);
7373
});
7474

75+
it('includes an empty properties object when the schema has no properties', function (): void {
76+
$tool = new TestTool;
77+
$array = $tool->toArray();
78+
79+
expect($array['inputSchema'])
80+
->toHaveKey('type', 'object')
81+
->toHaveKey('properties')
82+
->and($array['inputSchema']['properties'])->toEqual((object) []);
83+
});
84+
85+
it('includes schema properties when defined', function (): void {
86+
$tool = new ToolWithSchema;
87+
$array = $tool->toArray();
88+
89+
expect($array['inputSchema']['properties'])
90+
->toHaveKey('message')
91+
->and($array['inputSchema']['properties']['message'])
92+
->toHaveKey('type', 'string')
93+
->toHaveKey('description', 'The message to echo')
94+
->and($array['inputSchema']['required'])->toEqual(['message']);
95+
});
96+
7597
class TestTool extends Tool
7698
{
7799
public function description(): string
@@ -123,3 +145,13 @@ class CustomToolName extends TestTool
123145
{
124146
protected string $name = 'my_custom_tool_name';
125147
}
148+
149+
class ToolWithSchema extends TestTool
150+
{
151+
public function schema(\Illuminate\JsonSchema\JsonSchema $schema): array
152+
{
153+
return [
154+
'message' => $schema->string()->description('The message to echo')->required(),
155+
];
156+
}
157+
}

0 commit comments

Comments
 (0)