woocommerce
/
vendor
/
wordpress
/
mcp-adapter
/
includes
/
Handlers
/
Initialize
/
InitializeHandler.php
InitializeHandler.php
66 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Initialize method handler for MCP requests. |
| 4 | * |
| 5 | * @package McpAdapter |
| 6 | */ |
| 7 | |
| 8 | declare( strict_types=1 ); |
| 9 | |
| 10 | namespace WP\MCP\Handlers\Initialize; |
| 11 | |
| 12 | use WP\MCP\Core\McpServer; |
| 13 | use stdClass; |
| 14 | |
| 15 | /** |
| 16 | * Handles the initialize MCP method. |
| 17 | */ |
| 18 | class InitializeHandler { |
| 19 | /** |
| 20 | * The WordPress MCP instance. |
| 21 | * |
| 22 | * @var \WP\MCP\Core\McpServer |
| 23 | */ |
| 24 | private McpServer $mcp; |
| 25 | |
| 26 | /** |
| 27 | * Constructor. |
| 28 | * |
| 29 | * @param \WP\MCP\Core\McpServer $mcp The WordPress MCP instance. |
| 30 | */ |
| 31 | public function __construct( McpServer $mcp ) { |
| 32 | $this->mcp = $mcp; |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Handle the initialize request. |
| 37 | * |
| 38 | * @param int $request_id The request ID for JSON-RPC. |
| 39 | * |
| 40 | * @return array |
| 41 | */ |
| 42 | public function handle( int $request_id = 0 ): array { |
| 43 | $server_info = array( |
| 44 | 'name' => $this->mcp->get_server_name(), |
| 45 | 'version' => $this->mcp->get_server_version(), |
| 46 | ); |
| 47 | |
| 48 | // MCP 2025-06-18 compliant capabilities |
| 49 | $capabilities = array( |
| 50 | 'tools' => new stdClass(), // Empty object indicates support |
| 51 | 'resources' => new stdClass(), // Basic resources support without listChanged/subscribe |
| 52 | 'prompts' => new stdClass(), // Basic prompts support without listChanged |
| 53 | 'logging' => new stdClass(), // Server supports sending log messages to client |
| 54 | 'completions' => new stdClass(), // Server supports argument autocompletion (note: plural!) |
| 55 | ); |
| 56 | |
| 57 | // Send the response according to JSON-RPC 2.0 and InitializeResult schema. |
| 58 | return array( |
| 59 | 'protocolVersion' => '2025-06-18', |
| 60 | 'serverInfo' => $server_info, |
| 61 | 'capabilities' => (object) $capabilities, |
| 62 | 'instructions' => $this->mcp->get_server_description(), |
| 63 | ); |
| 64 | } |
| 65 | } |
| 66 |