Skip to content

Commit 5481e4f

Browse files
committed
Add reference and examples for Server class.
1 parent 2efd55b commit 5481e4f

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

docs/reference.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,68 @@ The service supports:
5858
| `Client::call_ai_service()` | AI function calls and processing. |
5959

6060
## `Server` class
61+
62+
The Server class serves as the core backend for the AI Command's MCP (Multi-Client Processor) in WP-CLI. It provides:
63+
64+
- Tool Registration - Defines AI-callable functions (e.g., calculations, event fetching).
65+
- Resource Management - Stores and retrieves structured data (e.g., users, products).
66+
- JSON-RPC Request Handling - Processes incoming requests and returns AI-usable responses.
67+
- Validation & Error Handling - Ensures correct data formats and secure execution.
68+
69+
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+
71+
### Properties
72+
73+
| Name | Visibility modifier | Description |
74+
| --- | --- | --- |
75+
| `$data` | private | Stores structured data (e.g., users, products). |
76+
| `$tools` | private | Registered AI-callable tools (functions AI can invoke). |
77+
| `$resources` | private | Registered data resources accessible to AI. |
78+
79+
### Methods
80+
81+
| Name | Description |
82+
| --- | --- |
83+
| `Server::__construct()` | Constructor. Initializes sample user and product data. These datasets are accessible via JSON-RPC requests. |
84+
| `Server::register_tool()` | Registers AI-callable functions (`tools`). Each tool must include identifier (`name`) and function to execute (`callable`); `description` and `inputSchema` are optional. |
85+
| `Server::register_resource()` | Registers structured data for AI access. |
86+
| `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. |
87+
| `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. |
91+
| `Server::create_success_response()` | Generates JSON-RPC success response. |
92+
| `Server::create_error_response()` | Generates JSON-RPC error response. |
93+
94+
### Examples
95+
96+
Register a Tool
97+
98+
```PHP
99+
$server->register_tool(
100+
[
101+
'name' => 'calculate_total',
102+
'callable' => function( $params ) {
103+
return $params['price'] * $params['quantity'];
104+
},
105+
'inputSchema' => [
106+
'properties' => [
107+
'price' => [ 'type' => 'integer' ],
108+
'quantity' => [ 'type' => 'integer' ]
109+
],
110+
],
111+
]
112+
);
113+
```
114+
115+
Register a Resource
116+
117+
```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+
]);
125+
```

0 commit comments

Comments
 (0)