Skip to content

Commit 9571b2d

Browse files
feat: make CORS headers configurable in StreamableHttpTransport
1 parent e4a82f7 commit 9571b2d

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

src/Server/Transport/StreamableHttpTransport.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,26 @@ class StreamableHttpTransport implements TransportInterface
4141
private ?int $outgoingStatusCode = null;
4242

4343
/** @var array<string, string> */
44-
private array $corsHeaders = [
45-
'Access-Control-Allow-Origin' => '*',
46-
'Access-Control-Allow-Methods' => 'GET, POST, DELETE, OPTIONS',
47-
'Access-Control-Allow-Headers' => 'Content-Type, Mcp-Session-Id, Mcp-Protocol-Version, Last-Event-ID, Authorization, Accept',
48-
];
44+
private array $corsHeaders;
4945

46+
/**
47+
* @param array<string, string> $corsHeaders
48+
*/
5049
public function __construct(
5150
private readonly ServerRequestInterface $request,
5251
private readonly ResponseFactoryInterface $responseFactory,
5352
private readonly StreamFactoryInterface $streamFactory,
53+
array $corsHeaders = [],
5454
private readonly LoggerInterface $logger = new NullLogger(),
5555
) {
5656
$sessionIdString = $this->request->getHeaderLine('Mcp-Session-Id');
5757
$this->sessionId = $sessionIdString ? Uuid::fromString($sessionIdString) : null;
58+
59+
$this->corsHeaders = array_merge([
60+
'Access-Control-Allow-Origin' => '*',
61+
'Access-Control-Allow-Methods' => 'GET, POST, DELETE, OPTIONS',
62+
'Access-Control-Allow-Headers' => 'Content-Type, Mcp-Session-Id, Mcp-Protocol-Version, Last-Event-ID, Authorization, Accept',
63+
], $corsHeaders);
5864
}
5965

6066
public function initialize(): void

0 commit comments

Comments
 (0)