Skip to content

Commit 71e026b

Browse files
committed
tool registration
1 parent bbb3b5a commit 71e026b

File tree

7 files changed

+41
-14
lines changed

7 files changed

+41
-14
lines changed

ai-command.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace WP_CLI\AiCommand;
44

5+
use WP_CLI\AiCommand\ToolRepository\CollectionToolRepository;
56
use WP_CLI;
67

78
if ( ! class_exists( '\WP_CLI' ) ) {
@@ -19,6 +20,13 @@
1920

2021
// TODO Register your tool here and add it to the collection
2122

22-
$ai_command = new AiCommand( $tools );
23+
// WordPress REST calls
24+
$rest_tools = new MapRESTtoMCP();
25+
26+
foreach( $rest_tools->map_rest_to_mcp() as $tool ) {
27+
$tools->add( $tool );
28+
}
29+
30+
$ai_command = new AiCommand( new CollectionToolRepository( $tools ) );
2331
$ai_command( $args, $assoc_args );
2432
} );

src/AiCommand.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace WP_CLI\AiCommand;
44

5+
use WP_CLI\AiCommand\ToolRepository\CollectionToolRepository;
56
use WP_CLI\AiCommand\Tools\FileTools;
67
use WP_CLI\AiCommand\Tools\URLTools;
78
use WP_CLI;
@@ -24,7 +25,7 @@
2425
class AiCommand extends WP_CLI_Command {
2526

2627
public function __construct(
27-
private ToolCollection $tools,
28+
private CollectionToolRepository $tools,
2829
) {
2930
parent::__construct();
3031
}
@@ -66,7 +67,16 @@ public function __invoke( $args, $assoc_args ) {
6667
}
6768

6869
// Register tools for AI processing
69-
private function register_tools($server, $client) {
70+
private function register_tools($server, $client) : void {
71+
$filters = apply_filters( 'wp_cli/ai_command/command/filters', [] );
72+
73+
foreach( $this->tools->find_all( $filters ) as $tool ) {
74+
$server->register_tool( $tool->get_data() );
75+
}
76+
77+
return;
78+
79+
// TODO move this
7080
$server->register_tool(
7181
[
7282
'name' => 'list_tools',
@@ -95,8 +105,7 @@ private function register_tools($server, $client) {
95105
]
96106
);
97107

98-
$map_rest_to_mcp = new MapRESTtoMCP();
99-
$map_rest_to_mcp->map_rest_to_mcp( $server );
108+
100109

101110
new FileTools( $server );
102111
new URLTools( $server );

src/Entity/Tool.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
declare(strict_types=1);
44

5+
namespace WP_CLI\AiCommand\Entity;
6+
57
final class Tool
68
{
79

src/MapRESTtoMCP.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
namespace WP_CLI\AiCommand;
44

5+
use WP_CLI\AiCommand\Entity\Tool;
56
use WP_CLI;
6-
use WP_CLI\AiCommand\MCP\Server;
77
use WP_REST_Request;
88

99

@@ -75,9 +75,10 @@ protected function sanitize_type( $type) {
7575

7676
}
7777

78-
public function map_rest_to_mcp( Server $mcp_server ) {
78+
public function map_rest_to_mcp() : array {
7979
$server = rest_get_server();
8080
$routes = $server->get_routes();
81+
$tools = [];
8182

8283
foreach ( $routes as $route => $endpoints ) {
8384
foreach ( $endpoints as $endpoint ) {
@@ -92,19 +93,21 @@ public function map_rest_to_mcp( Server $mcp_server ) {
9293
continue;
9394
}
9495

95-
$tool = [
96+
$tool = new Tool( [
9697
'name' => $information->get_sanitized_route_name(),
9798
'description' => $this->generate_description( $information ),
9899
'inputSchema' => $this->args_to_schema( $endpoint['args'] ),
99100
'callable' => function ( $inputs ) use ( $route, $method_name, $server ){
100101
return $this->rest_callable( $inputs, $route, $method_name, $server );
101102
},
102-
];
103+
] );
103104

104-
$mcp_server->register_tool($tool);
105+
$tools[] = $tool;
105106
}
106107
}
107108
}
109+
110+
return $tools;
108111
}
109112

110113
/**

src/ToolCollection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace WP_CLI\AiCommand;
66

7-
use Tool;
7+
use WP_CLI\AiCommand\Entity\Tool;
88

99
final class ToolCollection extends Collection
1010
{

src/ToolRepository.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace WP_CLI\AiCommand;
66

7-
use Tool;
7+
use WP_CLI\AiCommand\Entity\Tool;
88

99
interface ToolRepository
1010
{

src/ToolRepository/CollectionToolRepository.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
declare(strict_types=1);
44

5+
namespace WP_CLI\AiCommand\ToolRepository;
6+
7+
use WP_CLI\AiCommand\Entity\Tool;
58
use WP_CLI\AiCommand\ToolCollection;
69
use WP_CLI\AiCommand\ToolRepository;
710

@@ -33,10 +36,12 @@ public function find_all(array $filters = []): ToolCollection
3336

3437
$filters = array_merge($defaults, $filters);
3538

36-
$all = iterator_to_array($this->collection);
37-
$filtered = [];
39+
$filtered = iterator_to_array($this->collection);
3840

3941
if ($filters['include'] !== 'all') {
42+
$all = $filtered;
43+
$filtered = [];
44+
4045
foreach ($filters['include'] as $tag_to_include) {
4146
foreach ($all as $tool) {
4247
foreach ($tool->get_tags() as $tag_to_check) {

0 commit comments

Comments
 (0)