-
Notifications
You must be signed in to change notification settings - Fork 125
feat: relax StrictOidcDiscoveryMetadataPolicy and add Dynamic Client Registration middleware (RFC 7591) #269
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
simonchrz
wants to merge
4
commits into
modelcontextprotocol:main
Choose a base branch
from
simonchrz:main
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.
+486
−20
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
443af5c
feat: add LenientOidcDiscoveryMetadataPolicy for IdPs without code_ch…
simonchrz 7974a2f
feat: add OAuth 2.0 Dynamic Client Registration middleware (RFC 7591)
simonchrz 5652305
fix: address MR review comments for ClientRegistration
simonchrz bc3908b
refactor: remove LenientOidcDiscoveryMetadataPolicy, relax Strict ins…
simonchrz 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| <?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\Exception; | ||
|
|
||
| final class ClientRegistrationException extends \RuntimeException implements ExceptionInterface | ||
| { | ||
| } |
140 changes: 140 additions & 0 deletions
140
src/Server/Transport/Http/Middleware/ClientRegistrationMiddleware.php
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,140 @@ | ||
| <?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 Http\Discovery\Psr17FactoryDiscovery; | ||
| use Mcp\Exception\ClientRegistrationException; | ||
| use Mcp\Exception\InvalidArgumentException; | ||
| use Mcp\Server\Transport\Http\OAuth\ClientRegistrarInterface; | ||
| use Psr\Http\Message\ResponseFactoryInterface; | ||
| use Psr\Http\Message\ResponseInterface; | ||
| use Psr\Http\Message\ServerRequestInterface; | ||
| use Psr\Http\Message\StreamFactoryInterface; | ||
| use Psr\Http\Server\MiddlewareInterface; | ||
| use Psr\Http\Server\RequestHandlerInterface; | ||
|
|
||
| /** | ||
| * OAuth 2.0 Dynamic Client Registration (RFC 7591) middleware. | ||
| * | ||
| * Handles POST /register requests by delegating to a ClientRegistrarInterface | ||
| * and enriches /.well-known/oauth-authorization-server responses with the | ||
| * registration_endpoint. | ||
| */ | ||
| final class ClientRegistrationMiddleware implements MiddlewareInterface | ||
| { | ||
| private const REGISTRATION_PATH = '/register'; | ||
|
|
||
| private ResponseFactoryInterface $responseFactory; | ||
| private StreamFactoryInterface $streamFactory; | ||
|
|
||
| public function __construct( | ||
| private readonly ClientRegistrarInterface $registrar, | ||
| private readonly string $localBaseUrl, | ||
| ?ResponseFactoryInterface $responseFactory = null, | ||
| ?StreamFactoryInterface $streamFactory = null, | ||
| ) { | ||
| if ('' === trim($localBaseUrl)) { | ||
| throw new InvalidArgumentException('The $localBaseUrl must not be empty.'); | ||
| } | ||
|
|
||
| $this->responseFactory = $responseFactory ?? Psr17FactoryDiscovery::findResponseFactory(); | ||
| $this->streamFactory = $streamFactory ?? Psr17FactoryDiscovery::findStreamFactory(); | ||
| } | ||
|
|
||
| public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface | ||
| { | ||
| $path = $request->getUri()->getPath(); | ||
|
|
||
| if ('POST' === $request->getMethod() && self::REGISTRATION_PATH === $path) { | ||
| return $this->handleRegistration($request); | ||
| } | ||
|
|
||
| $response = $handler->handle($request); | ||
|
|
||
| if ('GET' === $request->getMethod() && '/.well-known/oauth-authorization-server' === $path) { | ||
| return $this->enrichAuthServerMetadata($response); | ||
| } | ||
|
|
||
| return $response; | ||
| } | ||
|
|
||
| private function handleRegistration(ServerRequestInterface $request): ResponseInterface | ||
| { | ||
| $body = $request->getBody()->__toString(); | ||
| $data = json_decode($body, true); | ||
|
|
||
| if (!\is_array($data)) { | ||
| return $this->jsonResponse(400, [ | ||
| 'error' => 'invalid_client_metadata', | ||
| 'error_description' => 'Request body must be valid JSON.', | ||
| ]); | ||
| } | ||
|
|
||
| try { | ||
| $result = $this->registrar->register($data); | ||
| } catch (ClientRegistrationException $e) { | ||
| return $this->jsonResponse(400, [ | ||
| 'error' => 'invalid_client_metadata', | ||
| 'error_description' => $e->getMessage(), | ||
| ]); | ||
| } | ||
|
|
||
| return $this->jsonResponse(201, $result); | ||
| } | ||
|
|
||
| private function enrichAuthServerMetadata(ResponseInterface $response): ResponseInterface | ||
| { | ||
| if (200 !== $response->getStatusCode()) { | ||
| return $response; | ||
| } | ||
|
|
||
| $stream = $response->getBody(); | ||
|
|
||
| if ($stream->isSeekable()) { | ||
| $stream->rewind(); | ||
| } | ||
|
|
||
| $metadata = json_decode($stream->__toString(), true); | ||
|
|
||
| if (!\is_array($metadata)) { | ||
| return $response; | ||
| } | ||
|
|
||
| $metadata['registration_endpoint'] = rtrim($this->localBaseUrl, '/').self::REGISTRATION_PATH; | ||
|
|
||
| return $this->jsonResponse(200, $metadata, [ | ||
| 'Cache-Control' => $response->getHeaderLine('Cache-Control'), | ||
| ]); | ||
| } | ||
|
|
||
| /** | ||
| * @param array<string, mixed> $data | ||
| * @param array<string, string> $extraHeaders | ||
| */ | ||
| private function jsonResponse(int $status, array $data, array $extraHeaders = []): ResponseInterface | ||
| { | ||
| $response = $this->responseFactory | ||
| ->createResponse($status) | ||
| ->withHeader('Content-Type', 'application/json') | ||
| ->withBody($this->streamFactory->createStream( | ||
| json_encode($data, \JSON_THROW_ON_ERROR | \JSON_UNESCAPED_SLASHES), | ||
| )); | ||
|
|
||
| foreach ($extraHeaders as $name => $value) { | ||
| if ('' !== $value) { | ||
| $response = $response->withHeader($name, $value); | ||
| } | ||
| } | ||
|
|
||
| return $response; | ||
| } | ||
| } |
42 changes: 42 additions & 0 deletions
42
src/Server/Transport/Http/OAuth/ClientRegistrarInterface.php
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,42 @@ | ||
| <?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\OAuth; | ||
|
|
||
| use Mcp\Exception\ClientRegistrationException; | ||
|
|
||
| /** | ||
| * Interface for OAuth 2.0 Dynamic Client Registration (RFC 7591). | ||
| * | ||
| * Implementations are responsible for persisting client credentials and | ||
| * returning a registration response as defined in RFC 7591 Section 3.2. | ||
| * | ||
| * @see https://datatracker.ietf.org/doc/html/rfc7591 | ||
| */ | ||
| interface ClientRegistrarInterface | ||
| { | ||
| /** | ||
| * Registers a new OAuth 2.0 client. | ||
| * | ||
| * The registration request contains metadata fields as defined in RFC 7591 | ||
| * Section 2 (e.g. redirect_uris, client_name, token_endpoint_auth_method). | ||
| * | ||
| * The returned array MUST include at least "client_id" and should include | ||
| * "client_secret" when the token endpoint auth method requires one. | ||
| * | ||
| * @param array<string, mixed> $registrationRequest Client metadata from the registration request body | ||
| * | ||
| * @return array<string, mixed> Registration response including client_id and optional client_secret | ||
| * | ||
| * @throws ClientRegistrationException If registration fails (e.g. invalid metadata, storage error) | ||
| */ | ||
| public function register(array $registrationRequest): array; | ||
| } | ||
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.
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.
Uh oh!
There was an error while loading. Please reload this page.