| 1: | <?php |
| 2: | |
| 3: | declare(strict_types=1); |
| 4: | |
| 5: | |
| 6: | |
| 7: | |
| 8: | |
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | namespace Nexus\Mcp\Core\Schema\Elicitation; |
| 15: | |
| 16: | use Nexus\Assert\Assert; |
| 17: | use Nexus\Mcp\Core\Schema\Arrayable; |
| 18: | use Nexus\Mcp\Core\Schema\Request; |
| 19: | use Nexus\Mcp\Core\Schema\Request\InputRequest; |
| 20: | use Nexus\Mcp\Core\Schema\RequestParams\ElicitRequestFormParams; |
| 21: | use Nexus\Mcp\Core\Schema\RequestParams\ElicitRequestUrlParams; |
| 22: | |
| 23: | |
| 24: | |
| 25: | |
| 26: | |
| 27: | |
| 28: | |
| 29: | |
| 30: | |
| 31: | |
| 32: | |
| 33: | |
| 34: | |
| 35: | |
| 36: | final readonly class ElicitRequest extends Request implements InputRequest |
| 37: | { |
| 38: | public function __construct(ElicitRequestFormParams|ElicitRequestUrlParams $params) |
| 39: | { |
| 40: | parent::__construct(params: $params); |
| 41: | } |
| 42: | |
| 43: | #[\Override] |
| 44: | public static function getMethod(): string |
| 45: | { |
| 46: | return 'elicitation/create'; |
| 47: | } |
| 48: | |
| 49: | #[\Override] |
| 50: | public static function fromArray(array $data): static |
| 51: | { |
| 52: | Assert::that($data)->hasOffset('params', 'elicit request is missing the required "params" key.'); |
| 53: | Assert::that($data['params']) |
| 54: | ->isArray('elicit request "params" must be an object, {type} given.') |
| 55: | ->isMap('elicit request "params" must be a string-keyed object.') |
| 56: | ; |
| 57: | |
| 58: | $mode = $data['params']['mode'] ?? ElicitRequestFormParams::MODE; |
| 59: | |
| 60: | $params = ElicitRequestUrlParams::MODE === $mode |
| 61: | ? ElicitRequestUrlParams::fromArray($data['params']) |
| 62: | : ElicitRequestFormParams::fromArray($data['params']); |
| 63: | |
| 64: | return new self(params: $params); |
| 65: | } |
| 66: | |
| 67: | #[\Override] |
| 68: | public function toArray(): array |
| 69: | { |
| 70: | return [ |
| 71: | 'method' => static::getMethod(), |
| 72: | 'params' => $this->params->toArray(), |
| 73: | ]; |
| 74: | } |
| 75: | |
| 76: | #[\Override] |
| 77: | public function jsonSerialize(): array |
| 78: | { |
| 79: | return [ |
| 80: | 'method' => static::getMethod(), |
| 81: | 'params' => $this->params->jsonSerialize(), |
| 82: | ]; |
| 83: | } |
| 84: | } |
| 85: | |