Skip to content

Commit 9bb5710

Browse files
committed
route information
1 parent c440518 commit 9bb5710

File tree

2 files changed

+59
-31
lines changed

2 files changed

+59
-31
lines changed

src/MapRESTtoMCP.php

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
namespace WP_CLI\AiCommand;
44

5+
use WP_CLI\AiCommand\RouteInformation;
56
use WP_CLI;
67
use WP_CLI\AiCommand\MCP\Server;
78
use WP_REST_Request;
89

10+
911
class MapRESTtoMCP {
1012

1113
public function __construct(
@@ -96,12 +98,20 @@ public function map_rest_to_mcp( Server $mcp_server ) {
9698
continue; // This route is the block list.
9799
}
98100

99-
100101
foreach( $endpoint['methods'] as $method_name => $enabled ) {
102+
$information = new RouteInformation(
103+
$route,
104+
$method_name,
105+
$endpoint['callback'],
106+
);
107+
108+
if ( ! $information->is_wp_rest_controller() ) {
109+
continue;
110+
}
101111

102112
$tool = [
103-
'name' => $this->generate_tool_name($route, $method_name),
104-
'description' => $this->generate_description( $route, $method_name, $endpoint ),
113+
'name' => $information->get_sanitized_route_name(),
114+
'description' => $this->generate_description( $information ),
105115
'inputSchema' => $this->args_to_schema( $endpoint['args'] ),
106116
'callable' => function ( $inputs ) use ( $route, $method_name, $server ){
107117
return $this->rest_callable( $inputs, $route, $method_name, $server );
@@ -113,6 +123,7 @@ public function map_rest_to_mcp( Server $mcp_server ) {
113123

114124
}
115125
}
126+
116127
}
117128

118129
protected function generate_tool_name($route, $method_name) {
@@ -130,36 +141,23 @@ protected function generate_tool_name($route, $method_name) {
130141
* Get a list of posts GET /wp/v2/posts
131142
* Get post with id GET /wp/v2/posts/(?P<id>[\d]+)
132143
*/
133-
protected function generate_description( $route, $method_name, $endpoint ) {
144+
protected function generate_description( RouteInformation $information ) : string {
134145

135-
// TODO all validation + exception handling.
136-
$verb = array(
146+
$verb = match ($information->get_method()) {
137147
'GET' => 'Get',
138148
'POST' => 'Create',
139-
'PUT' => 'Update',
140-
'PATCH' => 'Update',
149+
'PUT', 'PATCH' => 'Update',
141150
'DELETE' => 'Delete',
142-
);
151+
};
143152

144-
$controller = $endpoint['callback'][0];
145-
if ( !isset($endpoint['callback']) || ! \is_object($endpoint['callback'][0])) {
146-
throw new \Exception('Not an object: ' . $route);
147-
}
148-
if (! \method_exists($endpoint['callback'][0], 'get_public_item_schema')) {
149-
throw new \Exception('missing method: ' . $route);
150-
}
151-
152-
$schema = $controller->get_public_item_schema();
153+
$schema = $information->get_wp_rest_controller()->get_public_item_schema();
153154
$title = $schema['title'];
154155

155-
// is singular?
156-
$singular = 'a';
157-
if ( $method_name === 'GET' && ! \str_contains( $route, '(?P<' )) {
158-
$singular = 'List of';
159-
160-
}
156+
$determiner = $information->is_singular()
157+
? 'a'
158+
: 'list of';
161159

162-
return $verb[ $method_name ] . ' ' . $singular . ' ' . $title;
160+
return $verb . ' ' . $determiner . ' ' . $title;
163161
}
164162

165163
protected function rest_callable( $inputs, $route, $method_name, $server ) {

src/RouteInformation.php

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

33
declare(strict_types=1);
44

5+
namespace WP_CLI\AiCommand;
6+
7+
use BadMethodCallException;
8+
use WP_REST_Controller;
9+
use WP_REST_Posts_Controller;
10+
use WP_REST_Taxonomies_Controller;
11+
use WP_REST_Users_Controller;
12+
513
class RouteInformation
614
{
715

@@ -12,29 +20,42 @@ public function __construct(
1220
) {
1321
}
1422

23+
public function get_sanitized_route_name(): string
24+
{
25+
$route = $this->route;
26+
27+
preg_match_all('/\(?P<(\w+)>/', $this->route, $matches);
28+
29+
foreach ($matches[1] as $match) {
30+
$route = preg_replace('/(\(\?P<' . $match . '>.*\))/', 'p_' . $match, $route, 1);
31+
}
32+
33+
return $this->method . '_' . sanitize_title( $route );
34+
}
35+
1536
public function get_method(): string
1637
{
1738
return $this->method;
1839
}
1940

20-
public function is_post(): bool
41+
public function is_create(): bool
2142
{
2243
return $this->method === 'POST';
2344
}
2445

25-
public function is_put(): bool
46+
public function is_update(): bool
2647
{
27-
return $this->method === 'PUT';
48+
return $this->method === 'PUT' || $this->method === 'PATCH';
2849
}
2950

3051
public function is_delete(): bool
3152
{
3253
return $this->method === 'DELETE';
3354
}
3455

35-
public function is_put(): bool
56+
public function is_get(): bool
3657
{
37-
return $this->method === 'PUT';
58+
return $this->method === 'GET';
3859
}
3960

4061
public function is_singular(): bool
@@ -57,7 +78,7 @@ public function is_list(): bool
5778
return ! $this->is_singular();
5879
}
5980

60-
public function is_wp_rest_controller()
81+
public function is_wp_rest_controller(): bool
6182
{
6283
$allowed = [
6384
WP_REST_Posts_Controller::class,
@@ -74,4 +95,13 @@ public function is_wp_rest_controller()
7495
return false;
7596
}
7697

98+
public function get_wp_rest_controller(): WP_REST_Controller
99+
{
100+
if ( ! $this->is_wp_rest_controller()) {
101+
throw new BadMethodCallException('The callback needs to be a WP_Rest_Controller');
102+
}
103+
104+
return $this->callback[0];
105+
}
106+
77107
}

0 commit comments

Comments
 (0)