Repository navigation
Expand file tree
/
Copy pathServerCapabilities.php
More file actions
296 lines (274 loc) · 11.1 KB
/
Copy pathServerCapabilities.php
File metadata and controls
296 lines (274 loc) · 11.1 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
<?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 that a server may support. Known capabilities are defined here, in this schema, but this is not a closed
* set: any server can define its own, additional capabilities.
*
* @author Kyrian Obikwelu <koshnawaza@gmail.com>
*/
class ServerCapabilities implements \JsonSerializable
{
/**
* @param ?bool $tools server exposes callable tools
* @param ?bool $toolsListChanged server supports list changed notifications for tools
* @param ?bool $resources server provides readable resources
* @param ?bool $resourcesSubscribe server supports subscribing to changes in the list of resources
* @param ?bool $resourcesListChanged server supports list changed notifications for resources
* @param ?bool $prompts server provides prompts templates
* @param ?bool $promptsListChanged server supports list changed notifications for prompts
* @param ?bool $logging server emits structured log messages
* @param ?bool $completions Server supports argument autocompletion
* @param ?array<string, mixed> $experimental experimental, non-standard features that the server supports
* @param ?array<string, mixed> $extensions protocol extensions the server supports (e.g. io.modelcontextprotocol/ui)
* @param ?array<string, mixed> $tasks task-augmented requests the server supports, as the
* spec's nested object, e.g. `['list' => [], 'requests' => ['tools' => ['call' => []]]]`
*/
public function __construct(
public readonly ?bool $tools = true,
public readonly ?bool $toolsListChanged = false,
public readonly ?bool $resources = true,
public readonly ?bool $resourcesSubscribe = false,
public readonly ?bool $resourcesListChanged = false,
public readonly ?bool $prompts = true,
public readonly ?bool $promptsListChanged = false,
public readonly ?bool $logging = false,
public readonly ?bool $completions = false,
public readonly ?array $experimental = null,
public readonly ?array $extensions = null,
public readonly ?array $tasks = null,
) {
}
/**
* @param array{
* logging?: mixed,
* completions?: mixed,
* prompts?: array{listChanged?: bool}|object,
* resources?: array{listChanged?: bool, subscribe?: bool}|object,
* tools?: object|array{listChanged?: bool},
* experimental?: array<string, mixed>,
* extensions?: array<string, mixed>,
* tasks?: array<string, mixed>,
* } $data
*/
public static function fromArray(array $data): self
{
$loggingEnabled = isset($data['logging']);
$completionsEnabled = isset($data['completions']);
$toolsEnabled = isset($data['tools']);
$promptsEnabled = isset($data['prompts']);
$resourcesEnabled = isset($data['resources']);
$promptsListChanged = null;
if (isset($data['prompts'])) {
if (\is_array($data['prompts']) && \array_key_exists('listChanged', $data['prompts'])) {
$promptsListChanged = (bool) $data['prompts']['listChanged'];
} elseif (\is_object($data['prompts']) && property_exists($data['prompts'], 'listChanged')) {
$promptsListChanged = (bool) $data['prompts']->listChanged;
}
}
$resourcesSubscribe = null;
$resourcesListChanged = null;
if (isset($data['resources'])) {
if (\is_array($data['resources']) && \array_key_exists('subscribe', $data['resources'])) {
$resourcesSubscribe = (bool) $data['resources']['subscribe'];
} elseif (\is_object($data['resources']) && property_exists($data['resources'], 'subscribe')) {
$resourcesSubscribe = (bool) $data['resources']->subscribe;
}
if (\is_array($data['resources']) && \array_key_exists('listChanged', $data['resources'])) {
$resourcesListChanged = (bool) $data['resources']['listChanged'];
} elseif (\is_object($data['resources']) && property_exists($data['resources'], 'listChanged')) {
$resourcesListChanged = (bool) $data['resources']->listChanged;
}
}
$toolsListChanged = null;
if (isset($data['tools'])) {
if (\is_array($data['tools']) && \array_key_exists('listChanged', $data['tools'])) {
$toolsListChanged = (bool) $data['tools']['listChanged'];
} elseif (\is_object($data['tools']) && property_exists($data['tools'], 'listChanged')) {
$toolsListChanged = (bool) $data['tools']->listChanged;
}
}
return new self(
tools: $toolsEnabled,
toolsListChanged: $toolsListChanged,
resources: $resourcesEnabled,
resourcesSubscribe: $resourcesSubscribe,
resourcesListChanged: $resourcesListChanged,
prompts: $promptsEnabled,
promptsListChanged: $promptsListChanged,
logging: $loggingEnabled,
completions: $completionsEnabled,
experimental: \is_array($data['experimental'] ?? null) ? $data['experimental'] : null,
extensions: \is_array($data['extensions'] ?? null) ? $data['extensions'] : null,
tasks: \is_array($data['tasks'] ?? null) ? $data['tasks'] : null,
);
}
/**
* Returns a copy with the given protocol extensions merged into the existing ones.
*
* Entries in $extensions override existing ones sharing the same id.
*
* @param array<string, array<string, mixed>> $extensions
*/
public function withExtensions(array $extensions): self
{
return new self(
$this->tools,
$this->toolsListChanged,
$this->resources,
$this->resourcesSubscribe,
$this->resourcesListChanged,
$this->prompts,
$this->promptsListChanged,
$this->logging,
$this->completions,
$this->experimental,
[...$this->extensions ?? [], ...$extensions],
$this->tasks,
);
}
/**
* Returns a copy that announces no list-changed notifications.
*
* A kind announced only through its `listChanged` flag stays announced.
*/
public function withoutListChanged(): self
{
return new self(
$this->tools || $this->toolsListChanged,
false,
$this->resources || $this->resourcesListChanged,
$this->resourcesSubscribe,
false,
$this->prompts || $this->promptsListChanged,
false,
$this->logging,
$this->completions,
$this->experimental,
$this->extensions,
$this->tasks,
);
}
/**
* Returns a copy that does not announce resource subscriptions.
*
* Resources announced only through the `subscribe` flag stay announced.
*/
public function withoutResourcesSubscribe(): self
{
return new self(
$this->tools,
$this->toolsListChanged,
$this->resources || $this->resourcesSubscribe,
false,
$this->resourcesListChanged,
$this->prompts,
$this->promptsListChanged,
$this->logging,
$this->completions,
$this->experimental,
$this->extensions,
$this->tasks,
);
}
/**
* Returns a copy without what a server cannot offer without sessions: resource subscriptions and logging.
*
* Resources announced only through the `subscribe` flag stay announced.
*/
public function withoutSessionFeatures(): self
{
return new self(
$this->tools,
$this->toolsListChanged,
$this->resources || $this->resourcesSubscribe,
false,
$this->resourcesListChanged,
$this->prompts,
$this->promptsListChanged,
false,
$this->completions,
$this->experimental,
$this->extensions,
$this->tasks,
);
}
/**
* @return array{
* logging?: \stdClass,
* completions?: \stdClass,
* prompts?: \stdClass,
* resources?: \stdClass,
* tools?: \stdClass,
* experimental?: \stdClass,
* extensions?: \stdClass,
* tasks?: \stdClass,
* }
*/
public function jsonSerialize(): array
{
$data = [];
if ($this->logging) {
$data['logging'] = new \stdClass();
}
if ($this->completions) {
$data['completions'] = new \stdClass();
}
if ($this->prompts || $this->promptsListChanged) {
$data['prompts'] = new \stdClass();
if ($this->promptsListChanged) {
$data['prompts']->listChanged = $this->promptsListChanged;
}
}
if ($this->resources || $this->resourcesSubscribe || $this->resourcesListChanged) {
$data['resources'] = new \stdClass();
if ($this->resourcesSubscribe) {
$data['resources']->subscribe = $this->resourcesSubscribe;
}
if ($this->resourcesListChanged) {
$data['resources']->listChanged = $this->resourcesListChanged;
}
}
if ($this->tools || $this->toolsListChanged) {
$data['tools'] = new \stdClass();
if ($this->toolsListChanged) {
$data['tools']->listChanged = $this->toolsListChanged;
}
}
if ($this->experimental) {
$data['experimental'] = (object) $this->experimental;
}
if ($this->extensions) {
// Each entry is a settings *object*; an extension with no settings
// declares `{}`, and an empty PHP array would serialize as `[]`.
$data['extensions'] = (object) array_map(
static fn (mixed $settings): mixed => \is_array($settings) ? (object) $settings : $settings,
$this->extensions,
);
}
if (null !== $this->tasks) {
$data['tasks'] = self::toObject($this->tasks);
}
return $data;
}
/**
* Every node of the `tasks` capability is an object, most of them empty,
* which a PHP array would encode as `[]`.
*
* @param array<string, mixed> $data
*/
private static function toObject(array $data): \stdClass
{
return (object) array_map(
static fn (mixed $value): mixed => \is_array($value) ? self::toObject($value) : $value,
$data,
);
}
}