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