Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
10 changes: 0 additions & 10 deletions features/load-wp-cli.feature

This file was deleted.

80 changes: 80 additions & 0 deletions features/mcp-server.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
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"`
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`
Then STDOUT should contain:
"""
Success: Removed 2 of 2 servers.
"""

When 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 |
| bar | https://bar.example.com/mcp | inactive |

And STDOUT should not contain:
"""
foo.example.com
"""
And STDOUT should not contain:
"""
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 |
32 changes: 28 additions & 4 deletions src/McpServerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ class McpServerCommand extends WP_CLI_Command {
*
* ## OPTIONS
*
* [--<field>=<value>]
* : Filter results by key=value pairs.
*
* [--format=<format>]
* : Render output in a particular format.
* ---
Expand All @@ -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.
*/
Expand All @@ -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'],
Expand Down Expand Up @@ -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 {
Expand All @@ -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 );
Expand Down Expand Up @@ -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 {
Expand All @@ -143,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;
}
}
Expand Down Expand Up @@ -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 {
Expand All @@ -187,12 +205,18 @@ 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;
}
}

// 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 ) {
Expand Down