| 1: | <?php |
| 2: | |
| 3: | declare(strict_types=1); |
| 4: | |
| 5: | |
| 6: | |
| 7: | |
| 8: | |
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | namespace Nexus\Mcp\Core\Schema\RequestParams; |
| 15: | |
| 16: | use Nexus\Assert\Assert; |
| 17: | use Nexus\Mcp\Core\Schema\ClientCapabilities; |
| 18: | use Nexus\Mcp\Core\Schema\Implementation; |
| 19: | use Nexus\Mcp\Core\Schema\ProtocolVersion; |
| 20: | use Nexus\Mcp\Core\Schema\RequestMetaObject; |
| 21: | use Nexus\Mcp\Core\Schema\RequestParams; |
| 22: | |
| 23: | |
| 24: | |
| 25: | |
| 26: | |
| 27: | |
| 28: | final readonly class InitializeRequestParams extends RequestParams |
| 29: | { |
| 30: | public function __construct( |
| 31: | public ProtocolVersion $protocolVersion, |
| 32: | public ClientCapabilities $capabilities, |
| 33: | public Implementation $clientInfo, |
| 34: | RequestMetaObject $meta = new RequestMetaObject(), |
| 35: | ) { |
| 36: | parent::__construct($meta); |
| 37: | } |
| 38: | |
| 39: | |
| 40: | |
| 41: | |
| 42: | #[\Override] |
| 43: | public static function fromArray(array $data): static |
| 44: | { |
| 45: | Assert::that($data)->hasOffset('protocolVersion', 'missing the required "protocolVersion" key.'); |
| 46: | $protocolVersion = $data['protocolVersion']; |
| 47: | Assert::that($protocolVersion)->isString('"params.protocolVersion" must be a string, {type} given.'); |
| 48: | |
| 49: | Assert::that($data)->hasOffset('capabilities', 'missing the required "capabilities" key.'); |
| 50: | Assert::that($data['capabilities']) |
| 51: | ->isArray('"params.capabilities" must be an object, {type} given.') |
| 52: | ->isMap('"params.capabilities" must be a string-keyed object.') |
| 53: | ; |
| 54: | |
| 55: | Assert::that($data)->hasOffset('clientInfo', 'missing the required "clientInfo" key.'); |
| 56: | Assert::that($data['clientInfo']) |
| 57: | ->isArray('"params.clientInfo" must be an object, {type} given.') |
| 58: | ->isMap('"params.clientInfo" must be a string-keyed object.') |
| 59: | ; |
| 60: | |
| 61: | $meta = new RequestMetaObject(); |
| 62: | |
| 63: | if (\array_key_exists('_meta', $data)) { |
| 64: | Assert::that($data['_meta']) |
| 65: | ->isArray('"params._meta" must be an object, {type} given.') |
| 66: | ->isMap('"params._meta" must be a string-keyed object.') |
| 67: | ; |
| 68: | $meta = RequestMetaObject::fromArray($data['_meta']); |
| 69: | } |
| 70: | |
| 71: | return new self( |
| 72: | new ProtocolVersion($protocolVersion), |
| 73: | ClientCapabilities::fromArray($data['capabilities']), |
| 74: | Implementation::fromArray($data['clientInfo']), |
| 75: | $meta, |
| 76: | ); |
| 77: | } |
| 78: | |
| 79: | #[\Override] |
| 80: | public function toArray(): array |
| 81: | { |
| 82: | return [ |
| 83: | ...parent::toArray(), |
| 84: | 'protocolVersion' => $this->protocolVersion->version, |
| 85: | 'capabilities' => $this->capabilities->toArray(), |
| 86: | 'clientInfo' => $this->clientInfo->toArray(), |
| 87: | ]; |
| 88: | } |
| 89: | |
| 90: | #[\Override] |
| 91: | public function jsonSerialize(): array |
| 92: | { |
| 93: | return [ |
| 94: | ...parent::toArray(), |
| 95: | 'protocolVersion' => $this->protocolVersion->version, |
| 96: | 'capabilities' => $this->capabilities->jsonSerialize(), |
| 97: | 'clientInfo' => $this->clientInfo->jsonSerialize(), |
| 98: | ]; |
| 99: | } |
| 100: | } |
| 101: | |