1: <?php
2:
3: declare(strict_types=1);
4:
5: /**
6: * This file is part of the Nexus MCP SDK package.
7: *
8: * (c) 2026 John Paul E. Balandan, CPA <paulbalandan@gmail.com>
9: *
10: * For the full copyright and license information, please view
11: * the LICENSE file that was distributed with this source code.
12: */
13:
14: namespace Nexus\Mcp\Core\Schema\Result;
15:
16: use Nexus\Assert\Assert;
17: use Nexus\Mcp\Core\Schema\MetaObject;
18: use Nexus\Mcp\Core\Schema\Prompt\PromptMessage;
19: use Nexus\Mcp\Core\Schema\Result;
20:
21: /**
22: * The server's response to a prompts/get request from the client.
23: *
24: * @see https://modelcontextprotocol.io/specification/2025-11-25/schema#getpromptresult
25: */
26: final readonly class GetPromptResult extends Result implements ServerResult
27: {
28: /**
29: * @var list<PromptMessage>
30: */
31: public array $messages;
32:
33: /**
34: * @var null|non-empty-string
35: */
36: public ?string $description;
37:
38: /**
39: * @param list<PromptMessage> $messages
40: */
41: public function __construct(array $messages, ?string $description = null, MetaObject $meta = new MetaObject())
42: {
43: Assert::that($messages)->isList('"result.messages" must be a list, non-list array given.');
44:
45: foreach ($messages as $message) {
46: Assert::that($message)->isInstanceOf(PromptMessage::class);
47: }
48:
49: Assert::that($description)->nullOr()->isNonEmptyString('"result.description" must be a non-empty string or null.');
50:
51: $this->messages = $messages;
52: $this->description = $description;
53:
54: parent::__construct($meta);
55: }
56:
57: /**
58: * @param array<string, mixed> $data
59: */
60: #[\Override]
61: public static function fromArray(array $data): static
62: {
63: Assert::that($data)->hasOffset('messages', '"result" missing the required "messages" key.');
64: Assert::that($data['messages'])->isArray('"result.messages" must be an array, {type} given.');
65:
66: $messages = [];
67:
68: foreach ($data['messages'] as $entry) {
69: Assert::that($entry)
70: ->isArray('"result.message" must be an object, {type} given.')
71: ->isMap('"result.message" must be a string-keyed object.')
72: ;
73: $messages[] = PromptMessage::fromArray($entry);
74: }
75:
76: $description = $data['description'] ?? null;
77: Assert::that($description)->nullOr()->isString('"result.description" must be a string or null, {type} given.');
78:
79: $meta = new MetaObject();
80:
81: if (\array_key_exists('_meta', $data)) {
82: Assert::that($data['_meta'])
83: ->isArray('"result._meta" must be an object, {type} given.')
84: ->isMap('"result._meta" must be a string-keyed object.')
85: ;
86: $meta = MetaObject::fromArray($data['_meta']);
87: }
88:
89: return new self($messages, $description, $meta);
90: }
91:
92: #[\Override]
93: public function toArray(): array
94: {
95: $data = [
96: ...parent::toArray(),
97: 'messages' => array_map(static fn(PromptMessage $message): array => $message->toArray(), $this->messages),
98: ];
99:
100: if (null !== $this->description) {
101: $data['description'] = $this->description;
102: }
103:
104: return $data;
105: }
106:
107: #[\Override]
108: public function jsonSerialize(): array
109: {
110: return $this->toArray();
111: }
112: }
113: