| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace WP\McpSchema\Server\Core\DTO; |
| 6 |
|
| 7 |
use WP\McpSchema\Common\JsonRpc\DTO\RequestParams; |
| 8 |
use WP\McpSchema\Common\JsonRpc\DTO\RequestParamsMeta; |
| 9 |
use WP\McpSchema\Common\Traits\ValidatesRequiredFields; |
| 10 |
|
| 11 |
/** |
| 12 |
* Parameters for a `completion/complete` request. |
| 13 |
* |
| 14 |
* @since 2025-11-25 |
| 15 |
* |
| 16 |
* @mcp-domain Server |
| 17 |
* @mcp-subdomain Core |
| 18 |
* @mcp-version 2025-11-25 |
| 19 |
*/ |
| 20 |
class CompleteRequestParams extends RequestParams |
| 21 |
{ |
| 22 |
use ValidatesRequiredFields; |
| 23 |
|
| 24 |
/** |
| 25 |
* @since 2025-11-25 |
| 26 |
* |
| 27 |
* @var \WP\McpSchema\Server\Core\DTO\PromptReference|\WP\McpSchema\Server\Core\DTO\ResourceTemplateReference |
| 28 |
*/ |
| 29 |
protected $ref; |
| 30 |
|
| 31 |
/** |
| 32 |
* The argument's information |
| 33 |
* |
| 34 |
* @since 2025-11-25 |
| 35 |
* |
| 36 |
* @var \WP\McpSchema\Server\Core\DTO\CompleteRequestParamsArgument |
| 37 |
*/ |
| 38 |
protected CompleteRequestParamsArgument $argument; |
| 39 |
|
| 40 |
/** |
| 41 |
* Additional, optional context for completions |
| 42 |
* |
| 43 |
* @since 2025-11-25 |
| 44 |
* |
| 45 |
* @var \WP\McpSchema\Server\Core\DTO\CompleteRequestParamsContext|null |
| 46 |
*/ |
| 47 |
protected ?CompleteRequestParamsContext $context; |
| 48 |
|
| 49 |
/** |
| 50 |
* @param \WP\McpSchema\Server\Core\DTO\PromptReference|\WP\McpSchema\Server\Core\DTO\ResourceTemplateReference $ref @since 2025-11-25 |
| 51 |
* @param \WP\McpSchema\Server\Core\DTO\CompleteRequestParamsArgument $argument @since 2025-11-25 |
| 52 |
* @param \WP\McpSchema\Common\JsonRpc\DTO\RequestParamsMeta|null $_meta @since 2025-11-25 |
| 53 |
* @param \WP\McpSchema\Server\Core\DTO\CompleteRequestParamsContext|null $context @since 2025-11-25 |
| 54 |
*/ |
| 55 |
public function __construct( |
| 56 |
$ref, |
| 57 |
CompleteRequestParamsArgument $argument, |
| 58 |
?RequestParamsMeta $_meta = null, |
| 59 |
?CompleteRequestParamsContext $context = null |
| 60 |
) { |
| 61 |
parent::__construct($_meta); |
| 62 |
$this->ref = $ref; |
| 63 |
$this->argument = $argument; |
| 64 |
$this->context = $context; |
| 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 |
* ref: \WP\McpSchema\Server\Core\DTO\PromptReference|\WP\McpSchema\Server\Core\DTO\ResourceTemplateReference, |
| 73 |
* argument: array<string, mixed>|\WP\McpSchema\Server\Core\DTO\CompleteRequestParamsArgument, |
| 74 |
* context?: array<string, mixed>|\WP\McpSchema\Server\Core\DTO\CompleteRequestParamsContext|null |
| 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, ['ref', 'argument']); |
| 82 |
|
| 83 |
/** @var \WP\McpSchema\Server\Core\DTO\PromptReference|\WP\McpSchema\Server\Core\DTO\ResourceTemplateReference $ref */ |
| 84 |
$ref = $data['ref']; |
| 85 |
|
| 86 |
/** @var \WP\McpSchema\Server\Core\DTO\CompleteRequestParamsArgument $argument */ |
| 87 |
$argument = is_array($data['argument']) |
| 88 |
? CompleteRequestParamsArgument::fromArray(self::asArray($data['argument'])) |
| 89 |
: $data['argument']; |
| 90 |
|
| 91 |
/** @var \WP\McpSchema\Common\JsonRpc\DTO\RequestParamsMeta|null $_meta */ |
| 92 |
$_meta = isset($data['_meta']) |
| 93 |
? (is_array($data['_meta']) |
| 94 |
? RequestParamsMeta::fromArray(self::asArray($data['_meta'])) |
| 95 |
: $data['_meta']) |
| 96 |
: null; |
| 97 |
|
| 98 |
/** @var \WP\McpSchema\Server\Core\DTO\CompleteRequestParamsContext|null $context */ |
| 99 |
$context = isset($data['context']) |
| 100 |
? (is_array($data['context']) |
| 101 |
? CompleteRequestParamsContext::fromArray(self::asArray($data['context'])) |
| 102 |
: $data['context']) |
| 103 |
: null; |
| 104 |
|
| 105 |
return new self( |
| 106 |
$ref, |
| 107 |
$argument, |
| 108 |
$_meta, |
| 109 |
$context |
| 110 |
); |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Converts the instance to an array. |
| 115 |
* |
| 116 |
* @return array<string, mixed> |
| 117 |
*/ |
| 118 |
public function toArray(): array |
| 119 |
{ |
| 120 |
$result = parent::toArray(); |
| 121 |
|
| 122 |
$result['ref'] = (is_object($this->ref) && method_exists($this->ref, 'toArray')) ? $this->ref->toArray() : $this->ref; |
| 123 |
$result['argument'] = $this->argument->toArray(); |
| 124 |
if ($this->context !== null) { |
| 125 |
$result['context'] = $this->context->toArray(); |
| 126 |
} |
| 127 |
|
| 128 |
return $result; |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* @return \WP\McpSchema\Server\Core\DTO\PromptReference|\WP\McpSchema\Server\Core\DTO\ResourceTemplateReference |
| 133 |
*/ |
| 134 |
public function getRef() |
| 135 |
{ |
| 136 |
return $this->ref; |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* @return \WP\McpSchema\Server\Core\DTO\CompleteRequestParamsArgument |
| 141 |
*/ |
| 142 |
public function getArgument(): CompleteRequestParamsArgument |
| 143 |
{ |
| 144 |
return $this->argument; |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* @return \WP\McpSchema\Server\Core\DTO\CompleteRequestParamsContext|null |
| 149 |
*/ |
| 150 |
public function getContext(): ?CompleteRequestParamsContext |
| 151 |
{ |
| 152 |
return $this->context; |
| 153 |
} |
| 154 |
} |
| 155 |
|