mcp.conf
1 year ago
mcp.js
1 year ago
mcp.md
1 year ago
mcp.php
1 year ago
mcp_core.php
1 year ago
mcp_rest.php
1 year ago
realtime.php
1 year ago
mcp_rest.php
243 lines
| 1 | <?php |
| 2 | |
| 3 | class Meow_MWAI_Labs_MCP_Rest { |
| 4 | private $cache_key = 'mwai_mcp_tools_cache'; |
| 5 | private $allowed = [ 'posts', 'pages', 'media' ]; |
| 6 | |
| 7 | public function __construct() { |
| 8 | add_action( 'rest_api_init', [ $this, 'rest_api_init' ] ); |
| 9 | } |
| 10 | |
| 11 | public function rest_api_init() { |
| 12 | add_filter( 'mwai_mcp_tools', [ $this, 'register_rest_tools' ] ); |
| 13 | add_filter( 'mwai_mcp_callback', [ $this, 'handle_call' ], 10, 4 ); |
| 14 | } |
| 15 | |
| 16 | public function register_rest_tools( $prevTools ) { |
| 17 | $cached = get_transient( $this->cache_key ); |
| 18 | |
| 19 | if ( !$cached ) { |
| 20 | $tools = []; |
| 21 | $server = rest_get_server(); |
| 22 | $routes = $server->get_routes(); |
| 23 | |
| 24 | foreach ( $this->allowed as $resource ) { |
| 25 | $base = "/wp/v2/{$resource}"; |
| 26 | $item = "{$base}/(?P<id>[\d]+)"; |
| 27 | |
| 28 | if ( isset( $routes[ $base ] ) ) { |
| 29 | foreach ( $routes[ $base ] as $endpoint ) { |
| 30 | if ( ! empty( $endpoint['methods']['GET'] ) ) { |
| 31 | $tools[ "list_{$resource}" ] = [ |
| 32 | 'name' => "list_{$resource}", |
| 33 | 'description' => "List {$resource}", |
| 34 | 'inputSchema' => $this->build_schema_from_args( $endpoint['args'] ), |
| 35 | 'outputSchema' => $this->build_output_schema(), |
| 36 | ]; |
| 37 | break; |
| 38 | } |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | if ( isset( $routes[ $item ] ) ) { |
| 43 | foreach ( $routes[ $item ] as $endpoint ) { |
| 44 | if ( ! empty( $endpoint['methods']['GET'] ) ) { |
| 45 | $tools[ "get_{$resource}" ] = [ |
| 46 | 'name' => "get_{$resource}", |
| 47 | 'description' => "Get single {$resource} by ID", |
| 48 | 'inputSchema' => $this->build_schema_from_args( $endpoint['args'] ), |
| 49 | 'outputSchema' => $this->build_output_schema(), |
| 50 | ]; |
| 51 | break; |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | if ( isset( $routes[ $base ] ) ) { |
| 57 | foreach ( $routes[ $base ] as $endpoint ) { |
| 58 | if ( ! empty( $endpoint['methods']['POST'] ) ) { |
| 59 | $tools[ "create_{$resource}" ] = [ |
| 60 | 'name' => "create_{$resource}", |
| 61 | 'description' => "Create {$resource}", |
| 62 | 'inputSchema' => $this->build_schema_from_args( $endpoint['args'] ), |
| 63 | 'outputSchema' => $this->build_output_schema(), |
| 64 | ]; |
| 65 | break; |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | if ( isset( $routes[ $item ] ) ) { |
| 71 | foreach ( $routes[ $item ] as $endpoint ) { |
| 72 | $methods = array_keys( $endpoint['methods'] ); |
| 73 | if ( array_intersect( [ 'POST', 'PUT', 'PATCH' ], $methods ) ) { |
| 74 | $tools[ "update_{$resource}" ] = [ |
| 75 | 'name' => "update_{$resource}", |
| 76 | 'description' => "Update {$resource}", |
| 77 | 'inputSchema' => $this->build_schema_from_args( $endpoint['args'] ), |
| 78 | 'outputSchema' => $this->build_output_schema(), |
| 79 | ]; |
| 80 | break; |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | if ( isset( $routes[ $item ] ) ) { |
| 86 | foreach ( $routes[ $item ] as $endpoint ) { |
| 87 | if ( ! empty( $endpoint['methods']['DELETE'] ) ) { |
| 88 | $tools[ "delete_{$resource}" ] = [ |
| 89 | 'name' => "delete_{$resource}", |
| 90 | 'description' => "Delete {$resource}", |
| 91 | 'inputSchema' => $this->build_schema_from_args( $endpoint['args'] ), |
| 92 | 'outputSchema' => $this->build_output_schema(), |
| 93 | ]; |
| 94 | break; |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | set_transient( $this->cache_key, $tools, DAY_IN_SECONDS ); |
| 101 | $cached = $tools; |
| 102 | } |
| 103 | |
| 104 | return array_merge( array_values( $cached ), $prevTools ); |
| 105 | } |
| 106 | |
| 107 | private function build_schema_from_args( $args ) { |
| 108 | $schema = [ |
| 109 | 'type' => 'object', |
| 110 | 'properties' => [], |
| 111 | 'required' => [], |
| 112 | ]; |
| 113 | |
| 114 | foreach ( $args as $name => $def ) { |
| 115 | $schema['properties'][ $name ] = [ |
| 116 | 'type' => $def['type'] ?? 'string', |
| 117 | 'description' => $def['description'] ?? '', |
| 118 | ]; |
| 119 | |
| 120 | if ( ! empty( $def['required'] ) ) { |
| 121 | $schema['required'][] = $name; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | return $schema; |
| 126 | } |
| 127 | |
| 128 | private function build_output_schema() { |
| 129 | return [ |
| 130 | 'type' => 'object', |
| 131 | 'properties' => [ |
| 132 | 'content' => [ |
| 133 | 'type' => 'array', |
| 134 | 'items' => [ |
| 135 | 'type' => 'object', |
| 136 | 'properties' => [ |
| 137 | 'type' => [ |
| 138 | 'type' => 'string', |
| 139 | 'description' => 'Block type, e.g. text or image', |
| 140 | ], |
| 141 | 'text' => [ |
| 142 | 'type' => 'string', |
| 143 | 'description' => 'Human-readable content', |
| 144 | ], |
| 145 | ], |
| 146 | 'required' => [ 'type', 'text' ], |
| 147 | ], |
| 148 | ], |
| 149 | ], |
| 150 | 'required' => [ 'content' ], |
| 151 | ]; |
| 152 | } |
| 153 | |
| 154 | public function handle_call( $existing, $tool, $args, $id ) { |
| 155 | if ( !empty( $existing ) ) { |
| 156 | return $existing; |
| 157 | } |
| 158 | |
| 159 | $tools = get_transient( $this->cache_key ); |
| 160 | if ( ! isset( $tools[ $tool ] ) ) { |
| 161 | return $existing; |
| 162 | } |
| 163 | |
| 164 | $current_user = wp_get_current_user(); |
| 165 | if ( ! $current_user->has_cap( 'administrator' ) ) { |
| 166 | wp_set_current_user( 1 ); |
| 167 | } |
| 168 | |
| 169 | list( $action, $resource ) = explode( '_', $tool, 2 ); |
| 170 | $path = "/wp/v2/{$resource}"; |
| 171 | $method = 'GET'; |
| 172 | |
| 173 | if ( in_array( $action, [ 'get', 'update', 'delete' ], true ) ) { |
| 174 | if ( empty( $args['id'] ) ) { |
| 175 | return [ |
| 176 | 'jsonrpc' => '2.0', |
| 177 | 'id' => $id, |
| 178 | 'error' => [ |
| 179 | 'code' => -32602, |
| 180 | 'message' => 'Missing parameter: id', |
| 181 | ], |
| 182 | ]; |
| 183 | } |
| 184 | $path .= '/' . intval( $args['id'] ); |
| 185 | } |
| 186 | |
| 187 | switch ( $action ) { |
| 188 | case 'create': |
| 189 | case 'update': |
| 190 | $method = 'POST'; |
| 191 | break; |
| 192 | case 'delete': |
| 193 | $method = 'DELETE'; |
| 194 | break; |
| 195 | default: |
| 196 | $method = 'GET'; |
| 197 | break; |
| 198 | } |
| 199 | |
| 200 | $request = new WP_REST_Request( $method, $path ); |
| 201 | |
| 202 | if ( $method === 'GET' ) { |
| 203 | foreach ( $args as $key => $value ) { |
| 204 | $request->set_param( $key, $value ); |
| 205 | } |
| 206 | } |
| 207 | else { |
| 208 | $request->set_body_params( $args ); |
| 209 | } |
| 210 | |
| 211 | $response = rest_do_request( $request ); |
| 212 | $reply = [ 'jsonrpc' => '2.0', 'id' => $id ]; |
| 213 | |
| 214 | if ( is_wp_error( $response ) || $response->get_status() >= 400 ) { |
| 215 | $error_obj = is_wp_error( $response ) ? $response : $response->as_error(); |
| 216 | |
| 217 | return [ |
| 218 | 'jsonrpc' => '2.0', |
| 219 | 'id' => $id, |
| 220 | 'error' => [ |
| 221 | 'code' => (int) ( $error_obj->get_error_code() ?: $response->get_status() ), |
| 222 | 'message' => $error_obj->get_error_message(), |
| 223 | 'data' => $error_obj->get_error_data() ?: null, |
| 224 | ], |
| 225 | ]; |
| 226 | } |
| 227 | |
| 228 | $data = $response->get_data(); |
| 229 | |
| 230 | $reply['result'] = [ |
| 231 | 'content' => [ |
| 232 | [ |
| 233 | 'type' => 'text', |
| 234 | 'text' => wp_json_encode( $data, JSON_PRETTY_PRINT ), |
| 235 | ], |
| 236 | ], |
| 237 | 'data' => $data, |
| 238 | ]; |
| 239 | |
| 240 | return $reply; |
| 241 | } |
| 242 | } |
| 243 |