-
Notifications
You must be signed in to change notification settings - Fork 127
[Server] Extract CORS handling into CorsMiddleware #277
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
chr-hertel
wants to merge
2
commits into
main
Choose a base branch
from
refactor/cors-middleware
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the official PHP MCP SDK. | ||
| * | ||
| * A collaboration between Symfony and the PHP Foundation. | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace Mcp\Server\Transport\Http\Middleware; | ||
|
|
||
| use Psr\Http\Message\ResponseInterface; | ||
| use Psr\Http\Message\ServerRequestInterface; | ||
| use Psr\Http\Server\MiddlewareInterface; | ||
| use Psr\Http\Server\RequestHandlerInterface; | ||
|
|
||
| /** | ||
| * Applies CORS headers to all responses. | ||
| * | ||
| * By default, no Access-Control-Allow-Origin header is set, which effectively | ||
| * blocks cross-origin browser requests. Configure $allowedOrigins to allow | ||
| * specific origins or use ['*'] to allow all. | ||
| * | ||
| * @author Christopher Hertel <mail@christopher-hertel.de> | ||
| */ | ||
| final class CorsMiddleware implements MiddlewareInterface | ||
| { | ||
| /** | ||
| * @param list<string> $allowedOrigins Origins to allow (empty = no Access-Control-Allow-Origin header). Use ['*'] to allow all origins. | ||
| * @param list<string> $allowedMethods HTTP methods for Access-Control-Allow-Methods | ||
| * @param list<string> $allowedHeaders Request headers for Access-Control-Allow-Headers | ||
| * @param list<string> $exposedHeaders Response headers for Access-Control-Expose-Headers | ||
| */ | ||
| public function __construct( | ||
| private readonly array $allowedOrigins = [], | ||
| private readonly array $allowedMethods = ['GET', 'POST', 'DELETE', 'OPTIONS'], | ||
| private readonly array $allowedHeaders = ['Accept', 'Authorization', 'Content-Type', 'Last-Event-ID', 'Mcp-Protocol-Version', 'Mcp-Session-Id'], | ||
| private readonly array $exposedHeaders = ['Mcp-Session-Id'], | ||
| ) { | ||
| } | ||
|
|
||
| public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface | ||
| { | ||
| $response = $handler->handle($request); | ||
|
|
||
| $origin = $request->getHeaderLine('Origin'); | ||
| $allowedOrigin = $this->resolveAllowedOrigin($origin); | ||
|
|
||
| if (null !== $allowedOrigin && !$response->hasHeader('Access-Control-Allow-Origin')) { | ||
| $response = $response->withHeader('Access-Control-Allow-Origin', $allowedOrigin); | ||
| } | ||
|
|
||
| if (!$response->hasHeader('Access-Control-Allow-Methods')) { | ||
| $response = $response->withHeader('Access-Control-Allow-Methods', implode(', ', $this->allowedMethods)); | ||
| } | ||
|
|
||
| if (!$response->hasHeader('Access-Control-Allow-Headers')) { | ||
| $response = $response->withHeader('Access-Control-Allow-Headers', implode(', ', $this->allowedHeaders)); | ||
| } | ||
|
|
||
| if ([] !== $this->exposedHeaders && !$response->hasHeader('Access-Control-Expose-Headers')) { | ||
| $response = $response->withHeader('Access-Control-Expose-Headers', implode(', ', $this->exposedHeaders)); | ||
| } | ||
|
|
||
| return $response; | ||
| } | ||
|
|
||
| private function resolveAllowedOrigin(string $origin): ?string | ||
| { | ||
| if ([] === $this->allowedOrigins) { | ||
| return null; | ||
| } | ||
|
|
||
| if (\in_array('*', $this->allowedOrigins, true)) { | ||
| return '*'; | ||
| } | ||
|
|
||
| if ('' !== $origin && \in_array($origin, $this->allowedOrigins, true)) { | ||
| return $origin; | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's say I use the mcp bundle with the famous https://github.com/nelmio/NelmioCorsBundle how am I going to avoid conflicts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Haven't tested, but we'd need a way to opt-out completely you mean?