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\Arrayable;
18: use Nexus\Mcp\Core\Schema\Enum\ResultType;
19: use Nexus\Mcp\Core\Schema\MetaObject;
20: use Nexus\Mcp\Core\Schema\MetaObject\GenericResultMetaObject;
21: use Nexus\Mcp\Core\Schema\MetaObject\ResultMetaObject;
22: use Nexus\Mcp\Core\Schema\Prompt\PromptMessage;
23: use Nexus\Mcp\Core\Schema\Result;
24:
25: /**
26: * The result returned by the server for a `prompts/get` request.
27: *
28: * @extends Result<array{
29: * _meta?: template-type<ResultMetaObject, MetaObject, 'T'>,
30: * resultType: non-empty-string,
31: * description?: non-empty-string,
32: * messages: list<template-type<PromptMessage, Arrayable, 'T'>>,
33: * }>
34: *
35: * @see https://modelcontextprotocol.io/specification/2026-07-28/schema#getpromptresult
36: */
37: final readonly class GetPromptResult extends Result implements ServerResult
38: {
39: /**
40: * @param list<PromptMessage> $messages
41: * @param null|non-empty-string $description
42: */
43: public function __construct(
44: public array $messages,
45: public ?string $description = null,
46: ResultMetaObject $meta = new GenericResultMetaObject(),
47: ) {
48: Assert::that($messages)->isList('"result.messages" must be a list, non-list array given.');
49:
50: foreach ($messages as $message) {
51: Assert::that($message)->isInstanceOf(PromptMessage::class);
52: }
53:
54: Assert::that($description)->nullOr()->isNonEmptyString('"result.description" must be a non-empty string or null.');
55:
56: parent::__construct(meta: $meta);
57: }
58:
59: #[\Override]
60: public static function fromArray(array $data): static
61: {
62: Assert::that($data)->hasOffset('messages', '"result" is missing the required "messages" key.');
63: Assert::that($data['messages'])->isArray('"result.messages" must be an array, {type} given.');
64:
65: $messages = [];
66:
67: foreach ($data['messages'] as $entry) {
68: Assert::that($entry)
69: ->isArray('"result.message" must be an object, {type} given.')
70: ->isMap('"result.message" must be a string-keyed object.')
71: ;
72: $messages[] = PromptMessage::fromArray($entry);
73: }
74:
75: $description = $data['description'] ?? null;
76: Assert::that($description)->nullOr()->isNonEmptyString('"result.description" must be a non-empty string or null, {type} given.');
77:
78: $meta = new GenericResultMetaObject();
79:
80: if (\array_key_exists('_meta', $data)) {
81: Assert::that($data['_meta'])
82: ->isArray('"result._meta" must be an object, {type} given.')
83: ->not()->isNonEmptyList('"result._meta" must be a string-keyed object.')
84: ;
85: $meta = GenericResultMetaObject::fromArray($data['_meta']);
86: }
87:
88: return new self(messages: $messages, description: $description, meta: $meta);
89: }
90:
91: #[\Override]
92: public function toArray(): array
93: {
94: $data = [];
95: $meta = $this->meta->toArray();
96:
97: if ([] !== $meta) {
98: $data['_meta'] = $meta;
99: }
100:
101: $data['resultType'] = self::getResultType();
102:
103: if (null !== $this->description) {
104: $data['description'] = $this->description;
105: }
106:
107: $data['messages'] = array_map(
108: static fn(PromptMessage $message): array => $message->toArray(),
109: $this->messages,
110: );
111:
112: return $data;
113: }
114:
115: #[\Override]
116: public function rebuildWithMeta(ResultMetaObject $meta): static
117: {
118: return new self(
119: messages: $this->messages,
120: description: $this->description,
121: meta: $meta,
122: );
123: }
124:
125: #[\Override]
126: protected function getResultType(): string
127: {
128: return ResultType::Complete->value;
129: }
130: }
131: