Skip to content

Commit 2c57b42

Browse files
committed
Allow disabling individual servers
Makes testing a bit easier
1 parent 8d77999 commit 2c57b42

File tree

2 files changed

+100
-23
lines changed

2 files changed

+100
-23
lines changed

src/AiCommand.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -148,19 +148,25 @@ public function get_sessions( bool $with_wp_server, bool $with_cli_server ): arr
148148
$servers = array_values( ( new McpConfig() )->get_config() );
149149

150150
foreach ( $servers as $args ) {
151-
if ( str_starts_with( $args, 'http://' ) || str_starts_with( $args, 'https://' ) ) {
151+
if ( 'enabled' !== $args['status'] ) {
152+
continue;
153+
}
154+
155+
$server = $args['server'];
156+
157+
if ( str_starts_with( $server, 'http://' ) || str_starts_with( $server, 'https://' ) ) {
152158
$sessions[] = ( new Client( new CliLogger() ) )->connect(
153-
$args
159+
$server
154160
);
155161
continue;
156162
}
157163

158-
$args = explode( ' ', $args );
159-
$cmd_or_url = array_shift( $args );
164+
$server = explode( ' ', $server );
165+
$cmd_or_url = array_shift( $server );
160166

161167
$sessions[] = ( new Client( new CliLogger() ) )->connect(
162168
$cmd_or_url,
163-
$args,
169+
$server,
164170
);
165171
}
166172

src/McpServerCommand.php

Lines changed: 89 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
namespace McpWp\AiCommand;
44

55
use McpWp\AiCommand\Utils\McpConfig;
6+
use WP_CLI;
67
use WP_CLI\Formatter;
78
use WP_CLI_Command;
9+
use WP_CLI\Utils;
810

911
/**
1012
* MCP Server command.
@@ -51,7 +53,8 @@ public function list_( $args, $assoc_args ): void {
5153
foreach ( $config as $name => $server ) {
5254
$servers[] = [
5355
'name' => $name,
54-
'server' => $server,
56+
'server' => $server['server'],
57+
'status' => $server['status'],
5558
];
5659
}
5760

@@ -86,26 +89,33 @@ public function add( $args ): void {
8689
$config = $this->get_config()->get_config();
8790

8891
if ( isset( $config[ $args[0] ] ) ) {
89-
\WP_CLI::error( 'Server already exists.' );
92+
WP_CLI::error( 'Server already exists.' );
9093
} else {
91-
$config[ $args[0] ] = $args[1];
92-
$result = $this->get_config()->update_config( $config );
94+
$config[ $args[0] ] = [
95+
'server' => $args[1],
96+
'status' => 'enabled',
97+
];
98+
99+
$result = $this->get_config()->update_config( $config );
93100

94101
if ( ! $result ) {
95-
\WP_CLI::error( 'Could not add server.' );
102+
WP_CLI::error( 'Could not add server.' );
96103
} else {
97-
\WP_CLI::success( 'Server added.' );
104+
WP_CLI::success( 'Server added.' );
98105
}
99106
}
100107
}
101108

102109
/**
103-
* Remove a new MCP server from the list
110+
* Remove one or more MCP servers.
104111
*
105112
* ## OPTIONS
106113
*
107-
* <name>
108-
* : Name of the server to remove
114+
* [<name>...]
115+
* : One or more servers to remove
116+
*
117+
* [--all]
118+
* : Whether to remove all servers.
109119
*
110120
* ## EXAMPLES
111121
*
@@ -115,21 +125,81 @@ public function add( $args ): void {
115125
*
116126
* @param array $args Indexed array of positional arguments.
117127
*/
118-
public function remove( $args ): void {
128+
public function remove( $args, $assoc_args ): void {
129+
$all = Utils\get_flag_value( $assoc_args, 'all', false );
130+
131+
if ( ! $all && empty( $args ) ) {
132+
WP_CLI::error( 'Please specify one or more servers, or use --all.' );
133+
}
134+
119135
$config = $this->get_config()->get_config();
120136

121-
if ( ! array_key_exists( $args[0], $config ) ) {
122-
\WP_CLI::error( 'Server not found.' );
123-
} else {
124-
unset( $config[ $args[0] ] );
125-
$result = $this->get_config()->update_config( $config );
137+
$successes = 0;
138+
$errors = 0;
139+
$count = count( $args );
126140

127-
if ( ! $result ) {
128-
\WP_CLI::error( 'Could not remove server.' );
141+
foreach ( $args as $server ) {
142+
if ( ! array_key_exists( $server, $config ) ) {
143+
WP_CLI::warning( "Server '$server' not found." );
144+
++$errors;
129145
} else {
130-
\WP_CLI::success( 'Server removed.' );
146+
unset( $config[ $args[0] ] );
147+
++$successes;
148+
}
149+
}
150+
151+
$result = $this->get_config()->update_config( $config );
152+
153+
if ( ! $result ) {
154+
$successes = 0;
155+
$errors = $count;
156+
}
157+
158+
Utils\report_batch_operation_results( 'server', 'remove', $count, $successes, $errors );
159+
}
160+
161+
/**
162+
* Update an MCP server.
163+
*
164+
* ## OPTIONS
165+
*
166+
* <name>
167+
* : Name of the server.
168+
*
169+
* --<field>=<value>
170+
* : One or more fields to update.
171+
*
172+
* ## EXAMPLES
173+
*
174+
* # Remove server.
175+
* $ wp mcp server update "server-filesystem" --status=disabled
176+
* Success: Server updated.
177+
*
178+
* @param array $args Indexed array of positional arguments.
179+
*/
180+
public function update( $args, $assoc_args ): void {
181+
$config = $this->get_config()->get_config();
182+
183+
if ( ! isset( $config[ $args[0] ] ) ) {
184+
WP_CLI::error( "Server '$args[0]' not found." );
185+
}
186+
187+
foreach ( $config[ $args[0] ] as $key => $value ) {
188+
if ( isset( $assoc_args[ $key ] ) ) {
189+
if ( 'status' === $key ) {
190+
$value = 'disabled' === $value ? 'enabled' : 'disabled';
191+
}
192+
$config[ $args[0] ][ $key ] = $value;
131193
}
132194
}
195+
196+
$result = $this->get_config()->update_config( $config );
197+
198+
if ( ! $result ) {
199+
WP_CLI::error( 'Could not update server.' );
200+
} else {
201+
WP_CLI::success( 'Server updated.' );
202+
}
133203
}
134204

135205
/**
@@ -144,6 +214,7 @@ protected function get_formatter( &$assoc_args ) {
144214
[
145215
'name',
146216
'server',
217+
'status',
147218
]
148219
);
149220
}

0 commit comments

Comments
 (0)