Copy-paste this file into any AI chat or share it with a human developer. It is a single, compressed reference that explains what the plugin does, how its pieces fit together, and how to extend it with your own Tools, Resources, and Prompts.
The WordPress MCP plugin implements the Model Context Protocol to enable AI systems to interact with WordPress sites through a standardized interface. The plugin provides three main extension points:
- Tools: Actions AI can request the site to perform (e.g., creating posts, getting user info)
- Resources: Data AI can request from the site (e.g., site settings, plugin information)
- Prompts: Pre-defined templates for common AI tasks related to WordPress
This plugin serves as a bridge between AI models and WordPress, allowing secure, structured interactions with your site's content and functionality.
WordPress MCP follows a modular architecture with these key components:
- Core: Central classes managing registration and initialization
- Tools: Interfaces for performing actions in WordPress
- Resources: Data providers for site information
- Prompts: Structured interaction templates
- Admin Interface: Settings page for enabling/disabling features
- REST API Endpoints: Communication channels for MCP clients
The plugin exposes a REST API endpoint at /wp/v2/wpmcp that handles all MCP operations using JSON-RPC style requests.
The central class that manages registration of tools, resources, and prompts. It handles initialization and maintains references to all registered components.
// Access the main plugin instance
$mcp = WPMCP();
// Get registered tools, resources, or prompts
$tools = $mcp->get_tools();
$resources = $mcp->get_resources();
$prompts = $mcp->get_prompts();Three main classes handle registration of components:
RegisterMcpTool: Registers tools that perform actionsRegisterMcpResource: Registers resources that provide dataRegisterMcpPrompt: Registers prompts for structured interactions
Tools are actions AI can perform in WordPress. There are four types:
read: For retrieving data (always allowed)create: For creating new content (requires enabling in settings)update: For modifying existing content (requires enabling in settings)delete: For removing content (requires enabling in settings)
Example: Creating a Custom Tool
use Automattic\WordpressMcp\Core\RegisterMcpTool;
class MyCustomTool {
public function __construct() {
add_action('wordpress_mcp_init', [$this, 'register_tools']);
}
public function register_tools(): void {
new RegisterMcpTool([
'name' => 'my_custom_tool',
'description' => 'A custom tool that performs a specific action',
'type' => 'read',
'inputSchema' => [
'type' => 'object',
'properties' => [
'param1' => [
'type' => 'string',
'description' => 'First parameter',
],
],
'required' => ['param1'],
],
'callback' => [$this, 'tool_callback'],
'permission_callback' => [$this, 'permission_callback'],
]);
}
public function tool_callback($args) {
// Tool implementation
return [
'success' => true,
'data' => 'Result of the tool operation',
];
}
public function permission_callback() {
// Check if the user has permission to use this tool
return current_user_can('manage_options');
}
}
// Initialize your custom tool
new MyCustomTool();REST API Integration
You can also create tools that map to existing WordPress REST API endpoints:
new RegisterMcpTool([
'name' => 'wp_custom_post_tool',
'description' => 'Interact with custom post types',
'type' => 'read',
'rest_alias' => [
'route' => '/wp/v2/my-custom-post-type',
'method' => 'GET',
],
]);Resources provide data to AI systems. They are registered with a URI, name, description, and MIME type.
Example: Creating a Custom Resource
use Automattic\WordpressMcp\Core\RegisterMcpResource;
class MyCustomResource {
public function __construct() {
add_action('wordpress_mcp_init', [$this, 'register_resource']);
}
public function register_resource(): void {
new RegisterMcpResource(
[
'uri' => 'WordPress://my-custom-resource',
'name' => 'my-custom-resource',
'description' => 'Provides custom data about the site',
'mimeType' => 'application/json',
],
[$this, 'get_resource_data']
);
}
public function get_resource_data($params = []) {
// Resource implementation
return [
'custom_field' => 'Custom value',
'timestamp' => current_time('timestamp'),
// Add other data as needed
];
}
}
// Initialize your custom resource
new MyCustomResource();Prompts are templates for structured AI interactions. They define a set of messages and can include arguments that are replaced at runtime.
Example: Creating a Custom Prompt
use Automattic\WordpressMcp\Core\RegisterMcpPrompt;
class MyCustomPrompt {
public function __construct() {
add_action('wordpress_mcp_init', [$this, 'register_prompt']);
}
public function register_prompt(): void {
new RegisterMcpPrompt(
[
'name' => 'my-custom-prompt',
'description' => 'A custom prompt for a specific task',
'arguments' => [
[
'name' => 'input_data',
'description' => 'The data to process',
'required' => true,
'type' => 'string',
],
[
'name' => 'format',
'description' => 'The desired output format',
'required' => false,
'type' => 'string',
],
],
],
$this->messages()
);
}
private function messages(): array {
return [
[
'role' => 'user',
'content' => [
'type' => 'text',
'text' => 'Process the following data: {{input_data}}. Format the output as {{format|default:"JSON"}}.',
],
],
];
}
}
// Initialize your custom prompt
new MyCustomPrompt();use Automattic\WordpressMcp\Core\RegisterMcpTool;
// Register a tool to search posts
new RegisterMcpTool([
'name' => 'custom_posts_search',
'description' => 'Search and filter WordPress posts',
'type' => 'read',
'rest_alias' => [
'route' => '/wp/v2/posts',
'method' => 'GET',
],
]);
// Register a tool to create posts
new RegisterMcpTool([
'name' => 'custom_add_post',
'description' => 'Add a new WordPress post',
'type' => 'create',
'rest_alias' => [
'route' => '/wp/v2/posts',
'method' => 'POST',
],
]);use Automattic\WordpressMcp\Core\RegisterMcpResource;
new RegisterMcpResource(
[
'uri' => 'WordPress://site-custom-info',
'name' => 'site-custom-info',
'description' => 'Provides custom information about the WordPress site',
'mimeType' => 'application/json',
],
function($params) {
return [
'site_name' => get_bloginfo('name'),
'site_url' => get_bloginfo('url'),
'site_description' => get_bloginfo('description'),
'custom_meta' => get_option('my_custom_site_meta'),
'theme_info' => [
'name' => wp_get_theme()->get('Name'),
'version' => wp_get_theme()->get('Version'),
],
];
}
);use Automattic\WordpressMcp\Core\RegisterMcpTool;
// Check if WooCommerce is active before registering WooCommerce-specific tools
if (class_exists('WooCommerce')) {
// Register a tool to get WooCommerce products
new RegisterMcpTool([
'name' => 'custom_wc_products',
'description' => 'Get WooCommerce products',
'type' => 'read',
'rest_alias' => [
'route' => '/wc/v3/products',
'method' => 'GET',
],
]);
// Register a tool to analyze sales data
new RegisterMcpTool([
'name' => 'analyze_wc_sales',
'description' => 'Analyze WooCommerce sales data',
'type' => 'read',
'callback' => function($args) {
// Get sales data for the specified period
$period = $args['period'] ?? 'last_30_days';
// Get sales data from WooCommerce
$orders = wc_get_orders([
'limit' => -1,
'date_created' => '>' . strtotime('-30 days'),
'status' => ['completed', 'processing'],
]);
// Calculate sales metrics
$total_sales = 0;
$order_count = count($orders);
foreach ($orders as $order) {
$total_sales += $order->get_total();
}
$average_order_value = $order_count > 0 ? $total_sales / $order_count : 0;
return [
'period' => $period,
'total_sales' => $total_sales,
'order_count' => $order_count,
'average_order_value' => $average_order_value,
];
},
'permission_callback' => function() {
return current_user_can('manage_woocommerce');
},
'inputSchema' => [
'type' => 'object',
'properties' => [
'period' => [
'type' => 'string',
'description' => 'The period to analyze (e.g., last_7_days, last_30_days, last_quarter)',
'default' => 'last_30_days',
],
],
],
]);
}Main plugin class that manages registration and initialization.
Methods:
instance(): Returns the singleton instanceregister_tool($args): Registers a toolregister_resource($args): Registers a resourceregister_resource_callback($uri, $callback): Registers a resource callbackregister_prompt($prompt, $messages): Registers a promptget_tools(): Returns all registered toolsget_resources(): Returns all registered resourcesget_prompts(): Returns all registered prompts
Registers an MCP tool with the system.
Constructor Parameters:
$args: Array of tool arguments including:name: Unique identifier for the tooldescription: Human-readable descriptiontype: Tool type ('read', 'create', 'update', 'delete')callback: Function that implements the tool's logicpermission_callback: Function that checks user permissionsinputSchema: JSON Schema describing the tool's input parametersrest_alias: Optional mapping to a WordPress REST API endpoint
Registers an MCP resource with the system.
Constructor Parameters:
$args: Array of resource arguments including:uri: Unique URI identifier for the resourcename: Human-readable namedescription: Description of the resource's purposemimeType: MIME type of the resource content
$resource_content_callback: Function that provides the resource data
Registers an MCP prompt with the system.
Constructor Parameters:
$prompt: Array of prompt arguments including:name: Unique identifier for the promptdescription: Description of the prompt's purposearguments: Array of argument definitions
$messages: Array of message templates
The plugin's settings page is accessible at Settings > MCP in the WordPress admin dashboard. It provides options to enable/disable:
- MCP functionality
- WordPress Features Adapter
- Create tools
- Update tools
- Delete tools
You can programmatically access these settings:
$options = get_option('wordpress_mcp_settings', []);
$mcp_enabled = isset($options['enabled']) && $options['enabled'];
$create_tools_enabled = isset($options['enable_create_tools']) && $options['enable_create_tools'];The plugin can optionally integrate with the WordPress Feature API to expose registered features as MCP tools. This is handled by the WpFeaturesAdapter class.
To use this feature:
- Install and activate the WordPress Feature API plugin
- Enable "WordPress Features Adapter" in the MCP settings
-
Permissions: All MCP operations require administrator privileges by default. Use the
permission_callbackwhen registering tools to set custom permission requirements. -
Security: Be careful when enabling create/update/delete tools as they can modify your WordPress site content.
-
REST API Integration: The plugin exposes a single endpoint at
/wp/v2/wpmcpthat handles all MCP operations. -
Action Hooks: The main action hook to use when extending the plugin is
wordpress_mcp_init. -
Filter Hooks: You can filter the registered tools, resources, and prompts using standard WordPress filters.
-
Error Handling: Always handle errors appropriately in your tool and resource callbacks.
-
Tool Types: Remember that 'read' tools are always available when MCP is enabled, but other types require explicit enabling in settings.
To create extensions to the MCP plugin from your own theme or plugin:
/**
* Add custom MCP extensions
*/
function my_theme_register_mcp_extensions() {
// Check if MCP plugin is active
if (!function_exists('WPMCP')) {
return;
}
// Register your custom tools, resources, and prompts
add_action('wordpress_mcp_init', 'my_theme_register_mcp_tools');
add_action('wordpress_mcp_init', 'my_theme_register_mcp_resources');
add_action('wordpress_mcp_init', 'my_theme_register_mcp_prompts');
}
add_action('init', 'my_theme_register_mcp_extensions');
/**
* Register custom MCP tools
*/
function my_theme_register_mcp_tools() {
// Register your custom tools here
new \Automattic\WordpressMcp\Core\RegisterMcpTool([
'name' => 'my_theme_custom_tool',
'description' => 'A custom tool from my theme',
'type' => 'read',
'callback' => 'my_theme_custom_tool_callback',
'permission_callback' => function() {
return current_user_can('edit_posts');
},
'inputSchema' => [
'type' => 'object',
'properties' => [],
],
]);
}
/**
* Custom tool callback function
*/
function my_theme_custom_tool_callback($args) {
// Implement your tool logic here
return ['status' => 'success', 'data' => 'Custom tool executed successfully'];
}
// Similar functions for resources and prompts-
Plugin Not Working: Ensure MCP functionality is enabled in the settings.
-
Tools Not Available: Check that the appropriate tool types (create/update/delete) are enabled.
-
WordPress Features Adapter Not Working: Verify that the WordPress Feature API plugin is installed and activated.
-
REST API Errors: Check that the WordPress REST API is accessible and not blocked by security plugins.
-
Permission Issues: Ensure that the user has the appropriate permissions to use the tools/resources.