Repository navigation
Expand file tree
/
Copy pathStreamableHttpTransport.php
More file actions
636 lines (522 loc) · 24.5 KB
/
Copy pathStreamableHttpTransport.php
File metadata and controls
636 lines (522 loc) · 24.5 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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
<?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\Server\Transport;
use Http\Discovery\Psr17FactoryDiscovery;
use Mcp\Schema\Enum\ProtocolVersion;
use Mcp\Schema\JsonRpc\Error;
use Mcp\Server\Authorization\AccessToken;
use Mcp\Server\Exception\InvalidArgumentException;
use Mcp\Server\Exception\SessionStoreException;
use Mcp\Server\Stateless\StatelessProtocol;
use Mcp\Server\Transport\Http\Middleware\CorsMiddleware;
use Mcp\Server\Transport\Http\Middleware\DnsRebindingProtectionMiddleware;
use Mcp\Server\Transport\Http\Middleware\ProtocolVersionMiddleware;
use Mcp\Server\Transport\Http\MiddlewareRequestHandler;
use Mcp\Server\Transport\Http\StatelessResponder;
use Mcp\Server\Wire\InboundClassifier;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\StreamFactoryInterface;
use Psr\Http\Message\StreamInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\Uid\Uuid;
/**
* Carries MCP over HTTP, in either protocol era.
*
* Every request is classified once, before anything else looks at it, and
* routed to the lifecycle it belongs to: a per-request envelope claiming a
* modern revision goes to {@see StatelessProtocol}, everything else — the
* `initialize` handshake, its session's later requests, its `DELETE` teardown —
* goes to the session machinery below. One endpoint, both eras, nothing for the
* client to pick.
*
* A server run without a modern-era dispatcher (see
* {@see \Mcp\Server\Builder::withoutModernEra()}) serves the handshake era
* alone and refuses modern claims, naming the revisions it does serve.
*
* @extends BaseTransport<ResponseInterface>
*
* @phpstan-import-type McpFiber from TransportInterface
*
* @author Kyrian Obikwelu <koshnawaza@gmail.com>
*/
class StreamableHttpTransport extends BaseTransport implements StatelessAwareTransportInterface
{
use ReadsBoundedBody;
public const SESSION_HEADER = 'Mcp-Session-Id';
public const PROTOCOL_VERSION_HEADER = 'Mcp-Protocol-Version';
/**
* PSR-7 request attribute holding a non-empty string that identifies the authenticated principal.
*
* A session only serves the principal that opened it. Without this attribute, the principal is
* the one of the request's {@see AccessToken}; set it to identify users another way.
*/
public const PRINCIPAL_ATTRIBUTE = 'mcp.principal';
/**
* Upper bound on the request body read for a POST, guarding against memory
* exhaustion from an oversized (or unbounded chunked) payload.
*/
public const DEFAULT_MAX_BODY_BYTES = 4 * 1024 * 1024;
private ResponseFactoryInterface $responseFactory;
private StreamFactoryInterface $streamFactory;
private StatelessResponder $responder;
private InboundClassifier $classifier;
private ?StatelessProtocol $stateless = null;
/** @var non-empty-list<ProtocolVersion>|null null names every handshake revision */
private ?array $handshakeVersions = null;
private bool $sessionless = false;
private ProtocolVersion $headerProtocolVersion = ProtocolVersion::DEFAULT_HEADER_VERSION;
private ?string $immediateResponse = null;
private ?int $immediateStatusCode = null;
private ?string $jsonRpcResponse = null;
/** @var list<MiddlewareInterface>|null null until {@see self::listen()} resolves the defaults */
private ?array $middleware;
/**
* A non-null `$middleware` list replaces {@see self::defaultMiddleware()}.
* An empty list is treated as a likely mistake and logs a warning on every
* request. When the host application already handles CORS and host
* validation, opt out explicitly with `[new PassthroughMiddleware()]`.
*
* @param iterable<MiddlewareInterface>|null $middleware `null` installs {@see self::defaultMiddleware()}; `[]` disables them and logs a warning
*/
public function __construct(
private readonly ServerRequestInterface $request,
?ResponseFactoryInterface $responseFactory = null,
?StreamFactoryInterface $streamFactory = null,
?LoggerInterface $logger = null,
?iterable $middleware = null,
private readonly int $maxBodyBytes = self::DEFAULT_MAX_BODY_BYTES,
) {
parent::__construct($logger);
if ($this->maxBodyBytes < 1) {
throw new InvalidArgumentException('maxBodyBytes must be at least 1.');
}
$this->responseFactory = $responseFactory ?? Psr17FactoryDiscovery::findResponseFactory();
$this->streamFactory = $streamFactory ?? Psr17FactoryDiscovery::findStreamFactory();
$this->responder = new StatelessResponder($this->responseFactory, $this->streamFactory, $this->logger);
$this->classifier = new InboundClassifier();
if (null === $middleware) {
// Left unresolved: the default stack's version middleware has to
// know which revisions this endpoint serves, and the modern
// dispatcher arrives after the constructor.
$this->middleware = null;
} else {
$this->middleware = self::normalizeMiddleware($middleware);
if ([] === $this->middleware) {
$this->logger->warning('Streamable HTTP transport started with an empty middleware list, so the default CORS and DNS rebinding protections are disabled. Pass null (or omit the argument) to use them, or include them via [...StreamableHttpTransport::defaultMiddleware(), $yourMiddleware]. If the host application already provides them, pass [new PassthroughMiddleware()] to opt out without this warning.');
}
// Custom middleware runs before the request's era is classified, so a
// ProtocolVersionMiddleware here rejects every modern-era request by
// default — it only accepts the handshake versions it was built for.
foreach ($this->middleware as $entry) {
if ($entry instanceof ProtocolVersionMiddleware) {
$this->logger->warning('A custom middleware list includes ProtocolVersionMiddleware. It runs before the modern (2026-07-28) era is classified and rejects that era\'s requests by default, since it only recognises handshake revisions. Remove it from the custom list — the transport already applies it to handshake-era traffic on its own via self::handshakeMiddleware().');
break;
}
}
}
}
/**
* Secure default middleware stack applied when no `$middleware` is provided to the constructor.
*
* These run at the edge, before the request's era is known, because what
* they enforce — origin policy, DNS rebinding — is true of both eras. The
* `MCP-Protocol-Version` header rule is not: it belongs to the handshake
* era, so {@see self::handshakeMiddleware()} carries it instead and the
* modern leg answers for its own revisions.
*
* @return list<MiddlewareInterface>
*/
public static function defaultMiddleware(): array
{
return [
new CorsMiddleware(),
new DnsRebindingProtectionMiddleware(),
];
}
/**
* Middleware applied only to requests classified as handshake-era traffic.
*
* @return list<MiddlewareInterface>
*/
public static function handshakeMiddleware(): array
{
return [
new ProtocolVersionMiddleware(),
];
}
public function connectStateless(StatelessProtocol $protocol): void
{
$this->stateless = $protocol;
}
public function setHandshakeVersions(array $versions): void
{
$this->handshakeVersions = $versions;
}
/**
* Serves the handshake era without sessions: no `Mcp-Session-Id` is read or
* sent, and `DELETE`, like `GET`, is answered with `405`.
*
* @internal called by {@see \Mcp\Server\Protocol::connect()} for a server built without {@see \Mcp\Server\Builder::setSession()}
*/
public function setSessionless(bool $sessionless): void
{
$this->sessionless = $sessionless;
}
/**
* The revision the `Mcp-Protocol-Version` header of the handshake-era request names, or the one the spec assumes without it.
*
* @internal read by {@see \Mcp\Server\Protocol} for a server built without {@see \Mcp\Server\Builder::setSession()}
*/
public function getHeaderProtocolVersion(): ProtocolVersion
{
return $this->headerProtocolVersion;
}
public function send(string $data, array $context): void
{
// An error with its own HTTP status rejects the whole POST and is its only answer.
if (isset($context['status_code'])) {
$this->immediateResponse = $data;
$this->immediateStatusCode = $context['status_code'];
return;
}
$this->jsonRpcResponse = $data;
}
public function listen(): ResponseInterface
{
$handler = new MiddlewareRequestHandler(
$this->middleware ??= self::defaultMiddleware(),
\Closure::fromCallable([$this, 'handleRequest']),
);
return $handler->handle($this->request);
}
protected function handleOptionsRequest(): ResponseInterface
{
return $this->responseFactory->createResponse(204);
}
/**
* @param string $body the request body, already read and bounded by {@see self::handleRequest()}
* @param AccessToken|null $accessToken the token the request was authorized with, see {@see self::accessToken()}
* @param string|null $principal the principal the request was authenticated as, see {@see self::PRINCIPAL_ATTRIBUTE}
*/
protected function handlePostRequest(string $body, ?AccessToken $accessToken = null, ?string $principal = null): ResponseInterface
{
$this->handleMessage($body, $this->sessionId, $accessToken, $principal);
// Consume the immediate response exactly once, so a transport instance
// reused for a later POST does not replay it.
$immediateResponse = $this->immediateResponse;
$immediateStatusCode = $this->immediateStatusCode;
$this->immediateResponse = null;
$this->immediateStatusCode = null;
if (null !== $immediateResponse) {
$this->jsonRpcResponse = null;
return $this->responseFactory->createResponse($immediateStatusCode ?? 200)
->withHeader('Content-Type', 'application/json')
->withBody($this->streamFactory->createStream($immediateResponse));
}
if (null !== $this->sessionFiber) {
$this->logger->info('Fiber suspended, handling via SSE.');
return $this->createStreamedResponse();
}
return $this->createJsonResponse();
}
/**
* @param string|null $principal the principal the request was authenticated as, see {@see self::PRINCIPAL_ATTRIBUTE}
*/
protected function handleDeleteRequest(?string $principal = null): ResponseInterface
{
if (!$this->sessionId) {
return $this->createErrorResponse(Error::forInvalidRequest(self::SESSION_HEADER.' header is required.'), 400);
}
try {
$ended = $this->handleSessionEnd($this->sessionId, $principal);
} catch (SessionStoreException $e) {
$this->logger->error('Session store is unavailable.', ['session_id' => $this->sessionId->toRfc4122(), 'exception' => $e]);
return $this->createErrorResponse(Error::forInternalError('Session store is unavailable.'), 503);
}
if (!$ended) {
return $this->createErrorResponse(Error::forInvalidRequest('Session not found or has expired.'), 404);
}
return $this->responseFactory->createResponse(200);
}
protected function createJsonResponse(): ResponseInterface
{
// A POST without a request (a response or a notification) is accepted
// without a body, so the session's queued messages stay for a later answer.
if (null === $this->jsonRpcResponse) {
return $this->responseFactory->createResponse(202);
}
$jsonRpcResponse = $this->jsonRpcResponse;
$this->jsonRpcResponse = null;
// A JSON answer holds the response alone, so messages queued for the
// session make it a stream that carries them ahead of the response.
$queued = array_column($this->getOutgoingMessages($this->sessionId), 'message');
if ([] === $queued) {
$response = $this->responseFactory->createResponse(200)
->withHeader('Content-Type', 'application/json')
->withBody($this->streamFactory->createStream($jsonRpcResponse));
} else {
$events = array_map(static fn (string $message): string => "event: message\ndata: {$message}\n\n", [...$queued, $jsonRpcResponse]);
$response = $this->responseFactory->createResponse(200)
->withHeader('Content-Type', 'text/event-stream')
->withHeader('Cache-Control', 'no-cache')
->withBody($this->streamFactory->createStream(implode('', $events)));
}
if ($this->sessionId && !$this->sessionless) {
$response = $response->withHeader(self::SESSION_HEADER, $this->sessionId->toRfc4122());
}
return $response;
}
protected function createStreamedResponse(): ResponseInterface
{
$fiber = $this->sessionFiber;
$chunks = function () use ($fiber): \Generator {
if (null === $fiber) {
return;
}
try {
$this->logger->info('SSE: Starting request processing loop');
while ($fiber->isSuspended()) {
if ($this->isRequestCancelled($fiber)) {
return;
}
yield from $this->outgoingFrames();
$pendingRequests = $this->getPendingRequests($this->sessionId, $fiber);
if (empty($pendingRequests)) {
if (null !== $error = $this->resumeHandlerFiber($fiber, $this->sessionId)) {
yield from $this->outgoingFrames();
yield from $this->finalFrame($error);
return;
}
continue;
}
$resumed = false;
foreach ($pendingRequests as $pending) {
$requestId = Uuid::fromString($pending['request_id']);
$response = $this->checkForResponse($requestId, $this->sessionId);
if (null !== $response) {
if (null !== $error = $this->resumeHandlerFiber($fiber, $this->sessionId, $response)) {
yield from $this->outgoingFrames();
yield from $this->finalFrame($error);
return;
}
$resumed = true;
break;
}
}
if (!$resumed) {
// An idle turn: the consumer paces it and keeps it alive.
yield '';
}
}
if ($this->isRequestCancelled($fiber)) {
return;
}
yield from $this->finalFrame($fiber->getReturn());
} finally {
$this->sessionFiber = null;
}
};
$callback = function () use ($chunks): void {
$lastKeepAlive = null;
foreach ($chunks() as $chunk) {
if ('' !== $chunk) {
echo $chunk;
@ob_flush();
flush();
continue;
}
// PHP spots a dropped peer only by writing, so an idle turn writes a keep-alive, at most every 0.25s.
$now = microtime(true);
if (null === $lastKeepAlive || $now - $lastKeepAlive >= 0.25) {
$lastKeepAlive = $now;
echo ": keep-alive\n\n";
@ob_flush();
flush();
}
if ($this->isConnectionAborted()) {
$this->logger->info('SSE: Client disconnected, dropping the suspended request.');
return;
}
usleep(100000);
}
};
$stream = new CallbackStream($callback, $this->logger, $chunks);
$response = $this->responseFactory->createResponse(200)
->withHeader('Content-Type', 'text/event-stream')
->withHeader('Cache-Control', 'no-cache')
->withHeader('Connection', 'keep-alive')
->withHeader('X-Accel-Buffering', 'no')
->withBody($stream);
if ($this->sessionId && !$this->sessionless) {
$response = $response->withHeader(self::SESSION_HEADER, $this->sessionId->toRfc4122());
}
return $response;
}
protected function isConnectionAborted(): bool
{
return 1 === connection_aborted();
}
/**
* @return \Generator<int, string>
*/
private function finalFrame(mixed $finalResult): \Generator
{
if (null === $finalResult) {
return;
}
try {
yield "event: message\ndata: ".json_encode($finalResult, \JSON_THROW_ON_ERROR)."\n\n";
} catch (\JsonException $e) {
$this->logger->error('SSE: Failed to encode final Fiber result.', ['exception' => $e]);
}
}
/**
* @return \Generator<int, string>
*/
private function outgoingFrames(): \Generator
{
foreach ($this->getOutgoingMessages($this->sessionId) as $message) {
yield "event: message\ndata: {$message['message']}\n\n";
}
}
protected function createErrorResponse(Error $jsonRpcError, int $statusCode): ResponseInterface
{
$payload = json_encode($jsonRpcError, \JSON_THROW_ON_ERROR);
$response = $this->responseFactory->createResponse($statusCode)
->withHeader('Content-Type', 'application/json')
->withBody($this->streamFactory->createStream($payload));
if (405 === $statusCode) {
$response = $response->withHeader('Allow', $this->sessionless ? 'POST, OPTIONS' : 'POST, DELETE, OPTIONS');
}
return $response;
}
/**
* Reads the request body, bounded by {@see self::$maxBodyBytes}.
*/
private function readBody(StreamInterface $body): ?string
{
return $this->readBoundedBody($body, $this->maxBodyBytes);
}
/**
* @param iterable<MiddlewareInterface> $middleware
*
* @return list<MiddlewareInterface>
*/
private static function normalizeMiddleware(iterable $middleware): array
{
$normalized = [];
foreach ($middleware as $m) {
if (!$m instanceof MiddlewareInterface) {
throw new InvalidArgumentException('Streamable HTTP middleware must implement Psr\\Http\\Server\\MiddlewareInterface.');
}
$normalized[] = $m;
}
return $normalized;
}
private function handleRequest(ServerRequestInterface $request): ResponseInterface
{
if ('OPTIONS' === $request->getMethod()) {
return $this->handleOptionsRequest();
}
// Read once, here: the era decision needs the body, and so does
// whichever leg it routes to. A PSR-7 stream over `php://input` cannot
// be read twice.
$body = null;
if ('POST' === $request->getMethod()) {
$body = $this->readBody($request->getBody());
if (null === $body) {
$this->logger->warning('Rejected POST body exceeding the maximum allowed size.', ['limit' => $this->maxBodyBytes]);
return $this->createErrorResponse(Error::forBodyTooLarge($this->maxBodyBytes), 413);
}
}
$classification = $this->classifier->classify($request->getMethod(), $body, self::headers($request));
if ($classification->isRejected()) {
\assert(null !== $classification->error);
return $this->responder->error($classification->error, $classification->httpStatus);
}
if ($classification->modern) {
return $this->handleModernRequest($request, $body ?? '', $classification->claimedVersion ?? '');
}
// The version-header rule only reaches the traffic it is about. Running
// it at the edge would let it answer a modern claim with the handshake
// era's revision list, ahead of the leg that knows better.
return (new MiddlewareRequestHandler(
self::handshakeMiddleware(),
fn (ServerRequestInterface $handshake): ResponseInterface => $this->handleHandshakeRequest($handshake, $body),
))->handle($request);
}
private function handleHandshakeRequest(ServerRequestInterface $request, ?string $body): ResponseInterface
{
// Already validated by ProtocolVersionMiddleware.
$this->headerProtocolVersion = ProtocolVersion::tryFrom($request->getHeaderLine(self::PROTOCOL_VERSION_HEADER)) ?? ProtocolVersion::DEFAULT_HEADER_VERSION;
if ($this->sessionless) {
return match ($request->getMethod()) {
'POST' => $this->handlePostRequest($body ?? '', self::accessToken($request)),
default => $this->createErrorResponse(Error::forInvalidRequest('Method Not Allowed'), 405),
};
}
$sessionIdHeaders = $request->getHeader(self::SESSION_HEADER);
if (\count($sessionIdHeaders) > 1) {
return $this->createErrorResponse(Error::forInvalidRequest(self::SESSION_HEADER.' header must not be repeated.'), 400);
}
$sessionIdString = $sessionIdHeaders[0] ?? '';
try {
$this->sessionId = $sessionIdString ? Uuid::fromString($sessionIdString) : null;
// Symfony UID 5.4/6.4 throw the global parent; newer versions throw a namespaced subclass.
} catch (\InvalidArgumentException) {
return $this->createErrorResponse(Error::forInvalidRequest(self::SESSION_HEADER.' header must be a valid UUID.'), 400);
}
$principal = $request->getAttribute(self::PRINCIPAL_ATTRIBUTE) ?? self::accessToken($request)?->getPrincipal();
if (null !== $principal && (!\is_string($principal) || '' === $principal)) {
$this->logger->error(\sprintf('The "%s" request attribute must be a non-empty string, got "%s".', self::PRINCIPAL_ATTRIBUTE, get_debug_type($principal)));
return $this->createErrorResponse(Error::forInternalError('Internal error'), 500);
}
return match ($request->getMethod()) {
'POST' => $this->handlePostRequest($body ?? '', self::accessToken($request), $principal),
'DELETE' => $this->handleDeleteRequest($principal),
default => $this->createErrorResponse(Error::forInvalidRequest('Method Not Allowed'), 405),
};
}
/**
* Answers a request that claimed the modern era's per-request envelope.
*/
private function handleModernRequest(ServerRequestInterface $request, string $body, string $claimedVersion): ResponseInterface
{
if (null === $this->stateless) {
return $this->responder->error(
Error::forUnsupportedProtocolVersion($claimedVersion, $this->handshakeVersions ?? ProtocolVersion::handshakeVersions()),
400,
);
}
return $this->responder->respond($this->stateless->handle($body, self::headers($request), self::accessToken($request)));
}
/**
* The token an authorization middleware validated, see {@see Http\Middleware\AuthorizationMiddleware}.
*/
private static function accessToken(ServerRequestInterface $request): ?AccessToken
{
$accessToken = $request->getAttribute(AccessToken::class);
return $accessToken instanceof AccessToken ? $accessToken : null;
}
/**
* @return array<string, string>
*/
private static function headers(ServerRequestInterface $request): array
{
$headers = [];
foreach ($request->getHeaders() as $name => $values) {
$headers[$name] = implode(', ', $values);
}
return $headers;
}
}