Skip to content

Commit f9b86aa

Browse files
committed
feat: Add tools functionality and assertions for configuration testing
1 parent 30317ae commit f9b86aa

File tree

7 files changed

+1283
-320
lines changed

7 files changed

+1283
-320
lines changed

src/Console/GenerateCommand.php

+2
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ public function __invoke(Container $container, DirectoriesInterface $dirs): int
138138
'message' => 'No documents found in configuration.',
139139
'imports' => $imports,
140140
'prompts' => $config->getPrompts(),
141+
'tools' => $config->getTools(),
141142
]));
142143
} else {
143144
$this->output->warning('No documents found in configuration.');
@@ -171,6 +172,7 @@ public function __invoke(Container $container, DirectoriesInterface $dirs): int
171172
'result' => $result,
172173
'imports' => $imports,
173174
'prompts' => $config->getPrompts(),
175+
'tools' => $config->getTools(),
174176
]));
175177
} else {
176178
$this->output->writeln('');

src/McpServer/Tool/ToolRegistry.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,7 @@ public function getItems(): array
6060

6161
public function jsonSerialize(): array
6262
{
63-
return [
64-
'tools' => $this->getItems(),
65-
];
63+
return $this->getItems();
6664
}
6765

6866
public function getIterator(): \Traversable

tests/docs/writing-generate-command-tests.md

+123
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,44 @@ When using the `generate` command with the `--json` flag, it returns a JSON resp
6060
]
6161
}
6262
// Additional prompts...
63+
],
64+
"tools": [
65+
{
66+
"id": "tool-id",
67+
"description": "Tool description",
68+
"type": "run|http",
69+
"schema": {
70+
"properties": {
71+
"paramName": {
72+
"type": "string|number|boolean",
73+
"description": "Parameter description"
74+
}
75+
},
76+
"required": [
77+
"paramName"
78+
]
79+
},
80+
"commands": [
81+
{
82+
"cmd": "command-name",
83+
"args": [
84+
"arg1",
85+
"arg2"
86+
]
87+
}
88+
],
89+
"requests": [
90+
{
91+
"url": "https://api.example.com/endpoint",
92+
"method": "GET|POST",
93+
"headers": {
94+
"Content-Type": "application/json",
95+
"Authorization": "Bearer {{TOKEN}}"
96+
}
97+
}
98+
]
99+
}
100+
// Additional tools...
63101
]
64102
}
65103
```
@@ -85,6 +123,7 @@ The test files should follow these naming conventions:
85123
- `LocalImportTest.php` - Tests for local imports
86124
- `UrlImportTest.php` - Tests for URL imports
87125
- `PromptsTest.php` - Tests for prompts functionality
126+
- `ToolsTest.php` - Tests for tools functionality
88127

89128
Each test class should extend `Tests\Feature\Console\ConsoleTestCase` to access the common testing functionality.
90129

@@ -115,6 +154,7 @@ tests/fixtures/Console/GenerateCommand/GithubSource/
115154
tests/fixtures/Console/GenerateCommand/GitlabSource/
116155
tests/fixtures/Console/GenerateCommand/UrlSource/
117156
tests/fixtures/Console/GenerateCommand/Prompts/
157+
tests/fixtures/Console/GenerateCommand/Tools/
118158
```
119159

120160
### Creating Fixtures
@@ -205,6 +245,18 @@ $this
205245
| `assertPromptCount(count)` | Check the number of prompts |
206246
| `assertNoPrompts()` | Check that no prompts were found |
207247

248+
### Tool Assertions
249+
250+
| Method | Description |
251+
|----------------------------------------------|------------------------------------------------|
252+
| `assertToolExists(id)` | Check that a tool with the specified ID exists |
253+
| `assertTool(id, properties)` | Check tool properties |
254+
| `assertToolSchema(id, properties, required)` | Check tool schema structure |
255+
| `assertToolCommands(id, expectedCommands)` | Check commands in a run-type tool |
256+
| `assertToolRequests(id, expectedRequests)` | Check requests in an http-type tool |
257+
| `assertToolCount(count)` | Check the number of tools |
258+
| `assertNoTools()` | Check that no tools were found |
259+
208260
### Example Prompt Test
209261

210262
```php
@@ -233,6 +285,65 @@ public function basic_prompts_should_be_compiled(): void
233285
}
234286
```
235287

288+
### Example Tool Test
289+
290+
```php
291+
public function basic_tools_should_be_compiled(): void
292+
{
293+
$this
294+
->buildContext(
295+
workDir: $this->outputDir,
296+
configPath: $this->getFixturesDir('Console/GenerateCommand/Tools/basic.yaml'),
297+
)
298+
->assertSuccessfulCompiled()
299+
->assertToolExists('test-command')
300+
->assertTool('test-command', [
301+
'type' => 'run',
302+
'description' => 'A simple test command',
303+
])
304+
->assertToolSchema('test-command',
305+
[
306+
'param' => [
307+
'type' => 'string',
308+
'description' => 'Command parameter',
309+
],
310+
],
311+
['param'],
312+
)
313+
->assertToolCommands('test-command', [
314+
[
315+
'cmd' => 'echo',
316+
'args' => ['Hello', '{{param}}'],
317+
],
318+
]);
319+
}
320+
321+
public function http_tools_should_be_compiled(): void
322+
{
323+
$this
324+
->buildContext(
325+
workDir: $this->outputDir,
326+
configPath: $this->getFixturesDir('Console/GenerateCommand/Tools/http.yaml'),
327+
)
328+
->assertSuccessfulCompiled()
329+
->assertToolExists('test-api')
330+
->assertTool('test-api', [
331+
'type' => 'http',
332+
'description' => 'A simple HTTP API tool',
333+
])
334+
->assertToolRequests('test-api', [
335+
[
336+
'method' => 'GET',
337+
'url' => 'https://api.example.com/data',
338+
'headers' => [
339+
'Content-Type' => 'application/json',
340+
'Authorization' => 'Bearer {{token}}',
341+
],
342+
],
343+
]);
344+
}
345+
```
346+
236347
## 5. Testing Best Practices
237348

238349
### Use Data Providers
@@ -335,3 +446,15 @@ When testing imports:
335446
4. Test wildcard patterns
336447
5. Test variable substitution
337448
6. Test error handling (404, invalid configs)
449+
450+
### Testing Tools
451+
452+
When testing tools:
453+
454+
1. Test basic 'run' type tools with simple commands
455+
2. Test tools with command arguments and conditional arguments
456+
3. Test 'http' type tools with different request methods
457+
4. Test tools with environment variables
458+
5. Test tools with complex schemas
459+
6. Test tool error handling (validation errors, execution errors)
460+
7. Test the interaction between tools and other config sections (like variables)

0 commit comments

Comments
 (0)