Skip to content

Commit 321320c

Browse files
committed
DI for the AIcommand
1 parent 379c0f6 commit 321320c

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

ai-command.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
}
1717

1818
WP_CLI::add_command( 'ai', function ( $args, $assoc_args ) {
19+
$server = new MCP\Server();
20+
$client = new MCP\Client($server);
21+
1922
$tools = new ToolCollection();
2023

2124
// TODO Register your tool here and add it to the collection
@@ -27,6 +30,10 @@
2730
$tools->add( $tool );
2831
}
2932

30-
$ai_command = new AiCommand( new CollectionToolRepository( $tools ) );
33+
$ai_command = new AiCommand(
34+
new CollectionToolRepository( $tools ),
35+
$server,
36+
$client
37+
);
3138
$ai_command( $args, $assoc_args );
3239
} );

src/AiCommand.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ class AiCommand extends WP_CLI_Command {
2626

2727
public function __construct(
2828
private CollectionToolRepository $tools,
29+
private WP_CLI\AiCommand\MCP\Server $server,
30+
private WP_CLI\AiCommand\MCP\Client $client
2931
) {
3032
parent::__construct();
3133
}
@@ -54,20 +56,16 @@ public function __construct(
5456
* @param array $assoc_args Associative array of associative arguments.
5557
*/
5658
public function __invoke( $args, $assoc_args ) {
57-
$server = new MCP\Server();
58-
$client = new MCP\Client($server);
59+
$this->register_tools($this->server);
60+
$this->register_resources($this->server);
5961

60-
$this->register_tools($server, $client);
61-
62-
$this->register_resources($server);
63-
64-
$result = $client->call_ai_service_with_prompt( $args[0] );
62+
$result = $this->client->call_ai_service_with_prompt( $args[0] );
6563

6664
WP_CLI::success( $result );
6765
}
6866

6967
// Register tools for AI processing
70-
private function register_tools($server, $client) : void {
68+
private function register_tools($server) : void {
7169
$filters = apply_filters( 'wp_cli/ai_command/command/filters', [] );
7270

7371
foreach( $this->tools->find_all( $filters ) as $tool ) {

0 commit comments

Comments
 (0)