Repository navigation
Expand file tree
/
Copy pathClientCapabilities.php
More file actions
199 lines (182 loc) · 7.13 KB
/
Copy pathClientCapabilities.php
File metadata and controls
199 lines (182 loc) · 7.13 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<?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;
/**
* Capabilities a client may support. Known capabilities are defined here, in this schema, but this is not a closed set:
* any client can define its own, additional capabilities.
*
* @author Kyrian Obikwelu <koshnawaza@gmail.com>
*/
class ClientCapabilities implements \JsonSerializable
{
/**
* @param array<string, mixed> $experimental
* @param ?array<string, mixed> $extensions protocol extensions the client supports (e.g. io.modelcontextprotocol/ui)
* @param ?bool $samplingContext the `sampling.context` sub-capability
* @param ?bool $samplingTools the `sampling.tools` sub-capability
* @param ?bool $elicitationForm The `elicitation.form` sub-capability. Implied by declaring
* `elicitation` without naming any mode.
* @param ?bool $elicitationUrl the `elicitation.url` sub-capability
*
* The sub-capabilities trail `extensions` rather than sitting next to `sampling` and
* `elicitation` so that existing positional calls keep working. Pass them by name.
*/
public function __construct(
public readonly ?bool $roots = false,
public readonly ?bool $rootsListChanged = null,
public readonly ?bool $sampling = null,
public readonly ?bool $elicitation = null,
public readonly ?array $experimental = null,
public readonly ?array $extensions = null,
public readonly ?bool $samplingContext = null,
public readonly ?bool $samplingTools = null,
public readonly ?bool $elicitationForm = null,
public readonly ?bool $elicitationUrl = null,
) {
}
/**
* @param array{
* roots?: array{
* listChanged?: bool,
* },
* sampling?: array{context?: mixed, tools?: mixed}|object,
* elicitation?: array{form?: mixed, url?: mixed}|object|bool,
* experimental?: array<string, mixed>,
* extensions?: array<string, mixed>,
* } $data
*/
public static function fromArray(array $data): self
{
$rootsEnabled = isset($data['roots']);
$rootsListChanged = null;
if ($rootsEnabled) {
if (\is_array($data['roots']) && \array_key_exists('listChanged', $data['roots'])) {
$rootsListChanged = (bool) $data['roots']['listChanged'];
} elseif (\is_object($data['roots']) && property_exists($data['roots'], 'listChanged')) {
$rootsListChanged = (bool) $data['roots']->listChanged;
}
}
$sampling = null;
$samplingContext = null;
$samplingTools = null;
if (isset($data['sampling'])) {
$sampling = true;
if (\is_array($data['sampling'])) {
$samplingContext = isset($data['sampling']['context']);
$samplingTools = isset($data['sampling']['tools']);
} elseif (\is_object($data['sampling'])) {
$samplingContext = property_exists($data['sampling'], 'context');
$samplingTools = property_exists($data['sampling'], 'tools');
}
}
$elicitation = null;
$elicitationForm = null;
$elicitationUrl = null;
if (isset($data['elicitation'])) {
$elicitation = true;
$elicitationUrl = self::namesMode($data['elicitation'], 'url');
// Form mode is the backwards-compatible default: an `elicitation` capability
// naming no mode at all means form, the only shape that existed before `url`.
// Naming any mode is an explicit statement, so `{"url": {}}` is not form.
$elicitationForm = self::namesMode($data['elicitation'], 'form') || !$elicitationUrl;
}
return new self(
$rootsEnabled,
$rootsListChanged,
$sampling,
$elicitation,
\is_array($data['experimental'] ?? null) ? $data['experimental'] : null,
\is_array($data['extensions'] ?? null) ? $data['extensions'] : null,
$samplingContext,
$samplingTools,
$elicitationForm,
$elicitationUrl,
);
}
/**
* A mode is declared by the presence of a (possibly empty) object, so only the
* key matters — not whatever it holds. A boolean `elicitation` names none.
*/
private static function namesMode(mixed $capability, string $name): bool
{
if (\is_array($capability)) {
return \array_key_exists($name, $capability);
}
if (\is_object($capability)) {
return property_exists($capability, $name);
}
return false;
}
/**
* @param array<string, mixed> $extensions
*/
public function withExtensions(array $extensions): self
{
return new self(
$this->roots,
$this->rootsListChanged,
$this->sampling,
$this->elicitation,
$this->experimental,
array_replace($this->extensions ?? [], $extensions),
$this->samplingContext,
$this->samplingTools,
$this->elicitationForm,
$this->elicitationUrl,
);
}
/**
* @return array{
* roots?: \stdClass,
* sampling?: \stdClass,
* elicitation?: \stdClass,
* experimental?: \stdClass,
* extensions?: \stdClass,
* }|\stdClass
*/
public function jsonSerialize(): array|object
{
$data = [];
if ($this->roots || $this->rootsListChanged) {
$data['roots'] = new \stdClass();
if ($this->rootsListChanged) {
$data['roots']->listChanged = $this->rootsListChanged;
}
}
if ($this->sampling || $this->samplingContext || $this->samplingTools) {
$data['sampling'] = new \stdClass();
if ($this->samplingContext) {
$data['sampling']->context = new \stdClass();
}
if ($this->samplingTools) {
$data['sampling']->tools = new \stdClass();
}
}
if ($this->elicitation || $this->elicitationForm || $this->elicitationUrl) {
$data['elicitation'] = new \stdClass();
if ($this->elicitationForm) {
$data['elicitation']->form = new \stdClass();
}
if ($this->elicitationUrl) {
$data['elicitation']->url = new \stdClass();
}
}
if ($this->experimental) {
$data['experimental'] = (object) $this->experimental;
}
if ($this->extensions) {
$data['extensions'] = (object) array_map(
static fn (mixed $settings): mixed => \is_array($settings) ? (object) $settings : $settings,
$this->extensions,
);
}
return $data ?: new \stdClass();
}
}