|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the official PHP MCP SDK. |
| 5 | + * |
| 6 | + * A collaboration between Symfony and the PHP Foundation. |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Mcp\Server\Transport\Http\Middleware; |
| 13 | + |
| 14 | +use Http\Discovery\Psr17FactoryDiscovery; |
| 15 | +use Psr\Http\Message\ResponseFactoryInterface; |
| 16 | +use Psr\Http\Message\ResponseInterface; |
| 17 | +use Psr\Http\Message\ServerRequestInterface; |
| 18 | +use Psr\Http\Server\MiddlewareInterface; |
| 19 | +use Psr\Http\Server\RequestHandlerInterface; |
| 20 | + |
| 21 | +/** |
| 22 | + * Handles CORS preflight requests and applies CORS headers to all responses. |
| 23 | + * |
| 24 | + * By default, no Access-Control-Allow-Origin header is set, which effectively |
| 25 | + * blocks cross-origin browser requests. Configure $allowedOrigins to allow |
| 26 | + * specific origins or use ['*'] to allow all. |
| 27 | + * |
| 28 | + * @author Christopher Hertel <mail@christopher-hertel.de> |
| 29 | + */ |
| 30 | +final class CorsMiddleware implements MiddlewareInterface |
| 31 | +{ |
| 32 | + private readonly ResponseFactoryInterface $responseFactory; |
| 33 | + |
| 34 | + /** |
| 35 | + * @param list<string> $allowedOrigins Origins to allow (empty = no Access-Control-Allow-Origin header). Use ['*'] to allow all origins. |
| 36 | + * @param list<string> $allowedMethods HTTP methods for Access-Control-Allow-Methods |
| 37 | + * @param list<string> $allowedHeaders Request headers for Access-Control-Allow-Headers |
| 38 | + * @param list<string> $exposedHeaders Response headers for Access-Control-Expose-Headers |
| 39 | + * @param int|null $maxAge Preflight cache duration in seconds (Access-Control-Max-Age) |
| 40 | + * @param bool $allowCredentials Whether to send Access-Control-Allow-Credentials: true |
| 41 | + */ |
| 42 | + public function __construct( |
| 43 | + private readonly array $allowedOrigins = [], |
| 44 | + private readonly array $allowedMethods = ['GET', 'POST', 'DELETE', 'OPTIONS'], |
| 45 | + private readonly array $allowedHeaders = ['Accept', 'Authorization', 'Content-Type', 'Last-Event-ID', 'Mcp-Protocol-Version', 'Mcp-Session-Id'], |
| 46 | + private readonly array $exposedHeaders = ['Mcp-Session-Id'], |
| 47 | + private readonly ?int $maxAge = null, |
| 48 | + private readonly bool $allowCredentials = false, |
| 49 | + ?ResponseFactoryInterface $responseFactory = null, |
| 50 | + ) { |
| 51 | + $this->responseFactory = $responseFactory ?? Psr17FactoryDiscovery::findResponseFactory(); |
| 52 | + } |
| 53 | + |
| 54 | + public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
| 55 | + { |
| 56 | + if ('OPTIONS' === $request->getMethod()) { |
| 57 | + return $this->addCorsHeaders($request, $this->responseFactory->createResponse(204)); |
| 58 | + } |
| 59 | + |
| 60 | + return $this->addCorsHeaders($request, $handler->handle($request)); |
| 61 | + } |
| 62 | + |
| 63 | + private function addCorsHeaders(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface |
| 64 | + { |
| 65 | + $origin = $request->getHeaderLine('Origin'); |
| 66 | + $allowedOrigin = $this->resolveAllowedOrigin($origin); |
| 67 | + |
| 68 | + if (null !== $allowedOrigin && !$response->hasHeader('Access-Control-Allow-Origin')) { |
| 69 | + $response = $response->withHeader('Access-Control-Allow-Origin', $allowedOrigin); |
| 70 | + } |
| 71 | + |
| 72 | + if (!$response->hasHeader('Access-Control-Allow-Methods')) { |
| 73 | + $response = $response->withHeader('Access-Control-Allow-Methods', implode(', ', $this->allowedMethods)); |
| 74 | + } |
| 75 | + |
| 76 | + if (!$response->hasHeader('Access-Control-Allow-Headers')) { |
| 77 | + $response = $response->withHeader('Access-Control-Allow-Headers', implode(', ', $this->allowedHeaders)); |
| 78 | + } |
| 79 | + |
| 80 | + if ([] !== $this->exposedHeaders && !$response->hasHeader('Access-Control-Expose-Headers')) { |
| 81 | + $response = $response->withHeader('Access-Control-Expose-Headers', implode(', ', $this->exposedHeaders)); |
| 82 | + } |
| 83 | + |
| 84 | + if (null !== $this->maxAge && !$response->hasHeader('Access-Control-Max-Age')) { |
| 85 | + $response = $response->withHeader('Access-Control-Max-Age', (string) $this->maxAge); |
| 86 | + } |
| 87 | + |
| 88 | + if ($this->allowCredentials && !$response->hasHeader('Access-Control-Allow-Credentials')) { |
| 89 | + $response = $response->withHeader('Access-Control-Allow-Credentials', 'true'); |
| 90 | + } |
| 91 | + |
| 92 | + return $response; |
| 93 | + } |
| 94 | + |
| 95 | + private function resolveAllowedOrigin(string $origin): ?string |
| 96 | + { |
| 97 | + if ([] === $this->allowedOrigins) { |
| 98 | + return null; |
| 99 | + } |
| 100 | + |
| 101 | + if (\in_array('*', $this->allowedOrigins, true)) { |
| 102 | + return '*'; |
| 103 | + } |
| 104 | + |
| 105 | + if ('' !== $origin && \in_array($origin, $this->allowedOrigins, true)) { |
| 106 | + return $origin; |
| 107 | + } |
| 108 | + |
| 109 | + return null; |
| 110 | + } |
| 111 | +} |
0 commit comments