-
Notifications
You must be signed in to change notification settings - Fork 143
Expand file tree
/
Copy pathServerCapabilities.php
More file actions
197 lines (180 loc) · 7.81 KB
/
ServerCapabilities.php
File metadata and controls
197 lines (180 loc) · 7.81 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
<?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)
*/
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,
) {
}
/**
* @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>,
* } $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,
);
}
/**
* 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],
);
}
/**
* @return array{
* logging?: object,
* completions?: object,
* prompts?: object,
* resources?: object,
* tools?: object,
* experimental?: object,
* extensions?: object,
* }
*/
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) {
$data['extensions'] = (object) $this->extensions;
}
return $data;
}
}