1: <?php
2:
3: declare(strict_types=1);
4:
5: /**
6: * This file is part of the Nexus MCP SDK package.
7: *
8: * (c) 2025 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\Schema;
15:
16: use Nexus\Mcp\Schema\Resource\ResourceValidator;
17:
18: /**
19: * Represents a root directory or file that the server can operate on.
20: *
21: * @implements Arrayable<array{
22: * _meta?: array<string, mixed>,
23: * uri: non-empty-string,
24: * name?: non-empty-string,
25: * }>
26: */
27: final readonly class Root implements \JsonSerializable, Arrayable
28: {
29: /**
30: * @param non-empty-string $uri The URI identifying the root.
31: * @param null|non-empty-string $name An optional name for the root.
32: * @param null|array<string, mixed> $meta Reserved by MCP to allow clients and servers to attach
33: * additional metadata to their interactions.
34: */
35: public function __construct(
36: public string $uri,
37: public ?string $name = null,
38: public ?array $meta = null,
39: ) {
40: if (! ResourceValidator::isValidUri($this->uri) || ! str_starts_with($this->uri, 'file://')) {
41: throw new \InvalidArgumentException('The root URI must be a valid file URI.');
42: }
43: }
44:
45: #[\Override]
46: public function toArray(): array
47: {
48: return array_filter([
49: '_meta' => $this->meta,
50: 'uri' => $this->uri,
51: 'name' => $this->name,
52: ], static fn(mixed $value): bool => null !== $value);
53: }
54:
55: /**
56: * @return template-type<self, Arrayable, 'T'>
57: */
58: #[\Override]
59: public function jsonSerialize(): array
60: {
61: return $this->toArray();
62: }
63: }
64: