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\Resource;
15:
16: use Nexus\Assert\Assert;
17: use Nexus\Mcp\Core\Schema\Arrayable;
18: use Nexus\Mcp\Core\Validation\Rfc6570UriTemplateValidator;
19:
20: /**
21: * A reference to a resource or resource template definition.
22: *
23: * @implements Arrayable<array{
24: * type: 'ref/resource',
25: * uri: non-empty-string,
26: * }>
27: *
28: * @see https://modelcontextprotocol.io/specification/draft/schema#resourcetemplatereference
29: */
30: final readonly class ResourceTemplateReference implements Arrayable
31: {
32: public const string TYPE = 'ref/resource';
33:
34: /**
35: * @var non-empty-string
36: */
37: public string $uri;
38:
39: public function __construct(string $uri)
40: {
41: Rfc6570UriTemplateValidator::validate($uri, 'resource template reference "uri"');
42:
43: $this->uri = $uri;
44: }
45:
46: #[\Override]
47: public static function fromArray(array $data): static
48: {
49: Assert::that($data)->hasOffset('type', 'resource template reference is missing the required "type" key.');
50: $type = $data['type'];
51: Assert::that($type)->isIdentical(self::TYPE, 'resource template reference "type" must be {other}, {value} given.');
52:
53: Assert::that($data)->hasOffset('uri', 'resource template reference is missing the required "uri" key.');
54: $uri = $data['uri'];
55: Assert::that($uri)->isString('resource template reference "uri" must be a string, {type} given.');
56:
57: return new self(uri: $uri);
58: }
59:
60: #[\Override]
61: public function toArray(): array
62: {
63: return [
64: 'type' => self::TYPE,
65: 'uri' => $this->uri,
66: ];
67: }
68:
69: #[\Override]
70: public function jsonSerialize(): array
71: {
72: return $this->toArray();
73: }
74: }
75: