|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace WP_CLI\AiCommand; |
| 4 | + |
| 5 | +use WP_CLI; |
| 6 | +use WP_CLI\AiCommand\MCP\Server; |
| 7 | +use WP_REST_Request; |
| 8 | + |
| 9 | + |
| 10 | +class MapRESTtoMCP { |
| 11 | + |
| 12 | + public function args_to_schema( $args = [] ) { |
| 13 | + $schema = []; |
| 14 | + $required = []; |
| 15 | + |
| 16 | + if ( empty( $args ) ) { |
| 17 | + return []; |
| 18 | + } |
| 19 | + |
| 20 | + foreach ( $args as $title => $arg ) { |
| 21 | + $description = $arg['description'] ?? $title; |
| 22 | + $type = $this->sanitize_type( $arg['type'] ?? 'string' ); |
| 23 | + |
| 24 | + $schema[ $title ] = [ |
| 25 | + 'type' => $type, |
| 26 | + 'description' => $description, |
| 27 | + ]; |
| 28 | + if ( isset( $arg['required'] ) && $arg['required'] ) { |
| 29 | + $required[] = $title; |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + return [ |
| 34 | + 'type' => 'object', |
| 35 | + 'properties' => $schema, |
| 36 | + 'required' => $required, |
| 37 | + ]; |
| 38 | + } |
| 39 | + |
| 40 | + protected function sanitize_type( $type) { |
| 41 | + |
| 42 | + $mapping = array( |
| 43 | + 'string' => 'string', |
| 44 | + 'integer' => 'integer', |
| 45 | + 'number' => 'integer', |
| 46 | + 'boolean' => 'boolean', |
| 47 | + ); |
| 48 | + |
| 49 | + // Validated types: |
| 50 | + if ( !\is_array($type) && isset($mapping[ $type ]) ) { |
| 51 | + return $mapping[ $type ]; |
| 52 | + } |
| 53 | + |
| 54 | + if ( $type === 'array' || $type === 'object' ) { |
| 55 | + return 'string'; // TODO, better solution. |
| 56 | + } |
| 57 | + if (empty( $type ) || $type === 'null' ) { |
| 58 | + return 'string'; |
| 59 | + } |
| 60 | + |
| 61 | + if ( !\is_array( $type ) ) { |
| 62 | + throw new \Exception( 'Invalid type: ' . $type ); |
| 63 | + return 'string'; |
| 64 | + } |
| 65 | + |
| 66 | + // Find valid values in array. |
| 67 | + if ( \in_array( 'string', $type ) ) { |
| 68 | + return 'string'; |
| 69 | + } |
| 70 | + if ( \in_array( 'integer', $type ) ) { |
| 71 | + return 'integer'; |
| 72 | + } |
| 73 | + // TODO, better types handling. |
| 74 | + return 'string'; |
| 75 | + |
| 76 | + } |
| 77 | + |
| 78 | + public function map_rest_to_mcp( Server $mcp_server ) { |
| 79 | + $server = rest_get_server(); |
| 80 | + $routes = $server->get_routes(); |
| 81 | + |
| 82 | + foreach ( $routes as $route => $endpoints ) { |
| 83 | + foreach ( $endpoints as $endpoint ) { |
| 84 | + foreach( $endpoint['methods'] as $method_name => $enabled ) { |
| 85 | + $information = new RouteInformation( |
| 86 | + $route, |
| 87 | + $method_name, |
| 88 | + $endpoint['callback'], |
| 89 | + ); |
| 90 | + |
| 91 | + if ( ! $information->is_wp_rest_controller() ) { |
| 92 | + continue; |
| 93 | + } |
| 94 | + |
| 95 | + $tool = [ |
| 96 | + 'name' => $information->get_sanitized_route_name(), |
| 97 | + 'description' => $this->generate_description( $information ), |
| 98 | + 'inputSchema' => $this->args_to_schema( $endpoint['args'] ), |
| 99 | + 'callable' => function ( $inputs ) use ( $route, $method_name, $server ){ |
| 100 | + return $this->rest_callable( $inputs, $route, $method_name, $server ); |
| 101 | + }, |
| 102 | + ]; |
| 103 | + |
| 104 | + $mcp_server->register_tool($tool); |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Create description based on route and method. |
| 112 | + * |
| 113 | + * |
| 114 | + * Get a list of posts GET /wp/v2/posts |
| 115 | + * Get post with id GET /wp/v2/posts/(?P<id>[\d]+) |
| 116 | + */ |
| 117 | + protected function generate_description( RouteInformation $information ) : string { |
| 118 | + |
| 119 | + $verb = match ($information->get_method()) { |
| 120 | + 'GET' => 'Get', |
| 121 | + 'POST' => 'Create', |
| 122 | + 'PUT', 'PATCH' => 'Update', |
| 123 | + 'DELETE' => 'Delete', |
| 124 | + }; |
| 125 | + |
| 126 | + $schema = $information->get_wp_rest_controller()->get_public_item_schema(); |
| 127 | + $title = $schema['title']; |
| 128 | + |
| 129 | + $determiner = $information->is_singular() |
| 130 | + ? 'a' |
| 131 | + : 'list of'; |
| 132 | + |
| 133 | + return $verb . ' ' . $determiner . ' ' . $title; |
| 134 | + } |
| 135 | + |
| 136 | + protected function rest_callable( $inputs, $route, $method_name, \WP_REST_Server $server ) { |
| 137 | + preg_match_all( '/\(?P<(\w+)>/', $route, $matches ); |
| 138 | + |
| 139 | + foreach( $matches[1] as $match ) { |
| 140 | + if ( array_key_exists( $match, $inputs ) ) { |
| 141 | + $route = preg_replace( '/(\(\?P<'.$match.'>.*?\))/', $inputs[$match], $route, 1 ); |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + WP_CLI::debug( 'Rest Route: ' . $route . ' ' . $method_name, 'mcp_server' ); |
| 146 | + |
| 147 | + foreach( $inputs as $key => $value ) { |
| 148 | + WP_CLI::debug( ' param->' . $key . ' : ' . $value, 'mcp_server' ); |
| 149 | + } |
| 150 | + |
| 151 | + $request = new WP_REST_Request( $method_name, $route ); |
| 152 | + $request->set_body_params( $inputs ); |
| 153 | + |
| 154 | + /** |
| 155 | + * @var WP_REST_Response $response |
| 156 | + */ |
| 157 | + $response = $server->dispatch( $request ); |
| 158 | + |
| 159 | + $data = $server->response_to_data( $response, false ); |
| 160 | + |
| 161 | + if( isset( $data[0]['slug'] ) ) { |
| 162 | + $debug_data = 'Result List: '; |
| 163 | + foreach ( $data as $item ) { |
| 164 | + $debug_data .= $item['id'] . '=>' . $item['slug'] . ', '; |
| 165 | + } |
| 166 | + } elseif( isset( $data['slug'] ) ) { |
| 167 | + $debug_data = 'Result: ' . $data['id'] . ' ' . $data['slug']; |
| 168 | + } else { |
| 169 | + $debug_data = 'Unknown format'; |
| 170 | + } |
| 171 | + WP_CLI::debug( $debug_data, 'mcp_server' ); |
| 172 | + |
| 173 | + return $data; |
| 174 | + } |
| 175 | +} |
0 commit comments