-
Notifications
You must be signed in to change notification settings - Fork 127
Expand file tree
/
Copy pathProtocol.php
More file actions
318 lines (270 loc) · 10.1 KB
/
Protocol.php
File metadata and controls
318 lines (270 loc) · 10.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
<?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\Handler\Notification\NotificationHandlerInterface;
use Mcp\Client\Handler\Notification\ProgressNotificationHandler;
use Mcp\Client\Handler\Request\RequestHandlerInterface;
use Mcp\Client\State\ClientState;
use Mcp\Client\State\ClientStateInterface;
use Mcp\Client\Transport\TransportInterface;
use Mcp\JsonRpc\MessageFactory;
use Mcp\Schema\JsonRpc\Error;
use Mcp\Schema\JsonRpc\Notification;
use Mcp\Schema\JsonRpc\Request;
use Mcp\Schema\JsonRpc\Response;
use Mcp\Schema\Notification\InitializedNotification;
use Mcp\Schema\Request\InitializeRequest;
use Mcp\Schema\Result\InitializeResult;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
/**
* Client protocol handler for MCP communication.
*
* Handles message routing, request/response correlation, and the initialization handshake.
* All blocking operations are delegated to the transport.
*
* @phpstan-import-type FiberSuspend from TransportInterface
*
* @author Kyrian Obikwelu <koshnawaza@gmail.com>
*/
class Protocol
{
private ?TransportInterface $transport = null;
private ClientStateInterface $state;
private MessageFactory $messageFactory;
private LoggerInterface $logger;
/** @var NotificationHandlerInterface[] */
private array $notificationHandlers;
/**
* @param RequestHandlerInterface<mixed>[] $requestHandlers
* @param NotificationHandlerInterface[] $notificationHandlers
*/
public function __construct(
private readonly array $requestHandlers = [],
array $notificationHandlers = [],
?MessageFactory $messageFactory = null,
?LoggerInterface $logger = null,
) {
$this->state = new ClientState();
$this->messageFactory = $messageFactory ?? MessageFactory::make();
$this->logger = $logger ?? new NullLogger();
$this->notificationHandlers = [
new ProgressNotificationHandler($this->state),
...$notificationHandlers,
];
}
/**
* Connect this protocol to a transport.
*
* Sets up message handling callbacks.
*
* @param TransportInterface $transport The transport to connect
* @param Configuration $config The client configuration for initialization
*/
public function connect(TransportInterface $transport, Configuration $config): void
{
$this->transport = $transport;
$transport->setState($this->state);
$transport->onInitialize(fn () => $this->initialize($config));
$transport->onMessage($this->processMessage(...));
$transport->onError(fn (\Throwable $e) => $this->logger->error('Transport error', ['exception' => $e]));
$this->logger->info('Protocol connected to transport', ['transport' => $transport::class]);
}
/**
* Perform the MCP initialization handshake.
*
* Sends InitializeRequest and waits for response, then sends InitializedNotification.
*
* @param Configuration $config The client configuration
*
* @return Response<array<string, mixed>>|Error
*/
public function initialize(Configuration $config): Response|Error
{
$request = new InitializeRequest(
$config->protocolVersion->value,
$config->capabilities,
$config->clientInfo,
);
$response = $this->request($request, $config->initTimeout);
if ($response instanceof Response) {
$initResult = InitializeResult::fromArray($response->result);
$this->state->setServerInfo($initResult->serverInfo);
$this->state->setInstructions($initResult->instructions);
$this->state->setInitialized(true);
$this->sendNotification(new InitializedNotification());
$this->logger->info('Initialization complete', [
'server' => $initResult->serverInfo->name,
]);
}
return $response;
}
/**
* Send a request to the server and wait for response.
*
* If a response is immediately available (sync HTTP), returns it.
* Otherwise, suspends the Fiber and waits for the transport to resume it.
*
* @param Request $request The request to send
* @param int $timeout The timeout in seconds
* @param bool $withProgress Whether to attach a progress token to the request
*
* @return Response<array<string, mixed>>|Error
*/
public function request(Request $request, int $timeout, bool $withProgress = false): Response|Error
{
$requestId = $this->state->nextRequestId();
$request = $request->withId($requestId);
if ($withProgress) {
$progressToken = "prog-{$requestId}";
$request = $request->withMeta(['progressToken' => $progressToken]);
}
$this->state->addPendingRequest($requestId, $timeout);
$this->sendRequest($request);
$immediate = $this->state->consumeResponse($requestId);
if (null !== $immediate) {
$this->logger->debug('Received immediate response', ['id' => $requestId]);
return $immediate;
}
$this->logger->debug('Suspending fiber for response', ['id' => $requestId]);
return \Fiber::suspend([
'type' => 'await_response',
'request_id' => $requestId,
'timeout' => $timeout,
]);
}
/**
* Send a request to the server.
*/
private function sendRequest(Request $request): void
{
$this->logger->debug('Sending request', [
'id' => $request->getId(),
'method' => $request::getMethod(),
]);
$encoded = json_encode($request, \JSON_THROW_ON_ERROR);
$this->transport?->send($encoded);
}
/**
* Send a notification to the server (fire and forget).
*/
public function sendNotification(Notification $notification): void
{
$this->logger->debug('Sending notification', ['method' => $notification::getMethod()]);
$encoded = json_encode($notification, \JSON_THROW_ON_ERROR);
$this->transport?->send($encoded);
}
/**
* Send a response back to the server (for server-initiated requests).
*
* @param Response<mixed>|Error $response
*/
private function sendResponse(Response|Error $response): void
{
$this->logger->debug('Sending response', ['id' => $response->getId()]);
$encoded = json_encode($response, \JSON_THROW_ON_ERROR);
$this->transport?->send($encoded);
}
/**
* Process an incoming message from the server.
*
* Routes to appropriate handler based on message type.
*/
public function processMessage(string $input): void
{
$this->logger->debug('Received message', ['input' => $input]);
try {
$messages = $this->messageFactory->create($input);
} catch (\JsonException $e) {
$this->logger->warning('Failed to parse message', ['exception' => $e]);
return;
}
foreach ($messages as $message) {
if ($message instanceof Response || $message instanceof Error) {
$this->handleResponse($message);
} elseif ($message instanceof Request) {
$this->handleRequest($message);
} elseif ($message instanceof Notification) {
$this->handleNotification($message);
}
}
}
/**
* Handle a response from the server.
*
* This stores it in session. The transport will pick it up and resume the Fiber.
*
* @param Response<mixed>|Error $response
*/
private function handleResponse(Response|Error $response): void
{
$requestId = $response->getId();
$this->logger->debug('Handling response', ['id' => $requestId]);
$this->state->storeResponse($requestId, $response->jsonSerialize());
}
/**
* Handle a request from the server (e.g., sampling request).
*/
private function handleRequest(Request $request): void
{
$method = $request::getMethod();
$this->logger->debug('Received server request', [
'method' => $method,
'id' => $request->getId(),
]);
foreach ($this->requestHandlers as $handler) {
if ($handler->supports($request)) {
try {
$response = $handler->handle($request);
} catch (\Throwable $e) {
$this->logger->error('Unexpected error while handling request', [
'method' => $method,
'exception' => $e,
]);
$response = Error::forInternalError(
\sprintf('Unexpected error while handling "%s" request', $method),
$request->getId()
);
}
$this->sendResponse($response);
return;
}
}
$error = Error::forMethodNotFound(
\sprintf('Client does not handle "%s" requests.', $method),
$request->getId()
);
$this->sendResponse($error);
}
/**
* Handle a notification from the server.
*/
private function handleNotification(Notification $notification): void
{
$method = $notification::getMethod();
$this->logger->debug('Received server notification', [
'method' => $method,
]);
foreach ($this->notificationHandlers as $handler) {
if ($handler->supports($notification)) {
try {
$handler->handle($notification);
} catch (\Throwable $e) {
$this->logger->warning('Notification handler failed', ['exception' => $e]);
}
return;
}
}
}
public function getState(): ClientStateInterface
{
return $this->state;
}
}