Skip to content

[Server] Oauth2 based on middleware - #221

Merged
chr-hertel merged 22 commits into
modelcontextprotocol:mainfrom
sveneld:oauth_middleware
Mar 10, 2026
Merged

[Server] Oauth2 based on middleware#221
chr-hertel merged 22 commits into
modelcontextprotocol:mainfrom
sveneld:oauth_middleware

Conversation

@sveneld

@sveneld sveneld commented Jan 12, 2026

Copy link
Copy Markdown
Contributor

Motivation and Context

Implementation of OAuth based on middlewares from #218

How Has This Been Tested?

Breaking Changes

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Checklist

  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

Additional context

Fix #214

@chr-hertel

Copy link
Copy Markdown
Member

Thanks @sveneld - it will take some time for me to give some proper feedback here, just to let you know - needs quite some focus & attention - still really appreciated!

@chr-hertel chr-hertel changed the title Oauth2 based on middleware [Server] Oauth2 based on middleware Jan 23, 2026
@chr-hertel chr-hertel added Server Issues & PRs related to the Server component auth Issues and PRs related to Authentication / OAuth labels Jan 23, 2026

@chr-hertel chr-hertel left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi again @sveneld! This is great already - went to a first round of very detailed review directly. Thanks again for working on this! 🙏

Didn't manage to go through all, but dropping the first round of comments.

Comment thread examples/server/oauth-keycloak/README.md Outdated
Comment thread examples/server/oauth-keycloak/README.md
Comment thread examples/server/oauth-keycloak/server.php Outdated
Comment thread examples/server/oauth-keycloak/server.php Outdated
Comment thread examples/server/oauth-keycloak/server.php Outdated
Comment thread src/Server/Transport/Middleware/JwtTokenValidator.php Outdated
Comment thread src/Server/Transport/Middleware/JwtTokenValidator.php Outdated
Comment thread src/Server/Transport/Middleware/JwtTokenValidator.php Outdated
Comment thread src/Server/Transport/Middleware/JwtTokenValidator.php Outdated
Comment thread src/Server/Transport/Middleware/AuthorizationResult.php Outdated
@sveneld

sveneld commented Jan 28, 2026

Copy link
Copy Markdown
Contributor Author

@chr-hertel I updated pull request

@chr-hertel chr-hertel mentioned this pull request Feb 8, 2026
6 tasks
@Nyholm

Nyholm commented Feb 11, 2026

Copy link
Copy Markdown
Contributor

Can you please rebase this now when the Middleware Handler is merged?

Signed-off-by: Volodymyr Panivko <sveneld300@gmail.com>
@sveneld

sveneld commented Feb 11, 2026

Copy link
Copy Markdown
Contributor Author

Can you please rebase this now when the Middleware Handler is merged?

Done.

@Nyholm Nyholm left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great. Looks good.

I only have small things to comment at. I have not tested it out.

Comment thread src/Server/Transport/Middleware/AuthorizationMiddleware.php Outdated
Comment thread src/Server/Transport/Middleware/AuthorizationMiddleware.php Outdated
Comment thread src/Server/Transport/Middleware/AuthorizationMiddleware.php Outdated
Comment thread src/Server/Transport/Middleware/AuthorizationMiddleware.php Outdated
Comment thread src/Server/Transport/Middleware/AuthorizationMiddleware.php Outdated
Comment thread src/Server/Transport/Middleware/OAuthProxyMiddleware.php Outdated
Comment thread src/Server/Transport/Middleware/OidcDiscovery.php Outdated
Comment thread tests/Unit/Server/Transport/Middleware/AuthorizationMiddlewareTest.php Outdated
Comment thread src/Server/Transport/Middleware/JwtTokenValidator.php Outdated
Comment thread composer.json Outdated
@sveneld

sveneld commented Feb 16, 2026

Copy link
Copy Markdown
Contributor Author

I have updated the pull request, but I am not satisfied with the result. I need more time to rethink the flow and improve the implementation.

@chr-hertel

Copy link
Copy Markdown
Member

@sveneld anything in particular you would need feedback here?
can tell you about nit picks already, e.g. using Mcp\Exception\*Exception classes instead of built-in ones

@sveneld

sveneld commented Feb 18, 2026

Copy link
Copy Markdown
Contributor Author

Thank you, no feedback needed.

@sveneld

sveneld commented Feb 22, 2026

Copy link
Copy Markdown
Contributor Author

@chr-hertel take a look.

…nversion

Signed-off-by: Volodymyr Panivko <sveneld300@gmail.com>
@chr-hertel

Copy link
Copy Markdown
Member

@chr-hertel take a look.

@sveneld will do so this weekend - thanks! 👍

@chr-hertel chr-hertel left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @sveneld, getting into polishing here I feel - thanks again for your work! Pretty sure i missed some parts, but you get the overall vibe of my review i guess.

Could you as well add some docs, especially what users would need to enable this feature? or could be a follow up PR if you like

Comment thread examples/server/oauth-keycloak/server.php
Comment thread examples/server/oauth-keycloak/server.php Outdated
Comment thread examples/server/oauth-keycloak/server.php Outdated
Comment thread examples/server/oauth-keycloak/server.php Outdated
Comment thread examples/server/oauth-keycloak/server.php Outdated
Comment thread src/Server/Transport/Http/Middleware/OAuthProxyMiddleware.php Outdated
Comment thread src/Server/Transport/Http/Middleware/OAuthProxyMiddleware.php Outdated
Comment thread src/Server/Transport/Http/OAuth/OidcDiscovery.php Outdated
Comment thread src/Server/Transport/Http/OAuth/OidcDiscovery.php Outdated
Comment thread src/Server/Transport/Http/Middleware/OAuthProxyMiddleware.php Outdated
@chr-hertel chr-hertel added the needs more work Not ready to be merged yet, needs additional follow-up from the author(s). label Mar 1, 2026
Volodymyr Panivko and others added 4 commits March 2, 2026 10:00
…into oauth_middleware

# Conflicts:
#	composer.json
#	examples/server/oauth-keycloak/Dockerfile
#	examples/server/oauth-keycloak/McpElements.php
#	examples/server/oauth-keycloak/README.md
#	examples/server/oauth-keycloak/docker-compose.yml
#	examples/server/oauth-keycloak/server.php
#	examples/server/oauth-microsoft/Dockerfile
#	examples/server/oauth-microsoft/McpElements.php
#	examples/server/oauth-microsoft/README.md
#	examples/server/oauth-microsoft/docker-compose.yml
#	examples/server/oauth-microsoft/server.php
#	src/Server/Transport/StreamableHttpTransport.php
@sveneld

sveneld commented Mar 2, 2026

Copy link
Copy Markdown
Contributor Author

@chr-hertel I’ve updated the pull request. If everything looks good, I’ll rebase and squash the commits.

@sveneld

sveneld commented Mar 9, 2026

Copy link
Copy Markdown
Contributor Author

@chr-hertel should i change something else?

@chr-hertel

Copy link
Copy Markdown
Member

@sveneld can you please double check the diff - feels a bit bloated, see Mcp\Server\Transport\Middleware vs. Mcp\Server\Transport\Http\Middleware

Remove old Mcp\Server\Transport\Middleware\ classes that were duplicated
in Mcp\Server\Transport\Http\Middleware\ and Http\OAuth\. Update docs
to reference the correct namespaces.
@sveneld

sveneld commented Mar 9, 2026

Copy link
Copy Markdown
Contributor Author

@chr-hertel I reviewed the diff — you were right, the Mcp\Server\Transport\Middleware namespace was a leftover that duplicated classes already present in Mcp\Server\Transport\Http\Middleware and Mcp\Server\Transport\Http\OAuth.

Removed the duplicate namespace and updated the docs accordingly. The diff should be clean now.

Comment thread src/Server/Transport/Http/Middleware/OAuthProxyMiddleware.php Outdated
Comment thread src/Server/Transport/Http/OAuth/JwtTokenValidator.php Outdated
@chr-hertel chr-hertel added this to the 0.5.0 milestone Mar 9, 2026
…add JSON_THROW_ON_ERROR

- JwtTokenValidator: replace catch(\Throwable) with explicit
  \InvalidArgumentException|\UnexpectedValueException|\DomainException,
  letting RuntimeException from JwksProvider propagate as infrastructure errors
- OAuthProxyMiddleware: add \JSON_THROW_ON_ERROR to json_encode for metadata response
- Update tests to expect RuntimeException for JWKS infrastructure failures

@chr-hertel chr-hertel left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a ton @sveneld - I know it really took some effort and loops with all those changes! Thanks for your patience and endurance! 🥳

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

auth Issues and PRs related to Authentication / OAuth needs more work Not ready to be merged yet, needs additional follow-up from the author(s). Server Issues & PRs related to the Server component

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Server] Implement OAuth Flow for Client Authentication

3 participants