Skip to content

Commit c9caef1

Browse files
Merge pull request #2 from Automattic/tools-management
Add tool management features to MCP
2 parents 9e96cfa + 61d9006 commit c9caef1

File tree

12 files changed

+275
-111
lines changed

12 files changed

+275
-111
lines changed

includes/Admin/Settings.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,12 @@ public function enqueue_scripts( string $hook ): void {
100100
'enableMcpDescription' => __( 'Toggle to enable or disable the MCP plugin functionality.', 'wordpress-mcp' ),
101101
'enableFeaturesAdapter' => __( 'Enable WordPress Features Adapter', 'wordpress-mcp' ),
102102
'enableFeaturesAdapterDescription' => __( 'Enable or disable the WordPress Features Adapter. This option only works when MCP is enabled.', 'wordpress-mcp' ),
103+
'enableCreateTools' => __( 'Enable Create Tools', 'wordpress-mcp' ),
104+
'enableCreateToolsDescription' => __( 'Allow create operations via tools.', 'wordpress-mcp' ),
105+
'enableUpdateTools' => __( 'Enable Update Tools', 'wordpress-mcp' ),
106+
'enableUpdateToolsDescription' => __( 'Allow update operations via tools.', 'wordpress-mcp' ),
107+
'enableDeleteTools' => __( 'Enable Delete Tools', 'wordpress-mcp' ),
108+
'enableDeleteToolsDescription' => __( '⚠️ CAUTION: Allow deletion operations via tools.', 'wordpress-mcp' ),
103109
'saveSettings' => __( 'Save Settings', 'wordpress-mcp' ),
104110
'settingsSaved' => __( 'Settings saved successfully!', 'wordpress-mcp' ),
105111
'settingsError' => __( 'Error saving settings. Please try again.', 'wordpress-mcp' ),
@@ -150,6 +156,24 @@ public function sanitize_settings( array $input ): array {
150156
$sanitized['features_adapter_enabled'] = false;
151157
}
152158

159+
if ( isset( $input['enable_create_tools'] ) ) {
160+
$sanitized['enable_create_tools'] = (bool) $input['enable_create_tools'];
161+
} else {
162+
$sanitized['enable_create_tools'] = false;
163+
}
164+
165+
if ( isset( $input['enable_update_tools'] ) ) {
166+
$sanitized['enable_update_tools'] = (bool) $input['enable_update_tools'];
167+
} else {
168+
$sanitized['enable_update_tools'] = false;
169+
}
170+
171+
if ( isset( $input['enable_delete_tools'] ) ) {
172+
$sanitized['enable_delete_tools'] = (bool) $input['enable_delete_tools'];
173+
} else {
174+
$sanitized['enable_delete_tools'] = false;
175+
}
176+
153177
return $sanitized;
154178
}
155179

includes/Core/RegisterMcpTool.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,17 @@ private function validate_arguments(): void {
151151
throw new InvalidArgumentException( 'The description is required.' );
152152
}
153153

