| 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 WP\MCP\Core\McpVersionNegotiator; |
| 14 |
use WP\McpSchema\Common\Lifecycle\DTO\Implementation; |
| 15 |
use WP\McpSchema\Common\Protocol\DTO\InitializeResult; |
| 16 |
use WP\McpSchema\Server\Lifecycle\DTO\ServerCapabilities; |
| 17 |
|
| 18 |
/** |
| 19 |
* Handles the initialize MCP method. |
| 20 |
*/ |
| 21 |
class InitializeHandler { |
| 22 |
/** |
| 23 |
* The WordPress MCP instance. |
| 24 |
* |
| 25 |
* @var \WP\MCP\Core\McpServer |
| 26 |
*/ |
| 27 |
private McpServer $mcp; |
| 28 |
|
| 29 |
/** |
| 30 |
* Constructor. |
| 31 |
* |
| 32 |
* @param \WP\MCP\Core\McpServer $mcp The WordPress MCP instance. |
| 33 |
*/ |
| 34 |
public function __construct( McpServer $mcp ) { |
| 35 |
$this->mcp = $mcp; |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Handles the initialize request. |
| 40 |
* |
| 41 |
* Negotiates the protocol version with the client using McpVersionNegotiator. |
| 42 |
* If the client requests a supported version, that version is used. Otherwise |
| 43 |
* the server falls back to the latest supported version. |
| 44 |
* |
| 45 |
* @since 0.5.0 |
| 46 |
* |
| 47 |
* @param string $client_protocol_version The protocol version requested by the client. |
| 48 |
* |
| 49 |
* @return \WP\McpSchema\Common\Protocol\DTO\InitializeResult Response with server capabilities and information. |
| 50 |
*/ |
| 51 |
public function handle( string $client_protocol_version ): InitializeResult { |
| 52 |
$negotiated_version = McpVersionNegotiator::negotiate( $client_protocol_version ); |
| 53 |
|
| 54 |
$server_info = Implementation::fromArray( |
| 55 |
array( |
| 56 |
'name' => $this->mcp->get_server_name(), |
| 57 |
'version' => $this->mcp->get_server_version(), |
| 58 |
) |
| 59 |
); |
| 60 |
|
| 61 |
// Capabilities should only be advertised if they are implemented end-to-end. |
| 62 |
// IMPORTANT: We set explicit boolean values (not empty arrays) to ensure proper JSON serialization. |
| 63 |
// Empty arrays `[]` serialize as JSON arrays `[]`, but MCP spec requires JSON objects `{}`. |
| 64 |
// Setting explicit values like `listChanged: false` produces associative arrays that serialize correctly. |
| 65 |
$capabilities = ServerCapabilities::fromArray( |
| 66 |
array( |
| 67 |
'prompts' => array( 'listChanged' => false ), |
| 68 |
'resources' => array( |
| 69 |
'subscribe' => false, |
| 70 |
'listChanged' => false, |
| 71 |
), |
| 72 |
'tools' => array( 'listChanged' => false ), |
| 73 |
) |
| 74 |
); |
| 75 |
|
| 76 |
$result = InitializeResult::fromArray( |
| 77 |
array( |
| 78 |
'protocolVersion' => $negotiated_version, |
| 79 |
'capabilities' => $capabilities, |
| 80 |
'serverInfo' => $server_info, |
| 81 |
'instructions' => $this->mcp->get_server_description(), |
| 82 |
) |
| 83 |
); |
| 84 |
|
| 85 |
/** |
| 86 |
* Filters the initialize response before returning to the client. |
| 87 |
* |
| 88 |
* Use this filter to modify server capabilities, instructions, or |
| 89 |
* other initialization data dynamically. To modify the result, call |
| 90 |
* `$result->toArray()`, change the data, and return |
| 91 |
* `InitializeResult::fromArray( $modified_data )`. |
| 92 |
* |
| 93 |
* @since 0.5.0 |
| 94 |
* |
| 95 |
* @param \WP\McpSchema\Common\Protocol\DTO\InitializeResult $result The initialize result DTO. |
| 96 |
* @param \WP\MCP\Core\McpServer $server The MCP server instance. |
| 97 |
*/ |
| 98 |
return apply_filters( 'mcp_adapter_initialize_response', $result, $this->mcp ); |
| 99 |
} |
| 100 |
} |
| 101 |
|