|
12 | 12 | class ServerCapabilities implements JsonSerializable |
13 | 13 | { |
14 | 14 | /** |
15 | | - * Present if the server supports sending log messages to the client. |
16 | | - */ |
17 | | - public readonly ?array $logging; |
18 | | - |
19 | | - /** |
20 | | - * Present if the server supports argument autocompletion suggestions. |
21 | | - */ |
22 | | - public readonly ?array $completions; |
23 | | - |
24 | | - /** |
25 | | - * Present if the server offers any prompt templates. |
26 | | - * @var array{ listChanged: bool }|null |
27 | | - */ |
28 | | - public readonly ?array $prompts; |
29 | | - |
30 | | - /** |
31 | | - * Present if the server offers any resources. |
32 | | - * @var array{ subscribe: bool, listChanged: bool }|null |
33 | | - */ |
34 | | - public readonly ?array $resources; |
35 | | - |
36 | | - /** |
37 | | - * Present if the server offers any tools. |
38 | | - * @var array{ listChanged: bool }|null |
39 | | - */ |
40 | | - public readonly ?array $tools; |
41 | | - |
42 | | - /** |
43 | | - * Experimental, non-standard capabilities that the server supports. |
44 | | - */ |
45 | | - public readonly ?array $experimental; |
46 | | - |
47 | | - /** |
48 | | - * @param bool $toolsEnabled Server supports tools. |
49 | | - * @param bool $toolsListChanged Server supports sending a notification when the list of tools changes. |
50 | | - * @param bool $resourcesEnabled Server supports resources. |
51 | | - * @param bool $resourcesSubscribe Server supports subscribing to changes in the list of resources. |
52 | | - * @param bool $resourcesListChanged Server supports sending a notification when the list of resources changes. |
53 | | - * @param bool $promptsEnabled Server supports prompts. |
54 | | - * @param bool $promptsListChanged Server supports sending a notification when the list of prompts changes. |
55 | | - * @param bool $loggingEnabled Server supports sending log messages to the client. |
56 | | - * @param bool $completionsEnabled Server supports argument autocompletion suggestions. |
57 | | - * @param array|null $experimental Experimental, non-standard capabilities that the server supports. |
| 15 | + * @param bool|null $tools Server exposes callable tools. |
| 16 | + * @param bool|null $toolsListChanged Server supports list changed notifications for tools. |
| 17 | + * @param bool|null $resources Server provides readable resources. |
| 18 | + * @param bool|null $resourcesSubscribe Server supports subscribing to changes in the list of resources. |
| 19 | + * @param bool|null $resourcesListChanged Server supports list changed notifications for resources. |
| 20 | + * @param bool|null $prompts Server provides prompts templates. |
| 21 | + * @param bool|null $promptsListChanged Server supports list changed notifications for prompts. |
| 22 | + * @param bool|null $logging Server emits structured log messages. |
| 23 | + * @param bool|null $completions Server supports argument autocompletion |
| 24 | + * @param array|null $experimental Experimental, non-standard features that the server supports. |
58 | 25 | */ |
59 | 26 | public function __construct( |
60 | | - public readonly bool $toolsEnabled = true, |
61 | | - public readonly bool $toolsListChanged = false, |
62 | | - public readonly bool $resourcesEnabled = true, |
63 | | - public readonly bool $resourcesSubscribe = false, |
64 | | - public readonly bool $resourcesListChanged = false, |
65 | | - public readonly bool $promptsEnabled = true, |
66 | | - public readonly bool $promptsListChanged = false, |
67 | | - public readonly bool $loggingEnabled = false, |
68 | | - public readonly bool $completionsEnabled = false, |
69 | | - ?array $experimental = null |
70 | | - ) { |
71 | | - $this->logging = $loggingEnabled ? [] : null; |
72 | | - $this->completions = $completionsEnabled ? [] : null; |
73 | | - |
74 | | - if ($promptsEnabled) { |
75 | | - $this->prompts = ['listChanged' => $promptsListChanged]; |
76 | | - } else { |
77 | | - $this->prompts = null; |
78 | | - } |
79 | | - |
80 | | - if ($resourcesEnabled) { |
81 | | - $resources = []; |
82 | | - if ($resourcesSubscribe) { |
83 | | - $resources['subscribe'] = $resourcesSubscribe; |
84 | | - } |
85 | | - if ($resourcesListChanged) { |
86 | | - $resources['listChanged'] = $resourcesListChanged; |
87 | | - } |
88 | | - $this->resources = $resources; |
89 | | - } else { |
90 | | - $this->resources = null; |
91 | | - } |
92 | | - |
93 | | - if ($toolsEnabled) { |
94 | | - $this->tools = ['listChanged' => $toolsListChanged]; |
95 | | - } else { |
96 | | - $this->tools = null; |
97 | | - } |
98 | | - |
99 | | - $this->experimental = $experimental; |
100 | | - } |
| 27 | + public readonly ?bool $tools = true, |
| 28 | + public readonly ?bool $toolsListChanged = false, |
| 29 | + public readonly ?bool $resources = true, |
| 30 | + public readonly ?bool $resourcesSubscribe = false, |
| 31 | + public readonly ?bool $resourcesListChanged = false, |
| 32 | + public readonly ?bool $prompts = true, |
| 33 | + public readonly ?bool $promptsListChanged = false, |
| 34 | + public readonly ?bool $logging = false, |
| 35 | + public readonly ?bool $completions = false, |
| 36 | + public readonly ?array $experimental = null |
| 37 | + ) {} |
101 | 38 |
|
102 | 39 | /** |
103 | 40 | * Create a new ServerCapabilities object. |
104 | 41 | * |
105 | | - * @param bool $toolsEnabled Server supports tools. |
106 | | - * @param bool $toolsListChanged Server supports sending a notification when the list of tools changes. |
107 | | - * @param bool $resourcesEnabled Server supports resources. |
108 | | - * @param bool $resourcesSubscribe Server supports subscribing to changes in the list of resources. |
109 | | - * @param bool $resourcesListChanged Server supports sending a notification when the list of resources changes. |
110 | | - * @param bool $promptsEnabled Server supports prompts. |
111 | | - * @param bool $promptsListChanged Server supports sending a notification when the list of prompts changes. |
112 | | - * @param bool $loggingEnabled Server supports sending log messages to the client. |
113 | | - * @param bool $completionsEnabled Server supports argument autocompletion suggestions. |
| 42 | + * @param bool|null $tools Server offers tools. |
| 43 | + * @param bool|null $toolsListChanged Server supports sending a notification when the list of tools changes. |
| 44 | + * @param bool|null $resources Server offers resources. |
| 45 | + * @param bool|null $resourcesSubscribe Server supports subscribing to changes in the list of resources. |
| 46 | + * @param bool|null $resourcesListChanged Server supports sending a notification when the list of resources changes. |
| 47 | + * @param bool|null $prompts Server offers prompts. |
| 48 | + * @param bool|null $promptsListChanged Server supports sending a notification when the list of prompts changes. |
| 49 | + * @param bool|null $logging Server supports sending log messages to the client. |
| 50 | + * @param bool|null $completions Server supports argument autocompletion suggestions. |
114 | 51 | * @param array|null $experimental Experimental, non-standard capabilities that the server supports. |
115 | 52 | */ |
116 | 53 | public static function make( |
117 | | - bool $toolsEnabled = true, |
118 | | - bool $toolsListChanged = false, |
119 | | - bool $resourcesEnabled = true, |
120 | | - bool $resourcesSubscribe = false, |
121 | | - bool $resourcesListChanged = false, |
122 | | - bool $promptsEnabled = true, |
123 | | - bool $promptsListChanged = false, |
124 | | - bool $loggingEnabled = false, |
125 | | - bool $completionsEnabled = false, |
| 54 | + ?bool $tools = true, |
| 55 | + ?bool $toolsListChanged = false, |
| 56 | + ?bool $resources = true, |
| 57 | + ?bool $resourcesSubscribe = false, |
| 58 | + ?bool $resourcesListChanged = false, |
| 59 | + ?bool $prompts = true, |
| 60 | + ?bool $promptsListChanged = false, |
| 61 | + ?bool $logging = false, |
| 62 | + ?bool $completions = false, |
126 | 63 | ?array $experimental = null |
127 | 64 | ) { |
128 | 65 | return new static( |
129 | | - $toolsEnabled, |
| 66 | + $tools, |
130 | 67 | $toolsListChanged, |
131 | | - $resourcesEnabled, |
| 68 | + $resources, |
132 | 69 | $resourcesSubscribe, |
133 | 70 | $resourcesListChanged, |
134 | | - $promptsEnabled, |
| 71 | + $prompts, |
135 | 72 | $promptsListChanged, |
136 | | - $loggingEnabled, |
137 | | - $completionsEnabled, |
| 73 | + $logging, |
| 74 | + $completions, |
138 | 75 | $experimental |
139 | 76 | ); |
140 | 77 | } |
141 | 78 |
|
142 | 79 | public function toArray(): array |
143 | 80 | { |
144 | 81 | $data = []; |
145 | | - if ($this->logging !== null) { |
146 | | - $data['logging'] = (object) $this->logging; |
| 82 | + |
| 83 | + if ($this->logging) { |
| 84 | + $data['logging'] = new \stdClass(); |
147 | 85 | } |
148 | | - if ($this->completions !== null) { |
149 | | - $data['completions'] = (object) $this->completions; |
| 86 | + if ($this->completions) { |
| 87 | + $data['completions'] = new \stdClass(); |
150 | 88 | } |
151 | | - if ($this->prompts !== null) { |
152 | | - $data['prompts'] = (object) $this->prompts; |
| 89 | + |
| 90 | + if ($this->prompts || $this->promptsListChanged) { |
| 91 | + $data['prompts'] = new \stdClass(); |
| 92 | + if ($this->promptsListChanged) { |
| 93 | + $data['prompts']->listChanged = $this->promptsListChanged; |
| 94 | + } |
153 | 95 | } |
154 | | - if ($this->resources !== null) { |
155 | | - $data['resources'] = (object) $this->resources; |
| 96 | + |
| 97 | + if ($this->resources || $this->resourcesSubscribe || $this->resourcesListChanged) { |
| 98 | + $data['resources'] = new \stdClass(); |
| 99 | + if ($this->resourcesSubscribe) { |
| 100 | + $data['resources']->subscribe = $this->resourcesSubscribe; |
| 101 | + } |
| 102 | + if ($this->resourcesListChanged) { |
| 103 | + $data['resources']->listChanged = $this->resourcesListChanged; |
| 104 | + } |
156 | 105 | } |
157 | | - if ($this->tools !== null) { |
158 | | - $data['tools'] = (object) $this->tools; |
| 106 | + |
| 107 | + if ($this->tools || $this->toolsListChanged) { |
| 108 | + $data['tools'] = new \stdClass(); |
| 109 | + if ($this->toolsListChanged) { |
| 110 | + $data['tools']->listChanged = $this->toolsListChanged; |
| 111 | + } |
159 | 112 | } |
160 | | - if ($this->experimental !== null) { |
| 113 | + |
| 114 | + if ($this->experimental) { |
161 | 115 | $data['experimental'] = (object) $this->experimental; |
162 | 116 | } |
| 117 | + |
163 | 118 | return $data; |
164 | 119 | } |
165 | 120 |
|
166 | 121 | public static function fromArray(array $data): static |
167 | 122 | { |
168 | 123 | $loggingEnabled = isset($data['logging']); |
169 | 124 | $completionsEnabled = isset($data['completions']); |
| 125 | + $toolsEnabled = isset($data['tools']); |
| 126 | + $promptsEnabled = isset($data['prompts']); |
| 127 | + $resourcesEnabled = isset($data['resources']); |
170 | 128 |
|
171 | 129 | $promptsListChanged = null; |
172 | 130 | if (isset($data['prompts'])) { |
@@ -203,13 +161,16 @@ public static function fromArray(array $data): static |
203 | 161 | } |
204 | 162 |
|
205 | 163 | return new static( |
206 | | - $loggingEnabled, |
207 | | - $completionsEnabled, |
208 | | - $promptsListChanged, |
209 | | - $resourcesSubscribe, |
210 | | - $resourcesListChanged, |
211 | | - $toolsListChanged, |
212 | | - $data['experimental'] ?? null |
| 164 | + tools: $toolsEnabled, |
| 165 | + toolsListChanged: $toolsListChanged, |
| 166 | + resources: $resourcesEnabled, |
| 167 | + resourcesSubscribe: $resourcesSubscribe, |
| 168 | + resourcesListChanged: $resourcesListChanged, |
| 169 | + prompts: $promptsEnabled, |
| 170 | + promptsListChanged: $promptsListChanged, |
| 171 | + logging: $loggingEnabled, |
| 172 | + completions: $completionsEnabled, |
| 173 | + experimental: $data['experimental'] ?? null |
213 | 174 | ); |
214 | 175 | } |
215 | 176 |
|
|
0 commit comments