Skip to content

Commit b381915

Browse files
Merge pull request #47 from Automattic/bugfix/rest-api-alias
Bugfix/rest api alias
2 parents 75d4f49 + 7f08c3d commit b381915

File tree

4 files changed

+71
-50
lines changed

4 files changed

+71
-50
lines changed

includes/Core/RegisterMcpTool.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ private function get_args_from_rest_api(): void {
6868
// maybe use: rest_get_server()->get_route_options( $route ) );.
6969
$rest_route = $routes[ $route ] ?? null;
7070
if ( ! $rest_route ) {
71-
// translators: %s: Route.
72-
throw new InvalidArgumentException( sprintf( esc_html__( 'The route %1$s with method %2$s does not exist.', 'wordpress-mcp' ), $route, $method ) ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
71+
// Skip registration if the route doesn't exist.
72+
return;
7373
}
7474

7575
$rest_api = null;

includes/Tools/McpRestApiCrud.php

Lines changed: 64 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,18 @@ public function register_tools(): void {
4545

4646
new RegisterMcpTool(
4747
array(
48-
'name' => 'list_wordpress_rest_api_endpoints',
49-
'description' => 'List all available WordPress REST API endpoints and their supported HTTP methods. Use this first to discover what API endpoints are available before making specific calls.',
50-
'type' => 'read',
51-
'inputSchema' => array(
48+
'name' => 'list_api_functions',
49+
'description' => 'List all available WordPress REST API endpoints that support CRUD operations (Create, Read, Update, Delete). Use this first to discover what API functions are available before inspecting or calling them.',
50+
'type' => 'read',
51+
'inputSchema' => array(
5252
'type' => 'object',
5353
'properties' => new \stdClass(),
5454
'required' => new \stdClass(),
5555
),
56-
'callback' => array( $this, 'get_available_tools' ),
57-
'permission_callback' => '__return_true',
58-
'annotations' => array(
59-
'title' => 'List REST API Endpoints',
56+
'callback' => array( $this, 'get_available_tools' ),
57+
'permission_callback' => '__return_true',
58+
'annotations' => array(
59+
'title' => 'List API Functions',
6060
'readOnlyHint' => true,
6161
'openWorldHint' => false,
6262
),
@@ -65,28 +65,28 @@ public function register_tools(): void {
6565

6666
new RegisterMcpTool(
6767
array(
68-
'name' => 'get_wordpress_rest_api_endpoint_schema',
69-
'description' => 'Get the complete schema and documentation for a specific WordPress REST API endpoint and HTTP method. Use this to understand what parameters are required and available for an endpoint before making calls.',
70-
'type' => 'read',
71-
'inputSchema' => array(
68+
'name' => 'get_function_details',
69+
'description' => 'Get detailed metadata for a specific WordPress REST API endpoint and HTTP method. Includes available parameters, required fields, authentication needs, and expected response structure. Use this to get the details of a specific function before calling it.',
70+
'type' => 'read',
71+
'inputSchema' => array(
7272
'type' => 'object',
7373
'properties' => array(
74-
'route' => array(
75-
'type' => 'string',
76-
'description' => 'The REST API route (e.g., "/wp/v2/posts", "/wp/v2/users")'
74+
'route' => array(
75+
'type' => 'string',
76+
'description' => 'The REST API route (e.g., "/wp/v2/posts", "/wp/v2/users")',
7777
),
7878
'method' => array(
79-
'type' => 'string',
80-
'enum' => array( 'GET', 'POST', 'PATCH', 'DELETE' ),
81-
'description' => 'The HTTP method to get schema for'
79+
'type' => 'string',
80+
'enum' => array( 'GET', 'POST', 'PATCH', 'DELETE' ),
81+
'description' => 'The HTTP method to retrieve metadata for',
8282
),
8383
),
8484
'required' => array( 'route', 'method' ),
8585
),
86-
'callback' => array( $this, 'get_tool_details' ),
87-
'permission_callback' => '__return_true',
88-
'annotations' => array(
89-
'title' => 'Get Endpoint Schema',
86+
'callback' => array( $this, 'get_tool_details' ),
87+
'permission_callback' => '__return_true',
88+
'annotations' => array(
89+
'title' => 'Get Function Details',
9090
'readOnlyHint' => true,
9191
'openWorldHint' => false,
9292
),
@@ -95,32 +95,32 @@ public function register_tools(): void {
9595

9696
new RegisterMcpTool(
9797
array(
98-
'name' => 'call_wordpress_rest_api',
99-
'description' => 'Make a direct call to any WordPress REST API endpoint. Supports GET (read), POST (create), PATCH (update), and DELETE operations. Use this to interact with WordPress content like posts, pages, users, etc.',
100-
'type' => 'action',
101-
'inputSchema' => array(
98+
'name' => 'run_api_function',
99+
'description' => 'Execute a specific WordPress REST API function by providing the endpoint route, HTTP method, and any required parameters or request body. Supports standard CRUD operations: GET (read), POST (create), PATCH (update), DELETE (remove).',
100+
'type' => 'action',
101+
'inputSchema' => array(
102102
'type' => 'object',
103103
'properties' => array(
104-
'route' => array(
105-
'type' => 'string',
106-
'description' => 'The REST API route (e.g., "/wp/v2/posts", "/wp/v2/users/123")'
104+
'route' => array(
105+
'type' => 'string',
106+
'description' => 'The REST API route (e.g., "/wp/v2/posts", "/wp/v2/users/123")',
107107
),
108108
'method' => array(
109-
'type' => 'string',
110-
'enum' => array( 'GET', 'POST', 'PATCH', 'DELETE' ),
111-
'description' => 'The HTTP method: GET (read), POST (create), PATCH (update), DELETE (remove)'
109+
'type' => 'string',
110+
'enum' => array( 'GET', 'POST', 'PATCH', 'DELETE' ),
111+
'description' => 'The HTTP method to use: GET, POST, PATCH, or DELETE',
112112
),
113-
'data' => array(
114-
'type' => 'object',
115-
'description' => 'Request body data for POST/PATCH requests. Not needed for GET/DELETE.'
113+
'data' => array(
114+
'type' => 'object',
115+
'description' => 'Payload for POST or PATCH requests. Not required for GET or DELETE.',
116116
),
117117
),
118118
'required' => array( 'route', 'method' ),
119119
),
120-
'callback' => array( $this, 'handle_tool_run_request' ),
121-
'permission_callback' => '__return_true',
122-
'annotations' => array(
123-
'title' => 'Call REST API',
120+
'callback' => array( $this, 'handle_tool_run_request' ),
121+
'permission_callback' => '__return_true',
122+
'annotations' => array(
123+
'title' => 'Run API Function',
124124
'readOnlyHint' => false,
125125
'destructiveHint' => true,
126126
'idempotentHint' => false,
@@ -141,10 +141,10 @@ public function handle_tool_run_request( array $data ): array {
141141
$method = $data['method'];
142142
$data = $data['data'];
143143

144-
// Get settings to check if operations are enabled
144+
// Get settings to check if operations are enabled.
145145
$settings = get_option( 'wordpress_mcp_settings', array() );
146146

147-
// Check if the method is allowed based on settings
147+
// Check if the method is allowed based on settings.
148148
switch ( $method ) {
149149
case 'DELETE':
150150
if ( empty( $settings['enable_delete_tools'] ) ) {
@@ -185,10 +185,30 @@ public function handle_tool_run_request( array $data ): array {
185185
* @return array The routes and methods.
186186
*/
187187
public function get_available_tools(): array {
188-
// content.text.result[key]
189-
// get all routes and methods from the WordPress rest api.
188+
$exact_ignore_routes = array(
189+
'/',
190+
'/batch/v1',
191+
);
192+
$containing_ignore_strings = array(
193+
'oembed',
194+
'autosaves',
195+
'revisions',
196+
'jwt-auth',
197+
);
198+
// Get all routes and methods from the WordPress REST API.
190199
$routes = rest_get_server()->get_routes();
200+
$result = array();
191201
foreach ( $routes as $route => $methods ) {
202+
// Skip if route exactly matches any ignore route.
203+
if ( in_array( $route, $exact_ignore_routes, true ) ) {
204+
continue;
205+
}
206+
// Skip if route contains any of the ignore strings.
207+
foreach ( $containing_ignore_strings as $ignore_string ) {
208+
if ( strpos( $route, $ignore_string ) !== false ) {
209+
continue 2;
210+
}
211+
}
192212
foreach ( $methods as $the_methods ) {
193213
$result[] = array(
194214
'route' => $route,
@@ -219,4 +239,4 @@ public function get_tool_details( array $data ): array {
219239
}
220240
return array();
221241
}
222-
}
242+
}

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "wordpress-mcp",
3-
"version": "0.2.1",
3+
"version": "0.2.2",
44
"description": "A plugin to integrate WordPress with Model Context Protocol (MCP), providing AI-accessible interfaces to WordPress data and functionality through standardized tools, resources, and prompts. Enables AI assistants to interact with posts, users, site settings, and WooCommerce data.",
55
"keywords": [
66
"wordpress",
@@ -17,7 +17,8 @@
1717
"build",
1818
"includes",
1919
"vendor",
20-
"wordpress-mcp.php"
20+
"wordpress-mcp.php",
21+
"docs"
2122
],
2223
"scripts": {
2324
"build": "wp-scripts build",

wordpress-mcp.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
/**
33
* Plugin name: WordPress MCP
44
* Description: A plugin to integrate WordPress with Model Context Protocol (MCP), providing AI-accessible interfaces to WordPress data and functionality through standardized tools, resources, and prompts. Enables AI assistants to interact with posts, users, site settings, and WooCommerce data.
5-
* Version: 0.2.1
5+
* Version: 0.2.2
66
* Requires at least: 6.4
77
* Requires PHP: 8.0
88
* Author: Automattic AI, Ovidiu Galatan <[email protected]>
9-
* Author URI: https://automattic.ai
9+
* Author URI: https://automattic.com
1010
* License: GPL-2.0-or-later
1111
* License URI: https://spdx.org/licenses/GPL-2.0-or-later.html
1212
* Text Domain: wordpress-mcp

0 commit comments

Comments
 (0)