Skip to content

Commit 30401e5

Browse files
committed
Docs for updated Server class, and new RouteInformation and MapRESTtoMCP classes.
1 parent b87c37c commit 30401e5

File tree

1 file changed

+78
-10
lines changed

1 file changed

+78
-10
lines changed

docs/reference.md

Lines changed: 78 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,15 @@ This class acts as the server component in the MCP architecture, interfacing wit
8585
| `Server::register_resource()` | Registers structured data for AI access. |
8686
| `Server::get_capabilities()` | Retrives server capabilities and returns the list of available tools and resources. Used by AI clients to understand what functions and data are accessible. |
8787
| `Server::handle_request()` | Parses JSON-RPC 2.0 requests. Validates structure and executes method calls. |
88-
| `Server::process_method_call()` | Determines whether the request is for fetching capabilities (`get_capabilities`), accessing data (`get_users`, `get_products`), or executing a tool (`calculate_total`, `greet`, etc.) |
89-
| `Server::handle_data_request()` | Extracts requested resource and returns structured data. |
90-
| `Server::execute_tool()` | Calls registered AI tools and validates input against schema. |
88+
| `Server::list_resources()` | List registered resources. |
89+
| `Server::read_resource()` | Retrieve resource data. Uses `get_resource_data()` method. |
90+
| `Server::get_resource_data()` | Retrieve resource data. |
91+
| `Server::validate_input()` | Input validation for AI tool calls. |
92+
| `Server::handle_get_request()` | Retrive stored resource data. |
9193
| `Server::create_success_response()` | Generates JSON-RPC success response. |
9294
| `Server::create_error_response()` | Generates JSON-RPC error response. |
95+
| `Server::process_request()` | Wrapper for `handle_request()` method. |
96+
9397

9498
### Examples
9599

@@ -115,11 +119,75 @@ $server->register_tool(
115119
Register a Resource
116120

117121
```PHP
118-
$server->register_resource([
119-
'name' => 'product_catalog',
120-
'uri' => 'file://./products.json',
121-
'description' => 'Product catalog',
122-
'mimeType' => 'application/json',
123-
'filePath' => './products.json'
124-
]);
122+
$server->register_resource(
123+
[
124+
'name' => 'product_catalog',
125+
'uri' => 'file://./products.json',
126+
'description' => 'Product catalog',
127+
'mimeType' => 'application/json',
128+
'filePath' => './products.json'
129+
]
130+
);
131+
```
132+
133+
List resources
134+
135+
```PHP
136+
$server = new WP_CLI\AiCommand\MCP\Server();
137+
$resources = $server->list_resources();
138+
139+
echo json_encode( $resources, JSON_PRETTY_PRINT );
140+
```
141+
142+
Read resource
143+
144+
```PHP
145+
$server = new WP_CLI\AiCommand\MCP\Server();
146+
$resource_data = $server->read_resource( 'file://./products.json' );
147+
148+
echo json_encode( $resource_data, JSON_PRETTY_PRINT );
149+
```
150+
151+
Validate input
152+
153+
```PHP
154+
$input = [ 'price' => 100, 'quantity' => 2 ];
155+
$schema = $server->get_capabilities()['methods'][0]['inputSchema'];
156+
157+
$result = $server->validate_input( $input, $schema );
125158
```
159+
160+
## `RouteInformation` class
161+
162+
The `RouteInformation` class encapsulates details about a WordPress REST API route, including its method type (`GET`, `POST`, `PUT`, etc.), callback function, and whether it conforms to a `WP_REST_Controller`. The class provides helper methods for route sanitization, method checking, and controller validation.
163+
164+
### Methods
165+
166+
| Name | Return Type | Description |
167+
| --- | --- | --- |
168+
| `RouteInformation::get_sanitized_route_name()` | `string` | Returns a cleaned-up route name (e.g., GET_wp-v2-posts_p_id). |
169+
| `RouteInformation::get_method()` | `string` | Returns the HTTP method (`GET`, `POST`, etc.). |
170+
| `RouteInformation::is_create()` | `bool` | Returns `true` if the method is `POST`. |
171+
| `RouteInformation::is_update()` | `bool` | Returns `true` if the method is `PUT` or `PATCH`. |
172+
| `RouteInformation::is_delete()` | `bool` | Returns `true` if the method is `DELETE`. |
173+
| `RouteInformation::is_get()` | `bool` | Returns `true` if the method is `GET`. |
174+
| `RouteInformation::is_singular()` | `bool` | Returns `true` if the route targets a single resource. |
175+
| `RouteInformation::is_list()` | `bool` | Returns `true` if the route retrieves multiple resources. |
176+
| `RouteInformation::is_wp_rest_controller()` | `bool` | Returns `true` if the callback is a valid REST controller. |
177+
| `RouteInformation::get_wp_rest_controller()` | `WP_REST_Controller` | Returns the controller instance (throws an error if invalid). |
178+
179+
## `MapRESTtoMCP` class
180+
181+
The `MapRESTtoMCP` class is responsible for mapping WordPress REST API endpoints into MCP tools. It dynamically registers REST API routes as AI-callable tools in the MCP system.
182+
183+
This class enables AI-driven automation by exposing WordPress REST API endpoints to AI services in WP-CLI.
184+
185+
### Methods
186+
187+
| Name | Return Type | Description |
188+
| --- | --- | --- |
189+
| `MapRESTtoMCP::args_to_schema()` | `array` | Converts REST API arguments into JSON Schema. |
190+
| `MapRESTtoMCP::sanitize_type()` | `string` | Maps REST API types to standardized types. |
191+
| `MapRESTtoMCP::map_rest_to_mcp()` | `void` | Registers REST API endpoints as AI tools in MCP. |
192+
| `MapRESTtoMCP::generate_description()` | `string` | Creates human-readable descriptions for API tools. |
193+
| `MapRESTtoMCP::rest_callable()` | `array` | Executes REST API calls and returns formatted data. |

0 commit comments

Comments
 (0)