Skip to content

Commit 5af34bd

Browse files
committed
Improve mcp server CRUD functionality
1 parent 2c57b42 commit 5af34bd

File tree

3 files changed

+81
-13
lines changed

3 files changed

+81
-13
lines changed

features/load-wp-cli.feature

Lines changed: 0 additions & 10 deletions
This file was deleted.

features/mcp-server.feature

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
Feature: MCP server command
2+
3+
Scenario: CRUD
4+
5+
When I run `wp mcp server add foo "https://foo.example.com/mcp"`
6+
Then STDOUT should contain:
7+
"""
8+
Server added.
9+
"""
10+
11+
When I run `wp mcp server add bar "https://bar.example.com/mcp"`
12+
And I run `wp mcp server add baz "https://baz.example.com/mcp"`
13+
And I run `wp mcp server list`
14+
Then STDOUT should be a table containing rows:
15+
| name | server | status |
16+
| foo | https://foo.example.com/mcp | active |
17+
| bar | https://bar.example.com/mcp | active |
18+
| baz | https://baz.example.com/mcp | active |
19+
20+
When I run `wp mcp server remove bar baz`
21+
And I run `wp mcp server list`
22+
Then STDOUT should contain:
23+
"""
24+
foo.example.com
25+
"""
26+
And STDOUT should not contain:
27+
"""
28+
bar.example.com
29+
"""
30+
And STDOUT should not contain:
31+
"""
32+
baz.example.com
33+
"""
34+
35+
When I try `wp mcp server add foo "https://foo.example.com/mcp"`
36+
Then STDERR should contain:
37+
"""
38+
Server already exists.
39+
"""
40+
41+
When I run `wp mcp server add bar "https://bar.example.com/mcp"`
42+
And I run `wp mcp server add baz "https://baz.example.com/mcp"`
43+
And I run `wp mcp server update bar --status=inactive`
44+
Then STDOUT should contain:
45+
"""
46+
Server updated.
47+
"""
48+
49+
When I run `wp mcp server list --status=inactive`
50+
Then STDOUT should be a table containing rows:
51+
| name | server | status |
52+
| baz | https://baz.example.com/mcp | active |
53+
And STDOUT should not contain:
54+
"""
55+
foo.example.com
56+
"""
57+
And STDOUT should not contain:
58+
"""
59+
baz.example.com
60+
"""

src/McpServerCommand.php

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ class McpServerCommand extends WP_CLI_Command {
1919
*
2020
* ## OPTIONS
2121
*
22+
* [--<field>=<value>]
23+
* : Filter results by key=value pairs.
24+
*
2225
* [--format=<format>]
2326
* : Render output in a particular format.
2427
* ---
@@ -42,6 +45,8 @@ class McpServerCommand extends WP_CLI_Command {
4245
*
4346
* @subcommand list
4447
*
48+
* @when before_wp_load
49+
*
4550
* @param array $args Indexed array of positional arguments.
4651
* @param array $assoc_args Associative array of associative arguments.
4752
*/
@@ -51,6 +56,13 @@ public function list_( $args, $assoc_args ): void {
5156
$servers = [];
5257

5358
foreach ( $config as $name => $server ) {
59+
// Support features like --status=active.
60+
foreach ( array_keys( $server ) as $field ) {
61+
if ( isset( $assoc_args[ $field ] ) && ! in_array( $server[ $field ], array_map( 'trim', explode( ',', $assoc_args[ $field ] ) ), true ) ) {
62+
continue 2;
63+
}
64+
}
65+
5466
$servers[] = [
5567
'name' => $name,
5668
'server' => $server['server'],
@@ -83,6 +95,8 @@ public function list_( $args, $assoc_args ): void {
8395
* $ wp mcp server add "server-filesystem" "npx -y @modelcontextprotocol/server-filesystem /my/allowed/folder/"
8496
* Success: Server added.
8597
*
98+
* @when before_wp_load
99+
*
86100
* @param array $args Indexed array of positional arguments.
87101
*/
88102
public function add( $args ): void {
@@ -93,7 +107,7 @@ public function add( $args ): void {
93107
} else {
94108
$config[ $args[0] ] = [
95109
'server' => $args[1],
96-
'status' => 'enabled',
110+
'status' => 'active',
97111
];
98112

99113
$result = $this->get_config()->update_config( $config );
@@ -123,6 +137,8 @@ public function add( $args ): void {
123137
* $ wp mcp server remove "server-filesystem"
124138
* Success: Server removed.
125139
*
140+
* @when before_wp_load
141+
*
126142
* @param array $args Indexed array of positional arguments.
127143
*/
128144
public function remove( $args, $assoc_args ): void {
@@ -172,9 +188,11 @@ public function remove( $args, $assoc_args ): void {
172188
* ## EXAMPLES
173189
*
174190
* # Remove server.
175-
* $ wp mcp server update "server-filesystem" --status=disabled
191+
* $ wp mcp server update "server-filesystem" --status=inactive
176192
* Success: Server updated.
177193
*
194+
* @when before_wp_load
195+
*
178196
* @param array $args Indexed array of positional arguments.
179197
*/
180198
public function update( $args, $assoc_args ): void {
@@ -187,7 +205,7 @@ public function update( $args, $assoc_args ): void {
187205
foreach ( $config[ $args[0] ] as $key => $value ) {
188206
if ( isset( $assoc_args[ $key ] ) ) {
189207
if ( 'status' === $key ) {
190-
$value = 'disabled' === $value ? 'enabled' : 'disabled';
208+
$value = 'inactive' === $value ? 'active' : 'inactive';
191209
}
192210
$config[ $args[0] ][ $key ] = $value;
193211
}

0 commit comments

Comments
 (0)