From 5af34bd01414530837d592fbca035a11dc86ffc0 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Mon, 7 Apr 2025 19:24:27 +0200 Subject: [PATCH 1/5] Improve `mcp server` CRUD functionality --- features/load-wp-cli.feature | 10 ------ features/mcp-server.feature | 60 ++++++++++++++++++++++++++++++++++++ src/McpServerCommand.php | 24 +++++++++++++-- 3 files changed, 81 insertions(+), 13 deletions(-) delete mode 100644 features/load-wp-cli.feature create mode 100644 features/mcp-server.feature diff --git a/features/load-wp-cli.feature b/features/load-wp-cli.feature deleted file mode 100644 index 035b867..0000000 --- a/features/load-wp-cli.feature +++ /dev/null @@ -1,10 +0,0 @@ -Feature: Test that WP-CLI loads. - - Scenario: WP-CLI loads for your tests - Given a WP install - - When I run `wp eval 'echo "Hello world.";'` - Then STDOUT should contain: - """ - Hello world. - """ diff --git a/features/mcp-server.feature b/features/mcp-server.feature new file mode 100644 index 0000000..2e2c050 --- /dev/null +++ b/features/mcp-server.feature @@ -0,0 +1,60 @@ +Feature: MCP server command + + Scenario: CRUD + + When I run `wp mcp server add foo "https://foo.example.com/mcp"` + Then STDOUT should contain: + """ + Server added. + """ + + When I run `wp mcp server add bar "https://bar.example.com/mcp"` + And I run `wp mcp server add baz "https://baz.example.com/mcp"` + And I run `wp mcp server list` + Then STDOUT should be a table containing rows: + | name | server | status | + | foo | https://foo.example.com/mcp | active | + | bar | https://bar.example.com/mcp | active | + | baz | https://baz.example.com/mcp | active | + + When I run `wp mcp server remove bar baz` + And I run `wp mcp server list` + Then STDOUT should contain: + """ + foo.example.com + """ + And STDOUT should not contain: + """ + bar.example.com + """ + And STDOUT should not contain: + """ + baz.example.com + """ + + When I try `wp mcp server add foo "https://foo.example.com/mcp"` + Then STDERR should contain: + """ + Server already exists. + """ + + When I run `wp mcp server add bar "https://bar.example.com/mcp"` + And I run `wp mcp server add baz "https://baz.example.com/mcp"` + And I run `wp mcp server update bar --status=inactive` + Then STDOUT should contain: + """ + Server updated. + """ + + When I run `wp mcp server list --status=inactive` + Then STDOUT should be a table containing rows: + | name | server | status | + | baz | https://baz.example.com/mcp | active | + And STDOUT should not contain: + """ + foo.example.com + """ + And STDOUT should not contain: + """ + baz.example.com + """ diff --git a/src/McpServerCommand.php b/src/McpServerCommand.php index 59114ea..62b41d1 100644 --- a/src/McpServerCommand.php +++ b/src/McpServerCommand.php @@ -19,6 +19,9 @@ class McpServerCommand extends WP_CLI_Command { * * ## OPTIONS * + * [--=] + * : Filter results by key=value pairs. + * * [--format=] * : Render output in a particular format. * --- @@ -42,6 +45,8 @@ class McpServerCommand extends WP_CLI_Command { * * @subcommand list * + * @when before_wp_load + * * @param array $args Indexed array of positional arguments. * @param array $assoc_args Associative array of associative arguments. */ @@ -51,6 +56,13 @@ public function list_( $args, $assoc_args ): void { $servers = []; foreach ( $config as $name => $server ) { + // Support features like --status=active. + foreach ( array_keys( $server ) as $field ) { + if ( isset( $assoc_args[ $field ] ) && ! in_array( $server[ $field ], array_map( 'trim', explode( ',', $assoc_args[ $field ] ) ), true ) ) { + continue 2; + } + } + $servers[] = [ 'name' => $name, 'server' => $server['server'], @@ -83,6 +95,8 @@ public function list_( $args, $assoc_args ): void { * $ wp mcp server add "server-filesystem" "npx -y @modelcontextprotocol/server-filesystem /my/allowed/folder/" * Success: Server added. * + * @when before_wp_load + * * @param array $args Indexed array of positional arguments. */ public function add( $args ): void { @@ -93,7 +107,7 @@ public function add( $args ): void { } else { $config[ $args[0] ] = [ 'server' => $args[1], - 'status' => 'enabled', + 'status' => 'active', ]; $result = $this->get_config()->update_config( $config ); @@ -123,6 +137,8 @@ public function add( $args ): void { * $ wp mcp server remove "server-filesystem" * Success: Server removed. * + * @when before_wp_load + * * @param array $args Indexed array of positional arguments. */ public function remove( $args, $assoc_args ): void { @@ -172,9 +188,11 @@ public function remove( $args, $assoc_args ): void { * ## EXAMPLES * * # Remove server. - * $ wp mcp server update "server-filesystem" --status=disabled + * $ wp mcp server update "server-filesystem" --status=inactive * Success: Server updated. * + * @when before_wp_load + * * @param array $args Indexed array of positional arguments. */ public function update( $args, $assoc_args ): void { @@ -187,7 +205,7 @@ public function update( $args, $assoc_args ): void { foreach ( $config[ $args[0] ] as $key => $value ) { if ( isset( $assoc_args[ $key ] ) ) { if ( 'status' === $key ) { - $value = 'disabled' === $value ? 'enabled' : 'disabled'; + $value = 'inactive' === $value ? 'active' : 'inactive'; } $config[ $args[0] ][ $key ] = $value; } From 399f205bd6f966242c1ca13f4e3390c6fbbec963 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Mon, 7 Apr 2025 19:25:46 +0200 Subject: [PATCH 2/5] Fix composer dependency --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 21b8a86..7a66c00 100644 --- a/composer.json +++ b/composer.json @@ -7,7 +7,7 @@ "authors": [], "require": { "php": "^8.2", - "logiscape/mcp-sdk-php": "^1.0", + "logiscape/mcp-sdk-php": "dev-main", "mcaskill/composer-exclude-files": "^4.0", "mcp-wp/mcp-server": "dev-main", "wp-cli/wp-cli": "^2.11" From c4b98ff51f0c618ab58af262ec5dd4cddaeb536f Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Mon, 7 Apr 2025 19:35:42 +0200 Subject: [PATCH 3/5] Fixes --- features/mcp-server.feature | 11 ++++++++++- src/McpServerCommand.php | 2 +- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/features/mcp-server.feature b/features/mcp-server.feature index 2e2c050..f9ad417 100644 --- a/features/mcp-server.feature +++ b/features/mcp-server.feature @@ -1,5 +1,9 @@ Feature: MCP server command + Background: + Given an empty directory + And an empty {HOME} directory + Scenario: CRUD When I run `wp mcp server add foo "https://foo.example.com/mcp"` @@ -18,7 +22,12 @@ Feature: MCP server command | baz | https://baz.example.com/mcp | active | When I run `wp mcp server remove bar baz` - And I run `wp mcp server list` + Then STDOUT should contain: + """ + Success: Removed 2 of 2 servers. + """ + + When I run `wp mcp server list` Then STDOUT should contain: """ foo.example.com diff --git a/src/McpServerCommand.php b/src/McpServerCommand.php index 62b41d1..9c36672 100644 --- a/src/McpServerCommand.php +++ b/src/McpServerCommand.php @@ -159,7 +159,7 @@ public function remove( $args, $assoc_args ): void { WP_CLI::warning( "Server '$server' not found." ); ++$errors; } else { - unset( $config[ $args[0] ] ); + unset( $config[ $server ] ); ++$successes; } } From 400372fb856f4ea95160f5496041814f750a9be5 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Mon, 7 Apr 2025 19:45:32 +0200 Subject: [PATCH 4/5] more tests --- features/mcp-server.feature | 15 +++++++++++++-- src/McpServerCommand.php | 6 ++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/features/mcp-server.feature b/features/mcp-server.feature index f9ad417..43676c1 100644 --- a/features/mcp-server.feature +++ b/features/mcp-server.feature @@ -5,7 +5,6 @@ Feature: MCP server command And an empty {HOME} directory Scenario: CRUD - When I run `wp mcp server add foo "https://foo.example.com/mcp"` Then STDOUT should contain: """ @@ -58,7 +57,8 @@ Feature: MCP server command When I run `wp mcp server list --status=inactive` Then STDOUT should be a table containing rows: | name | server | status | - | baz | https://baz.example.com/mcp | active | + | bar | https://bar.example.com/mcp | inactive | + And STDOUT should not contain: """ foo.example.com @@ -67,3 +67,14 @@ Feature: MCP server command """ baz.example.com """ + + When I run `wp mcp server update foo --name=fabulous` + Then STDOUT should contain: + """ + Server updated. + """ + + When I run `wp mcp server list` + Then STDOUT should be a table containing rows: + | name | server | status | + | fabulous | https://foo.example.com/mcp | active | diff --git a/src/McpServerCommand.php b/src/McpServerCommand.php index 9c36672..55f8a01 100644 --- a/src/McpServerCommand.php +++ b/src/McpServerCommand.php @@ -211,6 +211,12 @@ public function update( $args, $assoc_args ): void { } } + // Special case for renaming an entry. + if ( isset( $assoc_args['name'] ) ) { + $config[ $assoc_args['name'] ] = $config[ $args[0] ]; + unset( $config[ $args[0] ] ); + } + $result = $this->get_config()->update_config( $config ); if ( ! $result ) { From d09aeba7c6d4da425f0948ee080ce0c9e930c518 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Mon, 7 Apr 2025 19:47:35 +0200 Subject: [PATCH 5/5] Formatting fix --- src/McpServerCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/McpServerCommand.php b/src/McpServerCommand.php index 55f8a01..bd24181 100644 --- a/src/McpServerCommand.php +++ b/src/McpServerCommand.php @@ -213,7 +213,7 @@ public function update( $args, $assoc_args ): void { // Special case for renaming an entry. if ( isset( $assoc_args['name'] ) ) { - $config[ $assoc_args['name'] ] = $config[ $args[0] ]; + $config[ $assoc_args['name'] ] = $config[ $args[0] ]; unset( $config[ $args[0] ] ); }