Skip to content

Commit d3bd97e

Browse files
committed
chore(http): create constant for session id header
1 parent 02e8637 commit d3bd97e

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

src/Server/Transport/StreamableHttpTransport.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
*/
3131
class StreamableHttpTransport extends BaseTransport
3232
{
33+
public const MCP_SESSION_ID_HEADER = 'Mcp-Session-Id';
34+
3335
private ResponseFactoryInterface $responseFactory;
3436
private StreamFactoryInterface $streamFactory;
3537

@@ -62,8 +64,8 @@ public function __construct(
6264
$this->corsHeaders = array_merge([
6365
'Access-Control-Allow-Origin' => '*',
6466
'Access-Control-Allow-Methods' => 'GET, POST, DELETE, OPTIONS',
65-
'Access-Control-Allow-Headers' => 'Content-Type, Mcp-Session-Id, Mcp-Protocol-Version, Last-Event-ID, Authorization, Accept',
66-
'Access-Control-Expose-Headers' => 'Mcp-Session-Id',
67+
'Access-Control-Allow-Headers' => 'Content-Type, '.self::MCP_SESSION_ID_HEADER.', Mcp-Protocol-Version, Last-Event-ID, Authorization, Accept',
68+
'Access-Control-Expose-Headers' => self::MCP_SESSION_ID_HEADER,
6769
], $corsHeaders);
6870

6971
foreach ($middleware as $m) {
@@ -120,7 +122,7 @@ protected function handlePostRequest(): ResponseInterface
120122
protected function handleDeleteRequest(): ResponseInterface
121123
{
122124
if (!$this->sessionId) {
123-
return $this->createErrorResponse(Error::forInvalidRequest('Mcp-Session-Id header is required.'), 400);
125+
return $this->createErrorResponse(Error::forInvalidRequest(self::MCP_SESSION_ID_HEADER.' header is required.'), 400);
124126
}
125127

126128
$this->handleSessionEnd($this->sessionId);
@@ -144,7 +146,7 @@ protected function createJsonResponse(): ResponseInterface
144146
->withBody($this->streamFactory->createStream($responseBody));
145147

146148
if ($this->sessionId) {
147-
$response = $response->withHeader('Mcp-Session-Id', $this->sessionId->toRfc4122());
149+
$response = $response->withHeader(self::MCP_SESSION_ID_HEADER, $this->sessionId->toRfc4122());
148150
}
149151

150152
return $response;
@@ -211,7 +213,7 @@ protected function createStreamedResponse(): ResponseInterface
211213
->withBody($stream);
212214

213215
if ($this->sessionId) {
214-
$response = $response->withHeader('Mcp-Session-Id', $this->sessionId->toRfc4122());
216+
$response = $response->withHeader(self::MCP_SESSION_ID_HEADER, $this->sessionId->toRfc4122());
215217
}
216218

217219
return $response;
@@ -276,7 +278,7 @@ protected function withCorsHeaders(ResponseInterface $response): ResponseInterfa
276278
private function handleRequest(ServerRequestInterface $request): ResponseInterface
277279
{
278280
$this->request = $request;
279-
$sessionIdString = $request->getHeaderLine('Mcp-Session-Id');
281+
$sessionIdString = $request->getHeaderLine(self::MCP_SESSION_ID_HEADER);
280282
$this->sessionId = $sessionIdString ? Uuid::fromString($sessionIdString) : null;
281283

282284
return match ($request->getMethod()) {

tests/Unit/Server/Transport/StreamableHttpTransportTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,10 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
8080
$this->assertSame('*', $response->getHeaderLine('Access-Control-Allow-Origin'));
8181
$this->assertSame('GET, POST, DELETE, OPTIONS', $response->getHeaderLine('Access-Control-Allow-Methods'));
8282
$this->assertSame(
83-
'Content-Type, Mcp-Session-Id, Mcp-Protocol-Version, Last-Event-ID, Authorization, Accept',
83+
'Content-Type, '.StreamableHttpTransport::MCP_SESSION_ID_HEADER.', Mcp-Protocol-Version, Last-Event-ID, Authorization, Accept',
8484
$response->getHeaderLine('Access-Control-Allow-Headers')
8585
);
86-
$this->assertSame('Mcp-Session-Id', $response->getHeaderLine('Access-Control-Expose-Headers'));
86+
$this->assertSame(StreamableHttpTransport::MCP_SESSION_ID_HEADER, $response->getHeaderLine('Access-Control-Expose-Headers'));
8787
}
8888

8989
#[TestDox('transport replaces existing CORS headers on the response')]
@@ -120,10 +120,10 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
120120
$this->assertSame('https://another.com', $response->getHeaderLine('Access-Control-Allow-Origin'));
121121
$this->assertSame('GET, POST, DELETE, OPTIONS', $response->getHeaderLine('Access-Control-Allow-Methods'));
122122
$this->assertSame(
123-
'Content-Type, Mcp-Session-Id, Mcp-Protocol-Version, Last-Event-ID, Authorization, Accept',
123+
'Content-Type, '.StreamableHttpTransport::MCP_SESSION_ID_HEADER.', Mcp-Protocol-Version, Last-Event-ID, Authorization, Accept',
124124
$response->getHeaderLine('Access-Control-Allow-Headers')
125125
);
126-
$this->assertSame('Mcp-Session-Id', $response->getHeaderLine('Access-Control-Expose-Headers'));
126+
$this->assertSame(StreamableHttpTransport::MCP_SESSION_ID_HEADER, $response->getHeaderLine('Access-Control-Expose-Headers'));
127127
}
128128

129129
#[TestDox('middleware runs before transport handles the request')]

0 commit comments

Comments
 (0)