Repository navigation
Expand file tree
/
Copy pathRoot.php
More file actions
80 lines (70 loc) · 2.34 KB
/
Copy pathRoot.php
File metadata and controls
80 lines (70 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php
/*
* This file is part of the official PHP MCP SDK.
*
* A collaboration between Symfony and the PHP Foundation.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Mcp\Schema;
use Mcp\Schema\Exception\InvalidArgumentException;
/**
* Represents a root directory or file that the server can operate on.
*
* @phpstan-type RootData array{
* uri: string,
* name?: string,
* }
*
* @author Kyrian Obikwelu <koshnawaza@gmail.com>
*
* @deprecated since protocol revision 2026-07-28 (SEP-2577), earliest removal 2027-07-28.
* Pass directories or files through tool arguments, resource
* URIs or server configuration instead.
*/
class Root implements \JsonSerializable
{
private const URI_PATTERN = '/^file:\/\/.*$/';
/**
* @param string $uri The URI identifying the root. This *must* start with file:// for now.
*
* This restriction may be relaxed in future versions of the protocol to allow other URI schemes.
* @param string|null $name An optional name for the root.
*
* This can be used to provide a human-readable identifier for the root, which may be useful for
* display purposes or for referencing the root in other parts of the application.
*/
public function __construct(
public readonly string $uri,
public readonly ?string $name = null,
) {
if (!preg_match(self::URI_PATTERN, $this->uri)) {
throw new InvalidArgumentException(\sprintf('Root URI must start with "file://". Given: "%s".', $this->uri));
}
}
/**
* @param RootData|array<mixed> $data
*/
public static function fromArray(array $data): self
{
if (empty($data['uri']) || !\is_string($data['uri'])) {
throw new InvalidArgumentException('Invalid or missing "uri" in Root data.');
}
if (isset($data['name']) && !\is_string($data['name'])) {
throw new InvalidArgumentException('Invalid "name" in Root data.');
}
return new self($data['uri'], $data['name'] ?? null);
}
/**
* @return RootData
*/
public function jsonSerialize(): array
{
$data = ['uri' => $this->uri];
if (null !== $this->name) {
$data['name'] = $this->name;
}
return $data;
}
}