Skip to content

Commit 6cba05e

Browse files
committed
Refactor AiCommand with methods for registering tools and resources.
1 parent 7810221 commit 6cba05e

File tree

1 file changed

+31
-25
lines changed

1 file changed

+31
-25
lines changed

src/AiCommand.php

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,19 @@ class AiCommand extends WP_CLI_Command {
4343
*/
4444
public function __invoke( $args, $assoc_args ) {
4545
$server = new MCP\Server();
46+
$client = new MCP\Client($server);
4647

48+
$this->register_tools($server, $client);
49+
50+
$this->register_resources($server);
51+
52+
$result = $client->call_ai_service_with_prompt( $args[0] );
53+
54+
WP_CLI::success( $result );
55+
}
56+
57+
// Register tools for AI processing
58+
private function register_tools($server, $client) {
4759
$server->register_tool(
4860
[
4961
'name' => 'calculate_total',
@@ -91,29 +103,6 @@ public function __invoke( $args, $assoc_args ) {
91103
]
92104
);
93105

94-
// Register resources:
95-
$server->register_resource(
96-
[
97-
'name' => 'users',
98-
'uri' => 'data://users',
99-
'description' => 'List of users',
100-
'mimeType' => 'application/json',
101-
'dataKey' => 'users', // This tells getResourceData() to look in the $data array
102-
]
103-
);
104-
105-
$server->register_resource(
106-
[
107-
'name' => 'product_catalog',
108-
'uri' => 'file://./products.json',
109-
'description' => 'Product catalog',
110-
'mimeType' => 'application/json',
111-
'filePath' => './products.json', // This tells getResourceData() to read from a file
112-
]
113-
);
114-
115-
$client = new MCP\Client( $server );
116-
117106
$server->register_tool(
118107
[
119108
'name' => 'generate_image',
@@ -133,9 +122,26 @@ public function __invoke( $args, $assoc_args ) {
133122
},
134123
]
135124
);
125+
}
136126

137-
$result = $client->call_ai_service_with_prompt( $args[0] );
127+
// Register resources for AI access
128+
private function register_resources($server) {
129+
// Register Users resource
130+
$server->register_resource([
131+
'name' => 'users',
132+
'uri' => 'data://users',
133+
'description' => 'List of users',
134+
'mimeType' => 'application/json',
135+
'dataKey' => 'users', // Data will be fetched from 'users'
136+
]);
138137

139-
WP_CLI::success( $result );
138+
// Register Product Catalog resource
139+
$server->register_resource([
140+
'name' => 'product_catalog',
141+
'uri' => 'file://./products.json',
142+
'description' => 'Product catalog',
143+
'mimeType' => 'application/json',
144+
'filePath' => './products.json', // Data will be fetched from products.json
145+
]);
140146
}
141147
}

0 commit comments

Comments
 (0)