Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions src/Server/Transport/StreamableHttpTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@
*/
class StreamableHttpTransport extends BaseTransport
{
private const SESSION_HEADER = 'Mcp-Session-Id';

private const ALLOWED_HEADER = [
'Accept',
'Authorization',
'Content-Type',
'Last-Event-ID',
'Mcp-Protocol-Version',
self::SESSION_HEADER,
];

private ResponseFactoryInterface $responseFactory;
private StreamFactoryInterface $streamFactory;

Expand Down Expand Up @@ -62,8 +73,8 @@ public function __construct(
$this->corsHeaders = array_merge([
'Access-Control-Allow-Origin' => '*',
'Access-Control-Allow-Methods' => 'GET, POST, DELETE, OPTIONS',
'Access-Control-Allow-Headers' => 'Content-Type, Mcp-Session-Id, Mcp-Protocol-Version, Last-Event-ID, Authorization, Accept',
'Access-Control-Expose-Headers' => 'Mcp-Session-Id',
'Access-Control-Allow-Headers' => implode(',', self::ALLOWED_HEADER),
'Access-Control-Expose-Headers' => self::SESSION_HEADER,
], $corsHeaders);

foreach ($middleware as $m) {
Expand Down Expand Up @@ -120,7 +131,7 @@ protected function handlePostRequest(): ResponseInterface
protected function handleDeleteRequest(): ResponseInterface
{
if (!$this->sessionId) {
return $this->createErrorResponse(Error::forInvalidRequest('Mcp-Session-Id header is required.'), 400);
return $this->createErrorResponse(Error::forInvalidRequest(self::SESSION_HEADER.' header is required.'), 400);
}

$this->handleSessionEnd($this->sessionId);
Expand All @@ -144,7 +155,7 @@ protected function createJsonResponse(): ResponseInterface
->withBody($this->streamFactory->createStream($responseBody));

if ($this->sessionId) {
$response = $response->withHeader('Mcp-Session-Id', $this->sessionId->toRfc4122());
$response = $response->withHeader(self::SESSION_HEADER, $this->sessionId->toRfc4122());
}

return $response;
Expand Down Expand Up @@ -211,7 +222,7 @@ protected function createStreamedResponse(): ResponseInterface
->withBody($stream);

if ($this->sessionId) {
$response = $response->withHeader('Mcp-Session-Id', $this->sessionId->toRfc4122());
$response = $response->withHeader(self::SESSION_HEADER, $this->sessionId->toRfc4122());
}

return $response;
Expand Down Expand Up @@ -276,7 +287,7 @@ protected function withCorsHeaders(ResponseInterface $response): ResponseInterfa
private function handleRequest(ServerRequestInterface $request): ResponseInterface
{
$this->request = $request;
$sessionIdString = $request->getHeaderLine('Mcp-Session-Id');
$sessionIdString = $request->getHeaderLine(self::SESSION_HEADER);
$this->sessionId = $sessionIdString ? Uuid::fromString($sessionIdString) : null;

return match ($request->getMethod()) {
Expand Down
4 changes: 2 additions & 2 deletions tests/Unit/Server/Transport/StreamableHttpTransportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
$this->assertSame('*', $response->getHeaderLine('Access-Control-Allow-Origin'));
$this->assertSame('GET, POST, DELETE, OPTIONS', $response->getHeaderLine('Access-Control-Allow-Methods'));
$this->assertSame(
'Content-Type, Mcp-Session-Id, Mcp-Protocol-Version, Last-Event-ID, Authorization, Accept',
'Accept,Authorization,Content-Type,Last-Event-ID,Mcp-Protocol-Version,Mcp-Session-Id',
$response->getHeaderLine('Access-Control-Allow-Headers')
);
$this->assertSame('Mcp-Session-Id', $response->getHeaderLine('Access-Control-Expose-Headers'));
Expand Down Expand Up @@ -120,7 +120,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
$this->assertSame('https://another.com', $response->getHeaderLine('Access-Control-Allow-Origin'));
$this->assertSame('GET, POST, DELETE, OPTIONS', $response->getHeaderLine('Access-Control-Allow-Methods'));
$this->assertSame(
'Content-Type, Mcp-Session-Id, Mcp-Protocol-Version, Last-Event-ID, Authorization, Accept',
'Accept,Authorization,Content-Type,Last-Event-ID,Mcp-Protocol-Version,Mcp-Session-Id',
$response->getHeaderLine('Access-Control-Allow-Headers')
);
$this->assertSame('Mcp-Session-Id', $response->getHeaderLine('Access-Control-Expose-Headers'));
Expand Down