Skip to content

Commit 5ac3c28

Browse files
committed
Revamp server config format
1 parent 350e120 commit 5ac3c28

File tree

3 files changed

+133
-58
lines changed

3 files changed

+133
-58
lines changed

src/AiCommand.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,9 @@ public function get_sessions( bool $with_wp_server, bool $with_cli_server ): arr
168168
);
169169
}
170170

171-
$servers = ( new McpConfig() )->get_config();
171+
$servers = ( new McpConfig() )->get_servers();
172172

173-
foreach ( $servers as $name => $args ) {
173+
foreach ( $servers as $args ) {
174174
if ( 'active' !== $args['status'] ) {
175175
continue;
176176
}
@@ -187,7 +187,7 @@ public function get_sessions( bool $with_wp_server, bool $with_cli_server ): arr
187187
$server = explode( ' ', $server );
188188
$cmd_or_url = array_shift( $server );
189189

190-
$sessions[ $name ] = ( new Client( new CliLogger() ) )->connect(
190+
$sessions[ $args['name'] ] = ( new Client( new CliLogger() ) )->connect(
191191
$cmd_or_url,
192192
$server,
193193
);

src/McpServerCommand.php

Lines changed: 22 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -51,23 +51,19 @@ class McpServerCommand extends WP_CLI_Command {
5151
* @param array<string, string> $assoc_args Associative arguments.
5252
*/
5353
public function list_( $args, $assoc_args ): void {
54-
$config = $this->get_config()->get_config();
54+
$_servers = $this->get_config()->get_servers();
5555

5656
$servers = [];
5757

58-
foreach ( $config as $name => $server ) {
58+
foreach ( $_servers as $server ) {
5959
// Support features like --status=active.
6060
foreach ( array_keys( $server ) as $field ) {
6161
if ( isset( $assoc_args[ $field ] ) && ! in_array( $server[ $field ], array_map( 'trim', explode( ',', $assoc_args[ $field ] ) ), true ) ) {
6262
continue 2;
6363
}
6464
}
6565

66-
$servers[] = [
67-
'name' => $name,
68-
'server' => $server['server'],
69-
'status' => $server['status'],
70-
];
66+
$servers[] = $server;
7167
}
7268

7369
$formatter = $this->get_formatter( $assoc_args );
@@ -100,23 +96,18 @@ public function list_( $args, $assoc_args ): void {
10096
* @param string[] $args Indexed array of positional arguments.
10197
*/
10298
public function add( $args ): void {
103-
$config = $this->get_config()->get_config();
104-
105-
if ( isset( $config[ $args[0] ] ) ) {
99+
if ( $this->get_config()->has_server( $args[0] ) ) {
106100
WP_CLI::error( 'Server already exists.' );
107101
} else {
108-
$config[ $args[0] ] = [
109-
'server' => $args[1],
110-
'status' => 'active',
111-
];
112-
113-
$result = $this->get_config()->update_config( $config );
114-
115-
if ( ! $result ) {
116-
WP_CLI::error( 'Could not add server.' );
117-
} else {
118-
WP_CLI::success( 'Server added.' );
119-
}
102+
$this->get_config()->add_server(
103+
[
104+
'name' => $args[0],
105+
'server' => $args[1],
106+
'status' => 'active',
107+
]
108+
);
109+
110+
WP_CLI::success( 'Server added.' );
120111
}
121112
}
122113

@@ -149,29 +140,20 @@ public function remove( $args, $assoc_args ): void {
149140
WP_CLI::error( 'Please specify one or more servers, or use --all.' );
150141
}
151142

152-
$config = $this->get_config()->get_config();
153-
154143
$successes = 0;
155144
$errors = 0;
156145
$count = count( $args );
157146

158147
foreach ( $args as $server ) {
159-
if ( ! array_key_exists( $server, $config ) ) {
148+
if ( ! $this->get_config()->has_server( $server ) ) {
160149
WP_CLI::warning( "Server '$server' not found." );
161150
++$errors;
162151
} else {
163-
unset( $config[ $server ] );
152+
$this->get_config()->remove_server( $server );
164153
++$successes;
165154
}
166155
}
167156

168-
$result = $this->get_config()->update_config( $config );
169-
170-
if ( ! $result ) {
171-
$successes = 0;
172-
$errors = $count;
173-
}
174-
175157
Utils\report_batch_operation_results( 'server', 'remove', $count, $successes, $errors );
176158
}
177159

@@ -198,34 +180,25 @@ public function remove( $args, $assoc_args ): void {
198180
* @param array<string, string> $assoc_args Associative arguments.
199181
*/
200182
public function update( $args, array $assoc_args ): void {
201-
$config = $this->get_config()->get_config();
183+
$server = $this->get_config()->get_server( $args[0] );
202184

203-
if ( ! isset( $config[ $args[0] ] ) ) {
185+
if ( null === $server ) {
204186
WP_CLI::error( "Server '$args[0]' not found." );
187+
return;
205188
}
206189

207-
foreach ( $config[ $args[0] ] as $key => $value ) {
190+
foreach ( $server as $key => $value ) {
208191
if ( isset( $assoc_args[ $key ] ) ) {
209192
if ( 'status' === $key ) {
210193
$value = 'inactive' === $value ? 'active' : 'inactive';
211194
}
212-
$config[ $args[0] ][ $key ] = $value;
195+
$server[ $key ] = $value;
213196
}
214197
}
215198

216-
// Special case for renaming an entry.
217-
if ( isset( $assoc_args['name'] ) ) {
218-
$config[ $assoc_args['name'] ] = $config[ $args[0] ];
219-
unset( $config[ $args[0] ] );
220-
}
221-
222-
$result = $this->get_config()->update_config( $config );
199+
$this->get_config()->update_server( $args[0], $server );
223200

224-
if ( ! $result ) {
225-
WP_CLI::error( 'Could not update server.' );
226-
} else {
227-
WP_CLI::success( 'Server updated.' );
228-
}
201+
WP_CLI::success( 'Server updated.' );
229202
}
230203

231204
/**

src/Utils/McpConfig.php

Lines changed: 108 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,132 @@
88
/**
99
* McpConfig class.
1010
*
11-
* @phpstan-type McpConfigData array<string, array{server: string, status: string}>
11+
* @phpstan-type McpConfigServer array{name: string, server: string, status: string}
12+
* @phpstan-type McpConfigData array{servers: McpConfigServer[]}
1213
*/
1314
class McpConfig extends WP_CLI_Command {
15+
/**
16+
* Returns a list of all servers.
17+
*
18+
* @return McpConfigServer[] List of servers.
19+
*/
20+
public function get_servers() {
21+
return $this->get_config()['servers'];
22+
}
23+
24+
/**
25+
* Returns a server with the given name.
26+
*
27+
* @param string $name Server name.
28+
* @return McpConfigServer|null Server if found, null otherwise.
29+
*/
30+
public function get_server( string $name ): ?array {
31+
$config = $this->get_config();
32+
foreach ( $config['servers'] as $server ) {
33+
if ( $name === $server['name'] ) {
34+
return $server;
35+
}
36+
}
37+
38+
return null;
39+
}
40+
41+
/**
42+
* Determines whether a server with the given name exists in the config.
43+
*
44+
* @param string $name Server name.
45+
* @return bool Whether the server exists.
46+
*/
47+
public function has_server( string $name ): bool {
48+
return $this->get_server( $name ) !== null;
49+
}
50+
51+
/**
52+
* Adds a new server to the list.
53+
*
54+
* @param McpConfigServer $server Server data.
55+
* @return void
56+
*/
57+
public function add_server( array $server ): void {
58+
$config = $this->get_config();
59+
$config['servers'][] = $server;
60+
$this->update_config( $config );
61+
}
62+
63+
/**
64+
* Updates a specific server in the config.
65+
* @param string $name Server name.
66+
* @param McpConfigServer $server Server data.
67+
* @return void
68+
*/
69+
public function update_server( string $name, array $server ): void {
70+
$config = $this->get_config();
71+
foreach ( $config['servers'] as &$_server ) {
72+
if ( $name === $_server['name'] ) {
73+
$_server = $server;
74+
}
75+
}
76+
77+
unset( $_server );
78+
79+
$this->update_config( $config );
80+
}
81+
82+
/**
83+
* Removes a given server from the config.
84+
*
85+
* @param string $name Server name.
86+
* @return void
87+
*/
88+
public function remove_server( string $name ): void {
89+
$config = $this->get_config();
90+
91+
foreach ( $config['servers'] as $key => $server ) {
92+
if ( $name === $server['name'] ) {
93+
unset( $config['servers'][ $key ] );
94+
}
95+
}
96+
97+
$this->update_config( $config );
98+
}
99+
14100
/**
15101
* Returns the current MCP config.
16102
*
17103
* @return array Config data.
18104
* @phpstan-return McpConfigData
19105
*/
20-
public function get_config() {
106+
protected function get_config() {
21107
$config_file = Utils\get_home_dir() . '/.wp-cli/ai-command.json';
22108

23109
if ( ! file_exists( $config_file ) ) {
24-
return [];
110+
return [
111+
'servers' => [],
112+
];
25113
}
26114

27115
$json_content = file_get_contents( $config_file );
28116

29117
if ( false === $json_content ) {
30-
return [];
118+
return [
119+
'servers' => [],
120+
];
31121
}
32122

33123
$config = json_decode( $json_content, true, 512, JSON_THROW_ON_ERROR );
34-
return null !== $config ? (array) $config : [];
124+
125+
if ( null === $config ) {
126+
return [
127+
'servers' => [],
128+
];
129+
}
130+
131+
/**
132+
* Loaded config.
133+
*
134+
* @var McpConfigData $config
135+
*/
136+
return $config;
35137
}
36138

37139
/**
@@ -41,7 +143,7 @@ public function get_config() {
41143
* @return bool Whether updating was successful.
42144
* @phpstan-param McpConfigData $new_config
43145
*/
44-
public function update_config( $new_config ): bool {
146+
protected function update_config( $new_config ): bool {
45147
$config_file = Utils\get_home_dir() . '/.wp-cli/ai-command.json';
46148

47149
if ( ! file_exists( $config_file ) ) {

0 commit comments

Comments
 (0)