| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace WP\McpSchema\Common\Protocol\DTO; |
| 6 |
|
| 7 |
use WP\McpSchema\Client\Lifecycle\DTO\ClientCapabilities; |
| 8 |
use WP\McpSchema\Common\JsonRpc\DTO\RequestParams; |
| 9 |
use WP\McpSchema\Common\JsonRpc\DTO\RequestParamsMeta; |
| 10 |
use WP\McpSchema\Common\Lifecycle\DTO\Implementation; |
| 11 |
use WP\McpSchema\Common\Traits\ValidatesRequiredFields; |
| 12 |
|
| 13 |
/** |
| 14 |
* Parameters for an `initialize` request. |
| 15 |
* |
| 16 |
* @since 2025-11-25 |
| 17 |
* |
| 18 |
* @mcp-domain Common |
| 19 |
* @mcp-subdomain Protocol |
| 20 |
* @mcp-version 2025-11-25 |
| 21 |
*/ |
| 22 |
class InitializeRequestParams extends RequestParams |
| 23 |
{ |
| 24 |
use ValidatesRequiredFields; |
| 25 |
|
| 26 |
/** |
| 27 |
* The latest version of the Model Context Protocol that the client supports. The client MAY decide to support older versions as well. |
| 28 |
* |
| 29 |
* @since 2025-11-25 |
| 30 |
* |
| 31 |
* @var string |
| 32 |
*/ |
| 33 |
protected string $protocolVersion; |
| 34 |
|
| 35 |
/** |
| 36 |
* @since 2025-11-25 |
| 37 |
* |
| 38 |
* @var \WP\McpSchema\Client\Lifecycle\DTO\ClientCapabilities |
| 39 |
*/ |
| 40 |
protected ClientCapabilities $capabilities; |
| 41 |
|
| 42 |
/** |
| 43 |
* @since 2025-11-25 |
| 44 |
* |
| 45 |
* @var \WP\McpSchema\Common\Lifecycle\DTO\Implementation |
| 46 |
*/ |
| 47 |
protected Implementation $clientInfo; |
| 48 |
|
| 49 |
/** |
| 50 |
* @param string $protocolVersion @since 2025-11-25 |
| 51 |
* @param \WP\McpSchema\Client\Lifecycle\DTO\ClientCapabilities $capabilities @since 2025-11-25 |
| 52 |
* @param \WP\McpSchema\Common\Lifecycle\DTO\Implementation $clientInfo @since 2025-11-25 |
| 53 |
* @param \WP\McpSchema\Common\JsonRpc\DTO\RequestParamsMeta|null $_meta @since 2025-11-25 |
| 54 |
*/ |
| 55 |
public function __construct( |
| 56 |
string $protocolVersion, |
| 57 |
ClientCapabilities $capabilities, |
| 58 |
Implementation $clientInfo, |
| 59 |
?RequestParamsMeta $_meta = null |
| 60 |
) { |
| 61 |
parent::__construct($_meta); |
| 62 |
$this->protocolVersion = $protocolVersion; |
| 63 |
$this->capabilities = $capabilities; |
| 64 |
$this->clientInfo = $clientInfo; |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Creates an instance from an array. |
| 69 |
* |
| 70 |
* @param array{ |
| 71 |
* _meta?: array<string, mixed>|\WP\McpSchema\Common\JsonRpc\DTO\RequestParamsMeta|null, |
| 72 |
* protocolVersion: string, |
| 73 |
* capabilities: array<string, mixed>|\WP\McpSchema\Client\Lifecycle\DTO\ClientCapabilities, |
| 74 |
* clientInfo: array<string, mixed>|\WP\McpSchema\Common\Lifecycle\DTO\Implementation |
| 75 |
* } $data |
| 76 |
* @phpstan-param array<string, mixed> $data |
| 77 |
* @return self |
| 78 |
*/ |
| 79 |
public static function fromArray(array $data): self |
| 80 |
{ |
| 81 |
self::assertRequired($data, ['protocolVersion', 'capabilities', 'clientInfo']); |
| 82 |
|
| 83 |
/** @var \WP\McpSchema\Client\Lifecycle\DTO\ClientCapabilities $capabilities */ |
| 84 |
$capabilities = is_array($data['capabilities']) |
| 85 |
? ClientCapabilities::fromArray(self::asArray($data['capabilities'])) |
| 86 |
: $data['capabilities']; |
| 87 |
|
| 88 |
/** @var \WP\McpSchema\Common\Lifecycle\DTO\Implementation $clientInfo */ |
| 89 |
$clientInfo = is_array($data['clientInfo']) |
| 90 |
? Implementation::fromArray(self::asArray($data['clientInfo'])) |
| 91 |
: $data['clientInfo']; |
| 92 |
|
| 93 |
/** @var \WP\McpSchema\Common\JsonRpc\DTO\RequestParamsMeta|null $_meta */ |
| 94 |
$_meta = isset($data['_meta']) |
| 95 |
? (is_array($data['_meta']) |
| 96 |
? RequestParamsMeta::fromArray(self::asArray($data['_meta'])) |
| 97 |
: $data['_meta']) |
| 98 |
: null; |
| 99 |
|
| 100 |
return new self( |
| 101 |
self::asString($data['protocolVersion']), |
| 102 |
$capabilities, |
| 103 |
$clientInfo, |
| 104 |
$_meta |
| 105 |
); |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Converts the instance to an array. |
| 110 |
* |
| 111 |
* @return array<string, mixed> |
| 112 |
*/ |
| 113 |
public function toArray(): array |
| 114 |
{ |
| 115 |
$result = parent::toArray(); |
| 116 |
|
| 117 |
$result['protocolVersion'] = $this->protocolVersion; |
| 118 |
$result['capabilities'] = $this->capabilities->toArray(); |
| 119 |
$result['clientInfo'] = $this->clientInfo->toArray(); |
| 120 |
|
| 121 |
return $result; |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* @return string |
| 126 |
*/ |
| 127 |
public function getProtocolVersion(): string |
| 128 |
{ |
| 129 |
return $this->protocolVersion; |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* @return \WP\McpSchema\Client\Lifecycle\DTO\ClientCapabilities |
| 134 |
*/ |
| 135 |
public function getCapabilities(): ClientCapabilities |
| 136 |
{ |
| 137 |
return $this->capabilities; |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* @return \WP\McpSchema\Common\Lifecycle\DTO\Implementation |
| 142 |
*/ |
| 143 |
public function getClientInfo(): Implementation |
| 144 |
{ |
| 145 |
return $this->clientInfo; |
| 146 |
} |
| 147 |
} |
| 148 |
|