Skip to content

Commit 501fa28

Browse files
feat: make CORS headers configurable in StreamableHttpTransport
1 parent 42b5261 commit 501fa28

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
@@ -35,16 +35,16 @@ class StreamableHttpTransport extends BaseTransport implements TransportInterfac
3535
private ?int $immediateStatusCode = null;
3636

3737
/** @var array<string, string> */
38-
private array $corsHeaders = [
39-
'Access-Control-Allow-Origin' => '*',
40-
'Access-Control-Allow-Methods' => 'GET, POST, DELETE, OPTIONS',
41-
'Access-Control-Allow-Headers' => 'Content-Type, Mcp-Session-Id, Mcp-Protocol-Version, Last-Event-ID, Authorization, Accept',
42-
];
38+
private array $corsHeaders;
4339

40+
/**
41+
* @param array<string, string> $corsHeaders
42+
*/
4443
public function __construct(
4544
private readonly ServerRequestInterface $request,
4645
?ResponseFactoryInterface $responseFactory = null,
4746
?StreamFactoryInterface $streamFactory = null,
47+
array $corsHeaders = [],
4848
LoggerInterface $logger = new NullLogger(),
4949
) {
5050
parent::__construct($logger);
@@ -53,6 +53,12 @@ public function __construct(
5353

5454
$this->responseFactory = $responseFactory ?? Psr17FactoryDiscovery::findResponseFactory();
5555
$this->streamFactory = $streamFactory ?? Psr17FactoryDiscovery::findStreamFactory();
56+
57+
$this->corsHeaders = array_merge([
58+
'Access-Control-Allow-Origin' => '*',
59+
'Access-Control-Allow-Methods' => 'GET, POST, DELETE, OPTIONS',
60+
'Access-Control-Allow-Headers' => 'Content-Type, Mcp-Session-Id, Mcp-Protocol-Version, Last-Event-ID, Authorization, Accept',
61+
], $corsHeaders);
5662
}
5763

5864
public function initialize(): void

0 commit comments

Comments
 (0)