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\Prompt;
15:
16: use Nexus\Assert\Assert;
17: use Nexus\Mcp\Core\Schema\Arrayable;
18: use Nexus\Mcp\Core\Schema\BaseMetadata;
19:
20: /**
21: * Identifies a prompt.
22: *
23: * @implements Arrayable<array{
24: * name: non-empty-string,
25: * type: 'ref/prompt',
26: * title?: non-empty-string,
27: * }>
28: *
29: * @see https://modelcontextprotocol.io/specification/2026-07-28/schema#promptreference
30: */
31: final readonly class PromptReference extends BaseMetadata implements Arrayable
32: {
33: public const string TYPE = 'ref/prompt';
34:
35: #[\Override]
36: public static function fromArray(array $data): static
37: {
38: Assert::that($data)->hasOffset('type', 'prompt reference is missing the required "type" key.');
39: $type = $data['type'];
40: Assert::that($type)->isIdentical(self::TYPE, 'prompt reference "type" must be {other}, {value} given.');
41:
42: Assert::that($data)->hasOffset('name', 'prompt reference is missing the required "name" key.');
43: $name = $data['name'];
44: Assert::that($name)->isNonEmptyString('prompt reference "name" must be a non-empty string, {type} given.');
45:
46: $title = $data['title'] ?? null;
47: Assert::that($title)->nullOr()->isNonEmptyString('prompt reference "title" must be a non-empty string or null, {type} given.');
48:
49: return new self(name: $name, title: $title);
50: }
51:
52: #[\Override]
53: public function toArray(): array
54: {
55: $data = [
56: 'name' => $this->name,
57: 'type' => self::TYPE,
58: ];
59:
60: if (null !== $this->title) {
61: $data['title'] = $this->title;
62: }
63:
64: return $data;
65: }
66:
67: #[\Override]
68: public function jsonSerialize(): array
69: {
70: return $this->toArray();
71: }
72: }
73: