[Server] Make CORS headers configurable in StreamableHttpTransport (#118)

* feat: make CORS headers configurable in StreamableHttpTransport

* docs: add CORS configuration documentation for HTTP transport

* refactor: streamline request creation and transport initialization in HTTP client example
This commit is contained in:
Kyrian Obikwelu 2025-10-26 23:58:26 +01:00 • committed by GitHub
commit 48a19b93a7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -35,16 +35,16 @@ class StreamableHttpTransport extends BaseTransport implements TransportInterfac
private ?int $immediateStatusCode = null;
/** @var array<string, string> */
private array $corsHeaders = [
'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',
];
private array $corsHeaders;
/**
* @param array<string, string> $corsHeaders
*/
public function __construct(
private readonly ServerRequestInterface $request,
?ResponseFactoryInterface $responseFactory = null,
?StreamFactoryInterface $streamFactory = null,
array $corsHeaders = [],
LoggerInterface $logger = new NullLogger(),
) {
parent::__construct($logger);
@ -53,6 +53,12 @@ class StreamableHttpTransport extends BaseTransport implements TransportInterfac
$this->responseFactory = $responseFactory ?? Psr17FactoryDiscovery::findResponseFactory();
$this->streamFactory = $streamFactory ?? Psr17FactoryDiscovery::findStreamFactory();
$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',
], $corsHeaders);
}
public function initialize(): void