Skip to content

Using /.well-known/ OAuth endpoints behind custom path on GKE #1335

Open

Description

@whitewg77

Question

I am running a simple MCP server (built with FastMCP) behind a gateway on GKE with an custom path defined by an HttpRoute. I am attempting to use the /.well-known/ endpoints for OAuth authentication and authorization. Unfortunately, it seems that the routes for the /.well-known/ endpoints; as well as the other OAuth endpoints /authorize, /token, /register, and /revoke, are all hardcoded in the MCP SDK within mcp/server/auth/routes.py within the create_auth_routes() function.

So, for example...

If my MCP server is deployed at https://{my-gateway}/custom/path/
However, the well-known endpoints are pointing to https://{my-gateway}/.well-known/*, which are obviously returning 404 Not Found responses.

When starting my server, I am passing the 'path' parameter in the mcp.run() command. Using my example above, my run command would look like -- mcp.run(transport="http", host="0.0.0.0", port=8080, path="/custom/path/mcp"). However, this path parameter seems to have zero effect on the OAuth well-known endpoints.

So, the question is... Is this expected behavior, and if so, how should we run MCP servers that use well-knonw OAuth endpoints behind API Proxies (Apigee, APIM, etc) or GKE Gateways that require a custom path?

Additional Context

mcp version -- 1.13.1
fastmcp version -- 2.11.4.dev128+5b433f5

Activity

  1. shulkx commented on Sep 3, 2025

    @shulkx
    Contributor

    I think it is related with this pull request: #1288

  2. whitewg77 commented on Sep 3, 2025

    @whitewg77
    Author

    Thanks @shulkx for the pull request link. However, the listed Expected Behavior in this pull request states: "The .well-known/oauth-protected-resource endpoint should always be served at the root path (/.well-known/oauth-protected-resource) regardless of the resource_server_url configuration". That seems the exact opposite of what would be needed in use cases like mine where the MCP server must run at a custom path. When attached to a GKE gateway (or Api proxy such as Apigee), serving the well-known endpoints from the root path is always going to return 404 responses.

  3. shulkx commented on Sep 27, 2025

    @shulkx
    Contributor

    Hi @whitewg77 - The root cause is because the well-known URL generated for WWW authenticated header (used to tell the client where the well-known endpoint is) and the real well-known route endpoint are mismatched.

    For WWW authenticated header, the existing implementation appends the .well-known/oauth-protected-resource directly after the resource_server_url.

    http://0.0.0.0:8000/custom/path/mcp/.well-known/oauth-protected-resource
    

    For route endpoint generation, it append the .well-known/oauth-protected-resource directly after the root URL.

    http://0.0.0.0:8000/.well-known/oauth-protected-resource
    

    Therefore, if we have customized path, it will cause a mismatch between those two and lead to 404.

    I have implemented a fix for this #1407.

  4. added
    ready for workEnough information for someone to start working on
    P2Moderate issues affecting some users, edge cases, potentially valuable feature
    on Dec 10, 2025
  5. enjoykumawat commented on Apr 7, 2026

    @enjoykumawat

    I'd like to work on this. The OAuth well-known and auth endpoints need to respect the custom path parameter so they're reachable behind gateways with custom base paths. Will submit a PR shortly.

  6. added
    needs decisionIssue is actionable, needs maintainer decision on whether to implement
    and removed
    ready for workEnough information for someone to start working on
    on Apr 17, 2026
  7. MoAz06 commented on Nov 20, 2026

    @MoAz06

    Still reproduces on v2 (main at 77001c9 and release 2.3.0). With an MCPServer whose issuer_url is https://gw.example.com/custom/path:

    • The client looks for the auth server metadata at /.well-known/oauth-authorization-server/custom/path (plus two OIDC variants): all 404.
    • The server only serves it at /.well-known/oauth-authorization-server.
    • The endpoints that metadata advertises (/custom/path/authorize, /token, /register, /revoke) are all 404.

    The protected-resource side already handles the path (#1407), so it's only the auth server side. RFC 8414 §3.1 says to serve the metadata at the path-inserted URL and put the endpoints under the issuer path, which is also where #2401 ended up.

    Repro (no network)
    import re
    from types import SimpleNamespace
    from urllib.parse import urlparse
    from starlette.testclient import TestClient
    from mcp.client.auth.utils import build_oauth_authorization_server_metadata_discovery_urls
    from mcp.server.auth.settings import AuthSettings, ClientRegistrationOptions, RevocationOptions
    from mcp.server.mcpserver import MCPServer
    
    GW = "https://gw.example.com"
    ISSUER = GW + "/custom/path"
    
    async def _no_token(token):
        return None
    
    server = MCPServer(
        "repro",
        auth_server_provider=SimpleNamespace(load_access_token=_no_token),  # type: ignore[arg-type]
        auth=AuthSettings(
            issuer_url=ISSUER,
            resource_server_url=ISSUER + "/mcp",
            client_registration_options=ClientRegistrationOptions(enabled=True),
            revocation_options=RevocationOptions(enabled=True),
        ),
    )
    c = TestClient(server.streamable_http_app(), base_url=GW)
    
    www = c.post("/mcp").headers["www-authenticate"]
    prm = c.get(urlparse(re.search(r'resource_metadata="([^"]+)"', www).group(1)).path).json()
    as_url = str(prm["authorization_servers"][0])
    for u in build_oauth_authorization_server_metadata_discovery_urls(as_url, ISSUER + "/mcp"):
        print("discovery", urlparse(u).path, c.get(urlparse(u).path).status_code)
    
    meta = c.get("/.well-known/oauth-authorization-server").json()
    for k in ("authorization_endpoint", "token_endpoint", "registration_endpoint", "revocation_endpoint"):
        p = urlparse(meta[k]).path
        print("advertised", p, c.request("GET" if k == "authorization_endpoint" else "POST", p).status_code)

    #2401 was closed in the pre-v2 cleanup. Would you take a v2 PR along the same lines? Happy to do it, unless @enjoykumawat wants to pick it back up.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Moderate issues affecting some users, edge cases, potentially valuable featureauthIssues and PRs related to Authentication / OAuthenhancementRequest for a new feature that's not currently supportedgood first issueGood for newcomersneeds decisionIssue is actionable, needs maintainer decision on whether to implementquestionFurther information is requested

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions