Skip to content

Commit c605dee

Browse files
refactor: update ClientCapabilities and ServerCapabilities to use boolean properties for better clarity and initialization
1 parent dbb7806 commit c605dee

File tree

2 files changed

+107
-158
lines changed

2 files changed

+107
-158
lines changed

src/ClientCapabilities.php

Lines changed: 21 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -11,56 +11,44 @@
1111
*/
1212
class ClientCapabilities implements JsonSerializable
1313
{
14-
/**
15-
* Present if the client supports listing roots.
16-
* @var array{ listChanged: bool }|null
17-
*/
18-
public readonly ?array $roots;
19-
20-
/**
21-
* Present if the client supports sampling from an LLM.
22-
*/
23-
public readonly ?array $sampling;
24-
25-
/**
26-
* Experimental, non-standard capabilities that the client supports.
27-
*/
28-
public readonly ?array $experimental;
29-
3014
public function __construct(
31-
?bool $rootsListChanged = null,
32-
?bool $sampling = null,
33-
?array $experimental = null
34-
) {
35-
$this->roots = ($rootsListChanged !== null) ? ['listChanged' => $rootsListChanged] : null;
36-
$this->sampling = ($sampling === true) ? [] : $sampling;
37-
$this->experimental = $experimental;
38-
}
15+
public readonly ?bool $roots = false,
16+
public readonly ?bool $rootsListChanged = null,
17+
public readonly ?bool $sampling = null,
18+
public readonly ?array $experimental = null
19+
) {}
3920

40-
public static function make(?bool $rootsListChanged = null, ?bool $sampling = null, ?array $experimental = null): static
21+
public static function make(?bool $roots = false, ?bool $rootsListChanged = null, ?bool $sampling = null, ?array $experimental = null): static
4122
{
42-
return new static($rootsListChanged, $sampling, $experimental);
23+
return new static($roots, $rootsListChanged, $sampling, $experimental);
4324
}
4425

4526
public function toArray(): array
4627
{
4728
$data = [];
48-
if ($this->roots !== null) {
49-
$data['roots'] = (object) $this->roots;
29+
if ($this->roots || $this->rootsListChanged) {
30+
$data['roots'] = new \stdClass();
31+
if ($this->rootsListChanged) {
32+
$data['roots']->listChanged = $this->rootsListChanged;
33+
}
5034
}
51-
if ($this->sampling !== null) {
52-
$data['sampling'] = (object) $this->sampling;
35+
36+
if ($this->sampling) {
37+
$data['sampling'] = new \stdClass();
5338
}
54-
if ($this->experimental !== null) {
55-
$data['experimental'] = $this->experimental;
39+
40+
if ($this->experimental) {
41+
$data['experimental'] = (object) $this->experimental;
5642
}
43+
5744
return $data;
5845
}
5946

6047
public static function fromArray(array $data): static
6148
{
49+
$rootsEnabled = isset($data['roots']);
6250
$rootsListChanged = null;
63-
if (isset($data['roots'])) {
51+
if ($rootsEnabled) {
6452
if (is_array($data['roots']) && array_key_exists('listChanged', $data['roots'])) {
6553
$rootsListChanged = (bool) $data['roots']['listChanged'];
6654
} elseif (is_object($data['roots']) && property_exists($data['roots'], 'listChanged')) {

src/ServerCapabilities.php

Lines changed: 86 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -12,161 +12,119 @@
1212
class ServerCapabilities implements JsonSerializable
1313
{
1414
/**
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.
5825
*/
5926
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+
) {}
10138

10239
/**
10340
* Create a new ServerCapabilities object.
10441
*
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.
11451
* @param array|null $experimental Experimental, non-standard capabilities that the server supports.
11552
*/
11653
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,
12663
?array $experimental = null
12764
) {
12865
return new static(
129-
$toolsEnabled,
66+
$tools,
13067
$toolsListChanged,
131-
$resourcesEnabled,
68+
$resources,
13269
$resourcesSubscribe,
13370
$resourcesListChanged,
134-
$promptsEnabled,
71+
$prompts,
13572
$promptsListChanged,
136-
$loggingEnabled,
137-
$completionsEnabled,
73+
$logging,
74+
$completions,
13875
$experimental
13976
);
14077
}
14178

14279
public function toArray(): array
14380
{
14481
$data = [];
145-
if ($this->logging !== null) {
146-
$data['logging'] = (object) $this->logging;
82+
83+
if ($this->logging) {
84+
$data['logging'] = new \stdClass();
14785
}
148-
if ($this->completions !== null) {
149-
$data['completions'] = (object) $this->completions;
86+
if ($this->completions) {
87+
$data['completions'] = new \stdClass();
15088
}
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+
}
15395
}
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+
}
156105
}
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+
}
159112
}
160-
if ($this->experimental !== null) {
113+
114+
if ($this->experimental) {
161115
$data['experimental'] = (object) $this->experimental;
162116
}
117+
163118
return $data;
164119
}
165120

166121
public static function fromArray(array $data): static
167122
{
168123
$loggingEnabled = isset($data['logging']);
169124
$completionsEnabled = isset($data['completions']);
125+
$toolsEnabled = isset($data['tools']);
126+
$promptsEnabled = isset($data['prompts']);
127+
$resourcesEnabled = isset($data['resources']);
170128

171129
$promptsListChanged = null;
172130
if (isset($data['prompts'])) {
@@ -203,13 +161,16 @@ public static function fromArray(array $data): static
203161
}
204162

205163
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
213174
);
214175
}
215176

0 commit comments

Comments
 (0)