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/draft/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: $name, title: $title);
39:
40: IdentifierNameValidator::validate($name, 'prompt reference "name"');
41: }
42:
43: #[\Override]
44: public static function fromArray(array $data): static
45: {
46: Assert::that($data)->hasOffset('type', 'prompt reference is missing the required "type" key.');
47: $type = $data['type'];
48: Assert::that($type)->isIdentical(self::TYPE, 'prompt reference "type" must be {other}, {value} given.');
49:
50: Assert::that($data)->hasOffset('name', 'prompt reference is missing the required "name" key.');
51: $name = $data['name'];
52: Assert::that($name)->isString('prompt reference "name" must be a string, {type} given.');
53:
54: $title = $data['title'] ?? null;
55: Assert::that($title)->nullOr()->isString('prompt reference "title" must be a string or null, {type} given.');
56:
57: return new self(name: $name, title: $title);
58: }
59:
60: #[\Override]
61: public function toArray(): array
62: {
63: $data = [
64: 'name' => $this->name,
65: 'type' => self::TYPE,
66: ];
67:
68: if (null !== $this->title) {
69: $data['title'] = $this->title;
70: }
71:
72: return $data;
73: }
74:
75: #[\Override]
76: public function jsonSerialize(): array
77: {
78: return $this->toArray();
79: }
80: }
81: