PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / 3.4.3
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages v3.4.3
3.4.3 3.4.2 3.4.1 3.4.0 3.3.9 3.3.8 3.3.7 3.3.6 3.3.5 3.3.4 3.3.3 3.3.2 3.3.1 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.2.7 2.2.8 2.2.9 2.3.0 2.3.1 All 196 releases
convertkit / vendor / wordpress / mcp-adapter / includes / Cli / McpCommand.php

McpCommand.php in Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages 3.4.3, at vendor/wordpress/mcp-adapter/includes/Cli/McpCommand.php

150 lines 3.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * WP-CLI Command for MCP STDIO Transport
4 *
5 * @package McpAdapter
6 */
7
8 declare( strict_types=1 );
9
10 namespace WP\MCP\Cli;
11
12 use WP\MCP\Core\McpAdapter;
13 use function WP_CLI\Utils\format_items;
14
15 /**
16 * Manage MCP servers via WP-CLI.
17 *
18 * Provides commands to serve MCP servers over STDIO transport for
19 * communication with MCP clients via subprocess.
20 */
21 class McpCommand extends \WP_CLI_Command { // phpcs:ignore
22
23 /**
24 * Serve an MCP server via STDIO transport.
25 *
26 * This command starts an MCP server that communicates via standard input/output
27 * using the JSON-RPC 2.0 protocol. It's designed to be launched as a subprocess
28 * by MCP clients.
29 *
30 * Use the global `--user` flag to specify the user context for the server. If not provided, runs as unauthenticated (limited capabilities).
31 *
32 * ## OPTIONS
33 *
34 * [--server=<server-id>]
35 * : The ID of the MCP server to serve. If not specified, uses the first available server.
36 *
37 * ## EXAMPLES
38 *
39 * # Serve the default MCP server as admin user
40 * wp mcp serve --user=admin
41 *
42 * # Serve a specific server as user with ID 1
43 * wp mcp serve --server=my-mcp-server --user=1
44 *
45 * # Serve without authentication (limited capabilities)
46 * wp mcp serve --server=public-server
47 *
48 * @when after_wp_load
49 * @synopsis [--server=<server-id>]
50 */
51 public function serve( array $args, array $assoc_args ): void {
52
53 // Get the MCP adapter instance
54 $adapter = McpAdapter::instance();
55
56 // Get all registered servers
57 $servers = $adapter->get_servers();
58
59 if ( empty( $servers ) ) {
60 \WP_CLI::error( 'No MCP servers are registered. Please register at least one server first.' );
61 }
62
63 // Determine which server to use
64 $server_id = $assoc_args['server'] ?? null;
65 $server = null;
66
67 if ( $server_id ) {
68 $server = $adapter->get_server( $server_id );
69 if ( ! $server ) {
70 \WP_CLI::error( sprintf( 'Server with ID "%s" not found.', $server_id ) );
71 }
72 } else {
73 // Use the first available server
74 $server = array_values( $servers )[0];
75 $server_id = $server->get_server_id();
76 \WP_CLI::debug( sprintf( 'Using server: %s', $server_id ) );
77 }
78
79 // Create and start STDIO server bridge
80 try {
81 \WP_CLI::debug( sprintf( 'Starting STDIO bridge for server: %s', $server_id ) );
82
83 // Create STDIO server bridge
84 $stdio_bridge = new StdioServerBridge( $server );
85
86 // Start serving (this blocks until terminated)
87 $stdio_bridge->serve();
88 } catch ( \RuntimeException $e ) {
89 \WP_CLI::error( $e->getMessage() );
90 } catch ( \Throwable $e ) {
91 \WP_CLI::error( 'Failed to start STDIO bridge: ' . $e->getMessage() );
92 }
93 }
94
95 /**
96 * List all registered MCP servers.
97 *
98 * ## OPTIONS
99 *
100 * [--format=<format>]
101 * : Render output in a particular format.
102 * ---
103 * default: table
104 * options:
105 * - table
106 * - csv
107 * - json
108 * - yaml
109 * ---
110 *
111 * ## EXAMPLES
112 *
113 * # List all MCP servers
114 * wp mcp list
115 *
116 * # List servers in JSON format
117 * wp mcp list --format=json
118 *
119 * @when after_wp_load
120 * @synopsis [--format=<format>]
121 */
122 public function list( array $args, array $assoc_args ): void {
123 $adapter = McpAdapter::instance();
124
125 $servers = $adapter->get_servers();
126
127 if ( empty( $servers ) ) {
128 \WP_CLI::line( 'No MCP servers registered.' );
129
130 return;
131 }
132
133 $items = array();
134 foreach ( $servers as $server ) {
135 $items[] = array(
136 'ID' => $server->get_server_id(),
137 'Name' => $server->get_server_name(),
138 'Version' => $server->get_server_version(),
139 'Tools' => count( $server->get_tools() ),
140 'Resources' => count( $server->get_resources() ),
141 'Prompts' => count( $server->get_prompts() ),
142 'Description' => $server->get_server_description(),
143 );
144 }
145
146 $format = $assoc_args['format'] ?? 'table';
147 format_items( $format, $items, array( 'ID', 'Name', 'Version', 'Tools', 'Resources', 'Prompts' ) );
148 }
149 }
150