commercebird
/
vendor
/
wordpress
/
php-mcp-schema
/
src
/
Server
/
Prompts
/
DTO
/
GetPromptResult.php
GetPromptRequest.php
3 months ago
GetPromptRequestParams.php
3 months ago
GetPromptResult.php
3 months ago
ListPromptsRequest.php
3 months ago
ListPromptsResult.php
3 months ago
Prompt.php
3 months ago
PromptArgument.php
3 months ago
PromptListChangedNotification.php
3 months ago
PromptMessage.php
3 months ago
GetPromptResult.php
119 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace WP\McpSchema\Server\Prompts\DTO; |
| 6 | |
| 7 | use WP\McpSchema\Common\Protocol\DTO\Result; |
| 8 | use WP\McpSchema\Common\Traits\ValidatesRequiredFields; |
| 9 | use WP\McpSchema\Server\Lifecycle\Union\ServerResultInterface; |
| 10 | |
| 11 | /** |
| 12 | * The server's response to a prompts/get request from the client. |
| 13 | * |
| 14 | * @since 2024-11-05 |
| 15 | * @last-updated 2025-11-25 (modified property: messages) |
| 16 | * |
| 17 | * @mcp-domain Server |
| 18 | * @mcp-subdomain Prompts |
| 19 | * @mcp-version 2025-11-25 |
| 20 | */ |
| 21 | class GetPromptResult extends Result implements ServerResultInterface |
| 22 | { |
| 23 | use ValidatesRequiredFields; |
| 24 | |
| 25 | /** |
| 26 | * An optional description for the prompt. |
| 27 | * |
| 28 | * @since 2024-11-05 |
| 29 | * |
| 30 | * @var string|null |
| 31 | */ |
| 32 | protected ?string $description; |
| 33 | |
| 34 | /** |
| 35 | * @since 2024-11-05 |
| 36 | * |
| 37 | * @var array<\WP\McpSchema\Server\Prompts\DTO\PromptMessage> |
| 38 | */ |
| 39 | protected array $messages; |
| 40 | |
| 41 | /** |
| 42 | * @param array<\WP\McpSchema\Server\Prompts\DTO\PromptMessage> $messages @since 2024-11-05 |
| 43 | * @param array<string, mixed>|null $_meta @since 2024-11-05 |
| 44 | * @param string|null $description @since 2024-11-05 |
| 45 | */ |
| 46 | public function __construct( |
| 47 | array $messages, |
| 48 | ?array $_meta = null, |
| 49 | ?string $description = null |
| 50 | ) { |
| 51 | parent::__construct($_meta); |
| 52 | $this->messages = $messages; |
| 53 | $this->description = $description; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Creates an instance from an array. |
| 58 | * |
| 59 | * @param array{ |
| 60 | * _meta?: array<string, mixed>|null, |
| 61 | * description?: string|null, |
| 62 | * messages: array<array<string, mixed>|\WP\McpSchema\Server\Prompts\DTO\PromptMessage> |
| 63 | * } $data |
| 64 | * @phpstan-param array<string, mixed> $data |
| 65 | * @return self |
| 66 | */ |
| 67 | public static function fromArray(array $data): self |
| 68 | { |
| 69 | self::assertRequired($data, ['messages']); |
| 70 | |
| 71 | /** @var array<\WP\McpSchema\Server\Prompts\DTO\PromptMessage> $messages */ |
| 72 | $messages = array_map( |
| 73 | static fn($item) => is_array($item) |
| 74 | ? PromptMessage::fromArray($item) |
| 75 | : $item, |
| 76 | self::asArray($data['messages']) |
| 77 | ); |
| 78 | |
| 79 | return new self( |
| 80 | $messages, |
| 81 | self::asArrayOrNull($data['_meta'] ?? null), |
| 82 | self::asStringOrNull($data['description'] ?? null) |
| 83 | ); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Converts the instance to an array. |
| 88 | * |
| 89 | * @return array<string, mixed> |
| 90 | */ |
| 91 | public function toArray(): array |
| 92 | { |
| 93 | $result = parent::toArray(); |
| 94 | |
| 95 | if ($this->description !== null) { |
| 96 | $result['description'] = $this->description; |
| 97 | } |
| 98 | $result['messages'] = array_map(static fn($item) => $item->toArray(), $this->messages); |
| 99 | |
| 100 | return $result; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * @return string|null |
| 105 | */ |
| 106 | public function getDescription(): ?string |
| 107 | { |
| 108 | return $this->description; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * @return array<\WP\McpSchema\Server\Prompts\DTO\PromptMessage> |
| 113 | */ |
| 114 | public function getMessages(): array |
| 115 | { |
| 116 | return $this->messages; |
| 117 | } |
| 118 | } |
| 119 |