The RegisterMcpResources API allows you to register and manage resources that provide data to AI models in WordPress.
Resources are data sources that can be accessed by AI models. They are registered using the RegisterMcpResource class and provide structured data in various formats.
use Automattic\WordpressMcp\Core\RegisterMcpResource;
// Register a simple resource
new RegisterMcpResource(
[
'uri' => 'WordPress://my-resource',
'name' => 'my-resource',
'description' => 'A custom resource for providing data',
'mimeType' => 'application/json',
],
function($params) {
// Resource implementation
return ['data' => 'example'];
}
);| Parameter | Type | Required | Description |
|---|---|---|---|
| uri | string | Yes | Unique URI identifier for the resource |
| name | string | Yes | Human-readable name of the resource |
| description | string | Yes | Description of the resource's purpose |
| mimeType | string | Yes | MIME type of the resource content (e.g., 'application/json') |
| callback | callable | Yes | Function that provides the resource data |
Resources can provide different types of data:
-
JSON Resources
- MIME type:
application/json - Used for structured data
- Example: Plugin information, site settings
- MIME type:
-
Text Resources
- MIME type:
text/plain - Used for unstructured text data
- Example: Log files, configuration files
- MIME type:
use Automattic\WordpressMcp\Core\RegisterMcpResource;
use Automattic\WordpressMcp\Utils\PluginsInfo;
class McpPluginInfoResource {
private $plugins_info;
public function __construct() {
$this->plugins_info = new PluginsInfo();
add_action('wordpress_mcp_init', [$this, 'register_resource']);
}
public function register_resource() {
new RegisterMcpResource(
[
'uri' => 'WordPress://plugin-info',
'name' => 'plugin-info',
'description' => 'Provides detailed information about active WordPress plugins',
'mimeType' => 'application/json',
],
[$this, 'get_plugin_info']
);
}
public function get_plugin_info($params = []) {
return $this->plugins_info->get_plugins_info();
}
}Resources can be subscribed to for real-time updates:
// Subscribe to a resource
$subscription = wp_mcp_subscribe_resource('WordPress://plugin-info');
// Unsubscribe from a resource
wp_mcp_unsubscribe_resource($subscription['subscriptionId']);Resources should handle errors appropriately:
new RegisterMcpResource(
[
'uri' => 'WordPress://my-resource',
'name' => 'my-resource',
'description' => 'A resource with error handling',
'mimeType' => 'application/json',
],
function($params) {
try {
// Resource implementation
return [
'success' => true,
'data' => $this->get_resource_data($params)
];
} catch (Exception $e) {
return [
'success' => false,
'error' => $e->getMessage()
];
}
}
);- Unique URIs: Ensure resource URIs are unique across all registered resources
- Clear Descriptions: Provide detailed descriptions of the resource's data
- Proper MIME Types: Use appropriate MIME types for the resource content
- Error Handling: Include proper error handling in callbacks
- Data Validation: Validate and sanitize resource data
- Caching: Implement caching for frequently accessed resources
- Documentation: Document the resource's data structure and available parameters
use Automattic\WordpressMcp\Core\RegisterMcpResource;
class MyCustomResource {
public function __construct() {
add_action('wordpress_mcp_init', [$this, 'register_resource']);
}
public function register_resource() {
new RegisterMcpResource(
[
'uri' => 'WordPress://my-custom-resource',
'name' => 'my-custom-resource',
'description' => 'A custom resource for providing custom data',
'mimeType' => 'application/json',
],
[$this, 'resource_callback']
);
}
public function resource_callback($params) {
try {
return [
'success' => true,
'data' => $this->get_resource_data($params)
];
} catch (Exception $e) {
return [
'success' => false,
'error' => $e->getMessage()
];
}
}
private function get_resource_data($params) {
// Implementation details
return [
'example' => 'data',
'timestamp' => time()
];
}
}