154+
// functionality_type is required.
155+
if ( ! isset( $this->args['type'] ) ) {
156+
throw new InvalidArgumentException( 'The functionality type is required.' );
157+
}
158+
159+
// validate functionality type: must be one of 'create', 'read', 'update', 'delete'.
160+
$valid_types = array( 'create', 'read', 'update', 'delete' );
161+
if ( ! in_array( $this->args['type'], $valid_types, true ) ) {
162+
throw new InvalidArgumentException( 'The functionality type must be one of: ' . esc_html( implode( ', ', $valid_types ) ) );
163+
}
164+
154165
// if rest_alias is provided, the rest of the arguments are not required.
155166
if ( isset( $this->args['rest_alias'] ) ) {
156167
$this->validate_rest_alias();

includes/Core/WpMcp.php

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,14 +170,48 @@ public static function instance(): WpMcp {
170170
return self::$instance;
171171
}
172172

173+
/**
174+
* Check if a tool type is enabled.
175+
*
176+
* @param string $type The tool type to check.
177+
* @return bool Whether the tool type is enabled.
178+
*/
179+
private function is_tool_type_enabled( string $type ): bool {
180+
$options = get_option( 'wordpress_mcp_settings', array() );
181+
182+
// Read operations are always allowed if MCP is enabled.
183+
if ( 'read' === $type ) {
184+
return true;
185+
}
186+
187+
// Check specific tool type settings.
188+
$type_settings_map = array(
189+
'create' => 'enable_create_tools',
190+
'update' => 'enable_update_tools',
191+
'delete' => 'enable_delete_tools',
192+
);
193+
194+
// Check if the type exists in our mapping and is enabled.
195+
if ( isset( $type_settings_map[ $type ] ) ) {
196+
return isset( $options[ $type_settings_map[ $type ] ] ) && $options[ $type_settings_map[ $type ] ];
197+
}
198+
199+
return false;
200+
}
201+
173202
/**
174203
* Register a tool.
175204
*
176205
* @param array $args The arguments.
177-
* @throws InvalidArgumentException If the tool name is not unique.
206+
* @throws InvalidArgumentException If the tool name is not unique or if the tool type is disabled.
178207
*/
179208
public function register_tool( array $args ): void {
180-
// the name should be unique.
209+
// Check if the tool type is enabled.
210+
if ( ! $this->is_tool_type_enabled( $args['type'] ) ) {
211+
return; // Skip registration if tool type is disabled.
212+
}
213+
214+
// The name should be unique.
181215
if ( in_array( $args['name'], array_column( $this->tools, 'name' ), true ) ) {
182216
throw new InvalidArgumentException( 'The tool name must be unique. A tool with this name already exists: ' . esc_html( $args['name'] ) );
183217
}
@@ -220,7 +254,8 @@ public function register_resource_callback( string $uri, callable $callback ): v
220254
/**
221255
* Register a prompt.
222256
*
223-
* @param array $prompt The prompt instance.
257+
* @param array $prompt The prompt instance.
258+
* @param array $messages The messages for the prompt.
224259
* @throws InvalidArgumentException If the prompt name is not unique.
225260
*/
226261
public function register_prompt( array $prompt, array $messages ): void {

includes/Tools/McpPostsTools.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?php //phpcs:ignore
2-
declare(strict_types=1);
2+
declare( strict_types=1 );
33

44
namespace Automattic\WordpressMcp\Tools;
55

@@ -25,6 +25,7 @@ public function register_tools(): void {
2525
array(
2626
'name' => 'wp_posts_search',
2727
'description' => 'Search and filter WordPress posts with pagination',
28+
'type' => 'read',
2829
'rest_alias' => array(
2930
'route' => '/wp/v2/posts',
3031
'method' => 'GET',
@@ -36,6 +37,7 @@ public function register_tools(): void {
3637
array(
3738
'name' => 'wp_get_post',
3839
'description' => 'Get a WordPress post by ID',
40+
'type' => 'read',
3941
'rest_alias' => array(
4042
'route' => '/wp/v2/posts/(?P<id>[\d]+)',
4143
'method' => 'GET',
@@ -47,6 +49,7 @@ public function register_tools(): void {
4749
array(
4850
'name' => 'wp_add_post',
4951
'description' => 'Add a new WordPress post',
52+
'type' => 'create',
5053
'rest_alias' => array(
5154
'route' => '/wp/v2/posts',
5255
'method' => 'POST',
@@ -58,6 +61,7 @@ public function register_tools(): void {
5861
array(
5962
'name' => 'wp_update_post',
6063
'description' => 'Update a WordPress post by ID',
64+
'type' => 'update',
6165
'rest_alias' => array(
6266
'route' => '/wp/v2/posts/(?P<id>[\d]+)',
6367
'method' => 'PUT',
@@ -69,6 +73,7 @@ public function register_tools(): void {
6973
array(
7074
'name' => 'wp_delete_post',
7175
'description' => 'Delete a WordPress post by ID',
76+
'type' => 'delete',
7277
'rest_alias' => array(
7378
'route' => '/wp/v2/posts/(?P<id>[\d]+)',
7479
'method' => 'DELETE',
@@ -81,6 +86,7 @@ public function register_tools(): void {
8186
array(
8287
'name' => 'wp_list_categories',
8388
'description' => 'List all WordPress post categories',
89+
'type' => 'read',
8490
'rest_alias' => array(
8591
'route' => '/wp/v2/categories',
8692
'method' => 'GET',
@@ -93,6 +99,7 @@ public function register_tools(): void {
9399
array(
94100
'name' => 'wp_add_category',
95101
'description' => 'Add a new WordPress post category',
102+
'type' => 'create',
96103
'rest_alias' => array(
97104
'route' => '/wp/v2/categories',
98105
'method' => 'POST',
@@ -105,6 +112,7 @@ public function register_tools(): void {
105112
array(
106113
'name' => 'wp_update_category',
107114
'description' => 'Update a WordPress post category',
115+
'type' => 'update',
108116
'rest_alias' => array(
109117
'route' => '/wp/v2/categories/(?P<id>[\d]+)',
110118
'method' => 'PUT',
@@ -117,6 +125,7 @@ public function register_tools(): void {
117125
array(
118126
'name' => 'wp_delete_category',
119127
'description' => 'Delete a WordPress post category',
128+
'type' => 'delete',
120129
'rest_alias' => array(
121130
'route' => '/wp/v2/categories/(?P<id>[\d]+)',
122131
'method' => 'DELETE',
@@ -129,6 +138,7 @@ public function register_tools(): void {
129138
array(
130139
'name' => 'wp_list_tags',
131140
'description' => 'List all WordPress post tags',
141+
'type' => 'read',
132142
'rest_alias' => array(
133143
'route' => '/wp/v2/tags',
134144
'method' => 'GET',
@@ -141,6 +151,7 @@ public function register_tools(): void {
141151
array(
142152
'name' => 'wp_add_tag',
143153
'description' => 'Add a new WordPress post tag',
154+
'type' => 'create',
144155
'rest_alias' => array(
145156
'route' => '/wp/v2/tags',
146157
'method' => 'POST',
@@ -153,6 +164,7 @@ public function register_tools(): void {
153164
array(
154165
'name' => 'wp_update_tag',
155166
'description' => 'Update a WordPress post tag',
167+
'type' => 'update',
156168
'rest_alias' => array(
157169
'route' => '/wp/v2/tags/(?P<id>[\d]+)',
158170
'method' => 'PUT',
@@ -165,6 +177,7 @@ public function register_tools(): void {
165177
array(
166178
'name' => 'wp_delete_tag',
167179
'description' => 'Delete a WordPress post tag',
180+
'type' => 'delete',
168181
'rest_alias' => array(
169182
'route' => '/wp/v2/tags/(?P<id>[\d]+)',
170183
'method' => 'DELETE',

includes/Tools/McpSiteInfo.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?php //phpcs:ignore
2-
declare(strict_types=1);
2+
declare( strict_types=1 );
33

44
namespace Automattic\WordpressMcp\Tools;
55

@@ -31,6 +31,7 @@ public function register_tools(): void {
3131
array(
3232
'name' => 'get_site_info',
3333
'description' => 'Provides detailed information about the WordPress site like site name, url, description, admin email, plugins, themes, users, and more',
34+
'type' => 'read',
3435
'inputSchema' => array(
3536
'type' => 'object',
3637
'properties' => new stdClass(),

includes/Tools/McpUsersTools.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?php //phpcs:ignore
2-
declare(strict_types=1);
2+
declare( strict_types=1 );
33

44
namespace Automattic\WordpressMcp\Tools;
55

@@ -25,6 +25,7 @@ public function register_tools(): void {
2525
array(
2626
'name' => 'wp_users_search',
2727
'description' => 'Search and filter WordPress users with pagination',
28+
'type' => 'read',
2829
'rest_alias' => array(
2930
'route' => '/wp/v2/users',
3031
'method' => 'GET',
@@ -36,6 +37,7 @@ public function register_tools(): void {
3637
array(
3738
'name' => 'wp_get_user',
3839
'description' => 'Get a WordPress user by ID',
40+
'type' => 'read',
3941
'rest_alias' => array(
4042
'route' => '/wp/v2/users/(?P<id>[\d]+)',
4143
'method' => 'GET',
@@ -47,6 +49,7 @@ public function register_tools(): void {
4749
array(
4850
'name' => 'wp_add_user',
4951
'description' => 'Add a new WordPress user',
52+
'type' => 'create',
5053
'rest_alias' => array(
5154
'route' => '/wp/v2/users',
5255
'method' => 'POST',
@@ -58,6 +61,7 @@ public function register_tools(): void {
5861
array(
5962
'name' => 'wp_update_user',
6063
'description' => 'Update a WordPress user by ID',
64+
'type' => 'update',
6165
'rest_alias' => array(
6266
'route' => '/wp/v2/users/(?P<id>[\d]+)',
6367
'method' => 'PUT',
@@ -69,30 +73,33 @@ public function register_tools(): void {
6973
array(
7074
'name' => 'wp_delete_user',
7175
'description' => 'Delete a WordPress user by ID',
76+
'type' => 'delete',
7277
'rest_alias' => array(
7378
'route' => '/wp/v2/users/(?P<id>[\d]+)',
7479
'method' => 'DELETE',
7580
),
7681
)
7782
);
7883

79-
// Get current user
84+
// Get current user.
8085
new RegisterMcpTool(
8186
array(
8287
'name' => 'wp_get_current_user',
8388
'description' => 'Get the current logged-in user',
89+
'type' => 'read',
8490
'rest_alias' => array(
8591
'route' => '/wp/v2/users/me',
8692
'method' => 'GET',
8793
),
8894
)
8995
);
9096

91-
// Update current user
97+
// Update current user.
9298
new RegisterMcpTool(
9399
array(
94100
'name' => 'wp_update_current_user',
95101
'description' => 'Update the current logged-in user',
102+
'type' => 'update',
96103
'rest_alias' => array(
97104
'route' => '/wp/v2/users/me',
98105
'method' => 'PUT',

includes/Tools/McpWooOrders.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?php //phpcs:ignore
2-
declare(strict_types=1);
2+
declare( strict_types=1 );
33

44
namespace Automattic\WordpressMcp\Tools;
55

@@ -36,18 +36,20 @@ public function register_tools(): void {
3636
array(
3737
'name' => 'wc_orders_search',
3838
'description' => 'Get a list of WooCommerce orders',
39+
'type' => 'read',
3940
'rest_alias' => array(
4041
'route' => '/wc/v3/orders',
4142
'method' => 'GET',
4243
),
4344
)
4445
);
4546

46-
// Reports tools
47+
// Reports tools.
4748
new RegisterMcpTool(
4849
array(
4950
'name' => 'wc_reports_coupons_totals',
5051
'description' => 'Get WooCommerce coupons totals report',
52+
'type' => 'read',
5153
'rest_alias' => array(
5254
'route' => '/wc/v3/reports/coupons/totals',
5355
'method' => 'GET',
@@ -59,6 +61,7 @@ public function register_tools(): void {
5961
array(
6062
'name' => 'wc_reports_customers_totals',
6163
'description' => 'Get WooCommerce customers totals report',
64+
'type' => 'read',
6265
'rest_alias' => array(
6366
'route' => '/wc/v3/reports/customers/totals',
6467
'method' => 'GET',
@@ -70,6 +73,7 @@ public function register_tools(): void {
7073
array(
7174
'name' => 'wc_reports_orders_totals',
7275
'description' => 'Get WooCommerce orders totals report',
76+
'type' => 'read',
7377
'rest_alias' => array(
7478
'route' => '/wc/v3/reports/orders/totals',
7579
'method' => 'GET',
@@ -81,6 +85,7 @@ public function register_tools(): void {
8185
array(
8286
'name' => 'wc_reports_products_totals',
8387
'description' => 'Get WooCommerce products totals report',
88+
'type' => 'read',
8489
'rest_alias' => array(
8590
'route' => '/wc/v3/reports/products/totals',
8691
'method' => 'GET',
@@ -92,6 +97,7 @@ public function register_tools(): void {
9297
array(
9398
'name' => 'wc_reports_reviews_totals',
9499
'description' => 'Get WooCommerce reviews totals report',
100+
'type' => 'read',
95101
'rest_alias' => array(
96102
'route' => '/wc/v3/reports/reviews/totals',
97103
'method' => 'GET',
@@ -103,6 +109,7 @@ public function register_tools(): void {
103109
array(
104110
'name' => 'wc_reports_sales',
105111
'description' => 'Get WooCommerce sales report',
112+
'type' => 'read',
106113
'rest_alias' => array(
107114
'route' => '/wc/v3/reports/sales',
108115
'method' => 'GET',

0 commit comments

Comments
 (0)