Skip to content

Commit 9cad79e

Browse files
committed
Update reference for all existing docs.
1 parent c624bb6 commit 9cad79e

File tree

1 file changed

+62
-54
lines changed

1 file changed

+62
-54
lines changed

docs/reference.md

Lines changed: 62 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,11 @@
1111

1212
## `AiCommand` class
1313

14-
The `AiCommand` class extends `WP_CLI_Command` to integrate AI-powered automation into WordPress through WP-CLI. It enables users to interact with an AI model by:
14+
The `AiCommand` class registers CLI command for WP-CLI. It is a WP-CLI command handler designed to integrate AI capabilities within WordPress. It connects AI-driven services with WP-CLI using the MCP (Multi-Client Processor) architecture, allowing AI models to:
1515

16-
- Executing AI-driven prompts.
17-
- Registering [tools](tools.md) that can be invoked by AI (e.g., calculations, greetings, image generation).
18-
- Registering resources accessible by AI (e.g., user lists, product catalogs).
19-
- Fetching [WordPress community events](https://developer.wordpress.org/reference/classes/wp_community_events/) dynamically.
16+
- Access resources (e.g., WordPress posts, users, products).
17+
- Call AI-powered [tools](tools.md) (e.g., image generation, event lookup).
18+
- Execute AI-generated functions dynamically.
2019

2120
This class follows an MCP client-server architecture, where:
2221

@@ -31,20 +30,17 @@ This class follows an MCP client-server architecture, where:
3130
| `AiCommand::__invoke()` | `void` | Executes AI-driven WP-CLI commands. |
3231
| `AiCommand::register_tools( $server )` | `void` | Registers AI-powered tools in MCP. See [available tools](tools.md). |
3332
| `AiCommand::register_resources( $server )` | `void` | Registers data resources (e.g., users, products). |
34-
| `AiCommand::list_tools()` | `string` | Lists available AI tools. |
35-
| `AiCommand::fetch_wp_community_events( $params )` | `array` | Retrieves WordPress community events. |
3633

3734
## `Client` class
3835

39-
`Client` class is part of the `WP_CLI\AiCommand\MCP` namespace and serves as an intermediary between the WP-CLI and an AI-based service. It interacts with an MCP (Multi-Client Processor) AI service using JSON-RPC over a local server instance.
36+
The `Client` class acts as the AI service interface for the MCP (Multi-Client Processor) system within WP-CLI. It communicates with the `Server` class via JSON-RPC, enabling AI-powered text generation, function calls, and image generation.
4037

41-
The service supports:
38+
This class supports:
4239

43-
- Sending and receiving requests from the AI service.
44-
- Retrieving resources from the AI service.
45-
- Generating images via AI.
46-
- Processing AI-generated text responses.
47-
- Managing function calls and executing AI-invoked functions dynamically.
40+
- AI service interaction (via `call_ai_service()`).
41+
- Function execution and AI response handling.
42+
- Dynamic REST resource access (`list_resources()`, `read_resource()`).
43+
- Image generation via AI models (`get_image_from_ai_service()`).
4844

4945
### Properties
5046

@@ -67,14 +63,14 @@ The service supports:
6763

6864
## `Server` class
6965

70-
The Server class serves as the core backend for the AI Command's MCP (Multi-Client Processor) in WP-CLI. It provides:
66+
The Server class is a core component of MCP (Multi-Client Processor) within WP-CLI. It acts as a JSON-RPC 2.0 server, handling data requests, tool registrations, and AI-driven function execution.
7167

72-
- Tool Registration - Defines AI-callable functions (e.g., calculations, event fetching).
73-
- Resource Management - Stores and retrieves structured data (e.g., users, products).
74-
- JSON-RPC Request Handling - Processes incoming requests and returns AI-usable responses.
75-
- Validation & Error Handling - Ensures correct data formats and secure execution.
68+
This class provides:
7669

77-
This class acts as the server component in the MCP architecture, interfacing with AI clients to process requests. It follows a JSON-RPC 2.0 protocol, ensuring a standardized communication format.
70+
- Tool registration (`register_tool()`)
71+
- Resource registration (`register_resource()`)
72+
- JSON-RPC request handling (`handle_request()`)
73+
- AI service capabilities retrieval (`get_capabilities()`)
7874

7975
### Properties
8076

@@ -86,22 +82,21 @@ This class acts as the server component in the MCP architecture, interfacing wit
8682

8783
### Methods
8884

89-
| Name | Description |
90-
| --- | --- |
91-
| `Server::__construct()` | Constructor. Initializes sample user and product data. These datasets are accessible via JSON-RPC requests. |
92-
| `Server::register_tool()` | Registers AI-callable functions (`tools`). Each tool must include identifier (`name`) and function to execute (`callable`); `description` and `inputSchema` are optional. |
93-
| `Server::register_resource()` | Registers structured data for AI access. |
94-
| `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. |
95-
| `Server::handle_request()` | Parses JSON-RPC 2.0 requests. Validates structure and executes method calls. |
96-
| `Server::list_resources()` | List registered resources. |
97-
| `Server::read_resource()` | Retrieve resource data. Uses `get_resource_data()` method. |
98-
| `Server::get_resource_data()` | Retrieve resource data. |
99-
| `Server::validate_input()` | Input validation for AI tool calls. |
100-
| `Server::handle_get_request()` | Retrive stored resource data. |
101-
| `Server::create_success_response()` | Generates JSON-RPC success response. |
102-
| `Server::create_error_response()` | Generates JSON-RPC error response. |
103-
| `Server::process_request()` | Wrapper for `handle_request()` method. |
104-
85+
| Name | Return Type | Description |
86+
| --- | --- | --- |
87+
| `Server::__construct()` | `void` | Initializes the MCP Server and loads default data (users, products). |
88+
| `Server::register_tool( array $tool_definition )` | `void` | Registers an AI tool with a name, description, input schema, and a callable function. |
89+
| `Server::register_resource( array $resource_definition )` | `void` | Registers a data resource with a name, URI, and optional description. |
90+
| `Server::get_capabilities()` | `array` | Returns the list of registered AI tools and resources available in MCP. |
91+
| `Server::handle_request( string $request_data )` | `false\|string` | Parses JSON-RPC 2.0 requests. Validates structure and executes method calls. |
92+
| `Server::list_resources()` | `array` | Returns all registered resources, including name, URI, and description. |
93+
| `Server::read_resource( string $uri )` | `array` | Retrieves a specific resource by its URI, returning structured data. |
94+
| `Server::get_resource_data( $mcp_resource )` | `mixed` | Fetches resource data from a file or the internal dataset. |
95+
| `Server::validate_input( $input, $schema )` | `array` | Validates tool input against the JSON schema, checking required fields and types. |
96+
| `Server::handle_get_request( $path, $params )` | `array` | Handles get_ requests for accessing resources like `get_users` or `get_products`. |
97+
| `Server::create_success_response( $id, $result )` | `false\|string` | Generates JSON-RPC success response. |
98+
| `Server::create_error_response( $id, $message, $code )` | `false\|string` | Generates JSON-RPC error response. |
99+
| `Server::process_request( $request_data )` | `false\|string` | Wrapper for `handle_request()` method. |
105100

106101
### Examples
107102

@@ -167,35 +162,48 @@ $result = $server->validate_input( $input, $schema );
167162

168163
## `RouteInformation` class
169164

170-
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.
165+
The `RouteInformation` class encapsulates metadata about a WordPress REST API route. It provides methods to determine route characteristics, REST method type, and controller details.
166+
167+
This class is used to:
168+
169+
- Identify REST route types (`singular`/`list`, `GET`/`POST`/`DELETE`, etc.).
170+
- Validate and process REST controller callbacks.
171+
- Generate sanitized route names for MCP registration.
171172

172173
### Methods
173174

174175
| Name | Return Type | Description |
175176
| --- | --- | --- |
176-
| `RouteInformation::get_sanitized_route_name()` | `string` | Returns a cleaned-up route name (e.g., GET_wp-v2-posts_p_id). |
177-
| `RouteInformation::get_method()` | `string` | Returns the HTTP method (`GET`, `POST`, etc.). |
178-
| `RouteInformation::is_create()` | `bool` | Returns `true` if the method is `POST`. |
179-
| `RouteInformation::is_update()` | `bool` | Returns `true` if the method is `PUT` or `PATCH`. |
180-
| `RouteInformation::is_delete()` | `bool` | Returns `true` if the method is `DELETE`. |
181-
| `RouteInformation::is_get()` | `bool` | Returns `true` if the method is `GET`. |
182-
| `RouteInformation::is_singular()` | `bool` | Returns `true` if the route targets a single resource. |
183-
| `RouteInformation::is_list()` | `bool` | Returns `true` if the route retrieves multiple resources. |
184-
| `RouteInformation::is_wp_rest_controller()` | `bool` | Returns `true` if the callback is a valid REST controller. |
185-
| `RouteInformation::get_wp_rest_controller()` | `WP_REST_Controller` | Returns the controller instance (throws an error if invalid). |
177+
| `RouteInformation::__construct($route, $method, $callback)` | `void` | Initializes the class with a REST route, method, and callback. |
178+
| `RouteInformation::get_sanitized_route_name()` | `string` | Returns a clean, MCP-compatible route name. |
179+
| `RouteInformation::get_method()` | `string` | Retrieves the HTTP method (GET, POST, etc.). |
180+
| `RouteInformation::is_create()` | `bool` | Checks if the method is POST (Create). |
181+
| `RouteInformation::is_update()` | `bool` | Checks if the method is PUT or PATCH (Update). |
182+
| `RouteInformation::is_delete()` | `bool` | Checks if the method is DELETE (Delete). |
183+
| `RouteInformation::is_get()` | `bool` | Checks if the method is GET (Retrieve). |
184+
| `RouteInformation::is_singular()` | `bool` | Determines if the route targets a single resource. |
185+
| `RouteInformation::is_list()` | `bool` | Determines if the route retrieves a list. |
186+
| `RouteInformation::get_scope()` | `string` | Returns the scope (post, user, taxonomy, or default). |
187+
| `RouteInformation::is_wp_rest_controller()` | `bool` | Checks if the callback belongs to a WP REST controller. |
188+
| `RouteInformation::get_wp_rest_controller()` | `WP_REST_Controller` | Retrieves the REST controller instance or throws an error. |
186189

187190
## `MapRESTtoMCP` class
188191

189-
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.
192+
The `MapRESTtoMCP` class is responsible for mapping WordPress REST API endpoints into Machine Contextual Processing (MCP) tools. It does this by:
193+
194+
- Extracting route details from the REST API.
195+
- Generating input schemas from REST arguments.
196+
- Creating AI tools dynamically based on route metadata.
190197

191-
This class enables AI-driven automation by exposing WordPress REST API endpoints to AI services in WP-CLI.
198+
This enables seamless AI-driven interactions with the WordPress REST API.
192199

193200
### Methods
194201

195202
| Name | Return Type | Description |
196203
| --- | --- | --- |
197-
| `MapRESTtoMCP::args_to_schema()` | `array` | Converts REST API arguments into JSON Schema. |
198-
| `MapRESTtoMCP::sanitize_type()` | `string` | Maps REST API types to standardized types. |
199-
| `MapRESTtoMCP::map_rest_to_mcp()` | `void` | Registers REST API endpoints as AI tools in MCP. |
200-
| `MapRESTtoMCP::generate_description()` | `string` | Creates human-readable descriptions for API tools. |
201-
| `MapRESTtoMCP::rest_callable()` | `array` | Executes REST API calls and returns formatted data. |
204+
| `MapRESTtoMCP::args_to_schema( $args )` | `array` | Converts REST API arguments into a structured schema. |
205+
| `MapRESTtoMCP::sanitize_type( $type )` | `string` | Maps input types to standard schema types. |
206+
| `MapRESTtoMCP::map_rest_to_mcp()` | `array` | Registers REST API endpoints as AI tools in MCP. |
207+
| `MapRESTtoMCP::generate_description( $info )` | `string` | Creates human-readable descriptions for API routes. |
208+
| `MapRESTtoMCP::rest_callable( $inputs, $route, $method_name, $server )` | `array` | Executes a REST API call dynamically. |
209+

0 commit comments

Comments
 (0)