-
Notifications
You must be signed in to change notification settings - Fork 129
Expand file tree
/
Copy pathHttpTransport.php
More file actions
298 lines (243 loc) · 9.41 KB
/
HttpTransport.php
File metadata and controls
298 lines (243 loc) · 9.41 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
<?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\Transport;
use Http\Discovery\Psr17FactoryDiscovery;
use Http\Discovery\Psr18ClientDiscovery;
use Mcp\Exception\ConnectionException;
use Mcp\Schema\JsonRpc\Error;
use Mcp\Schema\JsonRpc\Response;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\StreamFactoryInterface;
use Psr\Http\Message\StreamInterface;
use Psr\Log\LoggerInterface;
/**
* HTTP-based client transport using PSR-18 HTTP client.
*
* PSR-18 HTTP clients are auto-discovered if not provided.
*
* @phpstan-import-type McpFiber from TransportInterface
*
* @author Kyrian Obikwelu <koshnawaza@gmail.com>
*/
class HttpTransport extends BaseTransport
{
private ClientInterface $httpClient;
private RequestFactoryInterface $requestFactory;
private StreamFactoryInterface $streamFactory;
private ?string $sessionId = null;
/** @var McpFiber|null */
private ?\Fiber $activeFiber = null;
/** @var (callable(float, ?float, ?string): void)|null */
private $activeProgressCallback;
/** @var StreamInterface|null Active SSE stream being read */
private ?StreamInterface $activeStream = null;
/** @var string Buffer for incomplete SSE data */
private string $sseBuffer = '';
/**
* @param string $endpoint The MCP server endpoint URL
* @param array<string, string> $headers Additional headers to send
* @param ClientInterface|null $httpClient PSR-18 HTTP client (auto-discovered if null)
* @param RequestFactoryInterface|null $requestFactory PSR-17 request factory (auto-discovered if null)
* @param StreamFactoryInterface|null $streamFactory PSR-17 stream factory (auto-discovered if null)
*/
public function __construct(
private readonly string $endpoint,
private readonly array $headers = [],
?ClientInterface $httpClient = null,
?RequestFactoryInterface $requestFactory = null,
?StreamFactoryInterface $streamFactory = null,
?LoggerInterface $logger = null,
) {
parent::__construct($logger);
$this->httpClient = $httpClient ?? Psr18ClientDiscovery::find();
$this->requestFactory = $requestFactory ?? Psr17FactoryDiscovery::findRequestFactory();
$this->streamFactory = $streamFactory ?? Psr17FactoryDiscovery::findStreamFactory();
}
public function connect(): void
{
$this->activeFiber = new \Fiber(fn () => $this->handleInitialize());
$this->activeFiber->start();
while (!$this->activeFiber->isTerminated()) {
$this->tick();
}
$result = $this->activeFiber->getReturn();
$this->activeFiber = null;
if ($result instanceof Error) {
throw new ConnectionException('Initialization failed: '.$result->message);
}
$this->logger->info('HTTP client connected and initialized', ['endpoint' => $this->endpoint]);
}
public function send(string $data): void
{
$request = $this->requestFactory->createRequest('POST', $this->endpoint)
->withHeader('Content-Type', 'application/json')
->withHeader('Accept', 'application/json, text/event-stream')
->withBody($this->streamFactory->createStream($data));
if (null !== $this->sessionId) {
$request = $request->withHeader('Mcp-Session-Id', $this->sessionId);
}
foreach ($this->headers as $name => $value) {
$request = $request->withHeader($name, $value);
}
$this->logger->debug('Sending HTTP request', ['data' => $data]);
try {
$response = $this->httpClient->sendRequest($request);
} catch (\Throwable $e) {
$this->handleError($e);
throw new ConnectionException('HTTP request failed: '.$e->getMessage(), 0, $e);
}
if ($response->hasHeader('Mcp-Session-Id')) {
$this->sessionId = $response->getHeaderLine('Mcp-Session-Id');
$this->logger->debug('Received session ID', ['session_id' => $this->sessionId]);
}
$contentType = strtolower($response->getHeaderLine('Content-Type'));
if (str_contains($contentType, 'text/event-stream')) {
$this->activeStream = $response->getBody();
$this->sseBuffer = '';
} elseif (str_contains($contentType, 'application/json')) {
$body = $response->getBody()->getContents();
if (!empty($body)) {
$this->handleMessage($body);
}
}
}
/**
* @param McpFiber $fiber
* @param (callable(float $progress, ?float $total, ?string $message): void)|null $onProgress
*/
public function runRequest(\Fiber $fiber, ?callable $onProgress = null): Response|Error
{
$this->activeFiber = $fiber;
$this->activeProgressCallback = $onProgress;
$fiber->start();
while (!$fiber->isTerminated()) {
$this->tick();
}
$this->activeFiber = null;
$this->activeProgressCallback = null;
$this->activeStream = null;
return $fiber->getReturn();
}
public function close(): void
{
if (null !== $this->sessionId) {
try {
$request = $this->requestFactory->createRequest('DELETE', $this->endpoint)
->withHeader('Mcp-Session-Id', $this->sessionId);
foreach ($this->headers as $name => $value) {
$request = $request->withHeader($name, $value);
}
$this->httpClient->sendRequest($request);
$this->logger->info('Session closed', ['session_id' => $this->sessionId]);
} catch (\Throwable $e) {
$this->logger->warning('Failed to close session', ['error' => $e->getMessage()]);
}
}
$this->sessionId = null;
$this->activeStream = null;
$this->handleClose('Transport closed');
}
private function tick(): void
{
$this->processSSEStream();
$this->processProgress();
$this->processFiber();
usleep(1000); // 1ms
}
/**
* Read SSE data incrementally from active stream.
*/
private function processSSEStream(): void
{
if (null === $this->activeStream) {
return;
}
if (!$this->activeStream->eof()) {
$chunk = $this->activeStream->read(4096);
if ('' !== $chunk) {
$this->sseBuffer .= $chunk;
}
}
while (false !== ($pos = strpos($this->sseBuffer, "\n\n"))) {
$event = substr($this->sseBuffer, 0, $pos);
$this->sseBuffer = substr($this->sseBuffer, $pos + 2);
if (!empty(trim($event))) {
$this->processSSEEvent($event);
}
}
if ($this->activeStream->eof() && empty($this->sseBuffer)) {
$this->activeStream = null;
}
}
/**
* Parse a single SSE event and handle the message.
*/
private function processSSEEvent(string $event): void
{
$data = '';
foreach (explode("\n", $event) as $line) {
if (str_starts_with($line, 'data:')) {
$data .= trim(substr($line, 5));
}
}
if (!empty($data)) {
$this->handleMessage($data);
}
}
/**
* Process pending progress updates from session and execute callback.
*/
private function processProgress(): void
{
if (null === $this->activeProgressCallback || null === $this->state) {
return;
}
$updates = $this->state->consumeProgressUpdates();
foreach ($updates as $update) {
try {
($this->activeProgressCallback)(
$update['progress'],
$update['total'],
$update['message'],
);
} catch (\Throwable $e) {
$this->logger->warning('Progress callback failed', ['exception' => $e]);
}
}
}
private function processFiber(): void
{
if (null === $this->activeFiber || !$this->activeFiber->isSuspended()) {
return;
}
if (null === $this->state) {
return;
}
$pendingRequests = $this->state->getPendingRequests();
foreach ($pendingRequests as $pending) {
$requestId = $pending['request_id'];
$timestamp = $pending['timestamp'];
$timeout = $pending['timeout'];
$response = $this->state->consumeResponse($requestId);
if (null !== $response) {
$this->logger->debug('Resuming fiber with response', ['request_id' => $requestId]);
$this->activeFiber->resume($response);
return;
}
if (time() - $timestamp >= $timeout) {
$this->logger->warning('Request timed out', ['request_id' => $requestId]);
$error = Error::forInternalError('Request timed out', $requestId);
$this->activeFiber->resume($error);
return;
}
}
}
}