Repository navigation
Expand file tree
/
Copy pathBuilder.php
More file actions
226 lines (188 loc) · 6.43 KB
/
Copy pathBuilder.php
File metadata and controls
226 lines (188 loc) · 6.43 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
<?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\Client;
use Mcp\Client\Exception\InvalidArgumentException;
use Mcp\Client\Exception\LogicException;
use Mcp\Client\Handler\Notification\NotificationHandlerInterface;
use Mcp\Client\Handler\Request\RequestHandlerInterface;
use Mcp\Schema\ClientCapabilities;
use Mcp\Schema\Enum\ProtocolVersion;
use Mcp\Schema\Extension\ExtensionInterface;
use Mcp\Schema\Implementation;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
/**
* Fluent builder for creating Client instances.
*
* @author Kyrian Obikwelu <koshnawaza@gmail.com>
*/
final class Builder
{
private string $name = 'mcp-php-client';
private string $version = '1.0.0';
private ?string $description = null;
private ?string $title = null;
private ?ProtocolVersion $protocolVersion = null;
private ?ProtocolVersion $fallbackProtocolVersion = ProtocolVersion::V2025_11_25;
private ?ClientCapabilities $capabilities = null;
/** @var array<string, array<string, mixed>> */
private array $extensions = [];
private int $initTimeout = 30;
private int $requestTimeout = 120;
private int $maxRetries = 3;
private ?LoggerInterface $logger = null;
/** @var NotificationHandlerInterface[] */
private array $notificationHandlers = [];
/** @var RequestHandlerInterface<mixed>[] */
private array $requestHandlers = [];
/**
* Set the client name and version.
*
* @param ?string $title Display name for UI and end-user contexts. Falls back to $name when absent.
*/
public function setClientInfo(string $name, string $version, ?string $description = null, ?string $title = null): self
{
$this->name = $name;
$this->version = $version;
$this->description = $description;
$this->title = $title;
return $this;
}
/**
* Set the protocol version the client prefers, defaults to 2026-07-28; a modern one is probed for first.
*/
public function setProtocolVersion(ProtocolVersion $protocolVersion): self
{
$this->protocolVersion = $protocolVersion;
return $this;
}
/**
* Set the handshake revision a modern client falls back to, defaults to 2025-11-25; null makes it modern-only.
*/
public function setFallbackProtocolVersion(?ProtocolVersion $protocolVersion): self
{
$this->fallbackProtocolVersion = $protocolVersion;
return $this;
}
/**
* Set client capabilities.
*/
public function setCapabilities(ClientCapabilities $capabilities): self
{
$this->capabilities = $capabilities;
return $this;
}
/**
* Enable one or more MCP protocol extensions, announced to the server under
* `capabilities.extensions` in the initialize request.
*
* @throws InvalidArgumentException if the identifier is not a valid `_meta` prefix
* @throws LogicException if the same extension is enabled more than once
*/
public function enableExtension(ExtensionInterface ...$extensions): self
{
foreach ($extensions as $extension) {
$id = (string) $extension->getId();
if (isset($this->extensions[$id])) {
throw new LogicException(\sprintf('Extension "%s" is already enabled.', $id));
}
$this->extensions[$id] = $extension->getCapabilities();
}
return $this;
}
/**
* Set initialization timeout in seconds.
*/
public function setInitTimeout(int $seconds): self
{
$this->initTimeout = $seconds;
return $this;
}
/**
* Set request timeout in seconds.
*/
public function setRequestTimeout(int $seconds): self
{
$this->requestTimeout = $seconds;
return $this;
}
/**
* Set the number of times a failed connection attempt is retried.
*
* Counts retries, not attempts: 3 means one initial attempt plus up to three
* retries. Pass 0 to fail on the first failure. Only applies to
* {@see Client::connect()}; individual requests are never retried.
*/
public function setMaxRetries(int $retries): self
{
$this->maxRetries = $retries;
return $this;
}
/**
* Set the logger.
*/
public function setLogger(LoggerInterface $logger): self
{
$this->logger = $logger;
return $this;
}
/**
* Add a notification handler for server notifications.
*/
public function addNotificationHandler(NotificationHandlerInterface $handler): self
{
$this->notificationHandlers[] = $handler;
return $this;
}
/**
* Add a request handler for server requests (e.g., sampling).
*
* @param RequestHandlerInterface<mixed> $handler
*/
public function addRequestHandler(RequestHandlerInterface $handler): self
{
$this->requestHandlers[] = $handler;
return $this;
}
/**
* Build the client instance.
*/
public function build(): Client
{
$logger = $this->logger ?? new NullLogger();
$clientInfo = new Implementation(
$this->name,
$this->version,
$this->description,
title: $this->title,
);
$capabilities = $this->capabilities ?? new ClientCapabilities();
// Extensions enabled via enableExtension() are folded into caller-supplied
// capabilities too, so setCapabilities() does not silently drop them.
if ([] !== $this->extensions) {
$capabilities = $capabilities->withExtensions($this->extensions);
}
$config = new Configuration(
clientInfo: $clientInfo,
capabilities: $capabilities,
protocolVersion: $this->protocolVersion ?? ProtocolVersion::V2026_07_28,
initTimeout: $this->initTimeout,
requestTimeout: $this->requestTimeout,
maxRetries: $this->maxRetries,
fallbackProtocolVersion: $this->fallbackProtocolVersion,
);
$protocol = new Protocol(
requestHandlers: $this->requestHandlers,
notificationHandlers: $this->notificationHandlers,
logger: $logger,
);
return new Client($protocol, $config, $logger);
}
}