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;
15:
16: use Nexus\Assert\Assert;
17:
18: /**
19: * Represents a root directory or file that the server can operate on.
20: *
21: * @implements Arrayable<array{
22: * uri: string,
23: * name?: string,
24: * _meta?: template-type<MetaObject, Arrayable, 'T'>,
25: * }>
26: *
27: * @see https://modelcontextprotocol.io/specification/2025-11-25/schema#root
28: */
29: final readonly class Root implements Arrayable
30: {
31: public function __construct(
32: public string $uri,
33: public ?string $name = null,
34: public MetaObject $meta = new MetaObject(),
35: ) {
36: Assert::that($uri)->startsWith('file://', 'root "uri" must start with {needle}, got {value}.');
37: }
38:
39: /**
40: * @param array<string, mixed> $data
41: */
42: #[\Override]
43: public static function fromArray(array $data): static
44: {
45: Assert::that($data)->hasOffset('uri', 'root missing the required "uri" key.');
46: $uri = $data['uri'];
47: Assert::that($uri)->isString('root "uri" must be a string, {type} given.');
48:
49: $name = $data['name'] ?? null;
50: Assert::that($name)->nullOr()->isString('root "name" must be a string or null, {type} given.');
51:
52: $meta = new MetaObject();
53:
54: if (\array_key_exists('_meta', $data)) {
55: Assert::that($data['_meta'])
56: ->isArray('root "_meta" must be an object, {type} given.')
57: ->isMap('root "_meta" must be a string-keyed object.')
58: ;
59: $meta = MetaObject::fromArray($data['_meta']);
60: }
61:
62: return new self($uri, $name, $meta);
63: }
64:
65: #[\Override]
66: public function toArray(): array
67: {
68: $data = ['uri' => $this->uri];
69:
70: if (null !== $this->name) {
71: $data['name'] = $this->name;
72: }
73:
74: $meta = $this->meta->toArray();
75:
76: if ([] !== $meta) {
77: $data['_meta'] = $meta;
78: }
79:
80: return $data;
81: }
82:
83: #[\Override]
84: public function jsonSerialize(): array
85: {
86: return $this->toArray();
87: }
88: }
89: