fix: prefix auth routes with issuer_url base path for gateway deployments - #2401
fix: prefix auth routes with issuer_url base path for gateway deployments#2401enjoykumawat wants to merge 2 commits into
Conversation
|
Friendly ping — happy to address any feedback or make changes if needed. Let me know if there's anything blocking review. |
|
This prefixing also competes with Which is why FastMCP has Prefixing the rest of the endpoints makes sense, but afaict FastMCP has hardcoded references to For trusted reverse proxying we could get the scheme and host via It would be nice if someone took a long hard look at all the various pieces in play here, like proxy forwarding headers and path stripping, FastMCP Just my 2c |
When an MCP server is deployed behind a gateway with a custom base path (e.g., /custom/path), the OAuth auth routes (.well-known, /authorize, /token, /register, /revoke) were hardcoded at root, making them unreachable through the gateway. Extract the path component from issuer_url and prefix it to all auth route registrations. This matches the metadata URLs already built by build_metadata(), which correctly use issuer_url + path. Backward compatible: when issuer_url has no path, routes stay at root. Github-Issue: modelcontextprotocol#1335 Reported-by: whitewg77
5250a9c to
fd2d465
Compare
The .well-known/oauth-authorization-server route was prefixed with the issuer's base path *after* the well-known suffix (e.g. /custom/path/.well-known/oauth-authorization-server), which RFC 8615 3 does not recognize as a well-known URI. RFC 8414 3.1 requires the suffix to be inserted between the authority and the path component instead: /.well-known/oauth-authorization-server/custom/path. The /authorize, /token, /register, /revoke routes are unaffected - they're plain URLs under the issuer's namespace, not well-known URIs.
|
Good catch, thanks — and you're right that this needed a real fix, not just a caveat. Pushed The On the broader |
Summary
Fixes #1335 — When an MCP server is deployed behind a gateway with a custom base path (e.g.,
https://gateway/custom/path/mcp), the OAuth auth routes (.well-known,/authorize,/token,/register,/revoke) are hardcoded at root, making them unreachable through the gateway.Root cause:
create_auth_routes()registers routes at fixed root paths (/.well-known/oauth-authorization-server,/authorize, etc.) regardless of theissuer_urlpath. Meanwhile,build_metadata()correctly builds metadata URLs usingissuer_url+ path, creating a mismatch.Fix: Extract the path component from
issuer_urland prefix it to all auth route registrations. This aligns the actual route paths with the metadata URLs already built bybuild_metadata().Backward compatible: when
issuer_urlhas no path (or just/),issuer_pathis empty and routes stay at root.Changes
src/mcp/server/auth/routes.py: Extractissuer_pathfromissuer_urland prefix all route pathstests/server/auth/test_routes.py: Add 3 tests for default paths, custom base path, and trailing slash handlingTest plan
test_routes.pytests pass (9 existing + 3 new)test_error_handling.pytests pass (no regression)test_auth_integration.pytests pass (no regression)