Skip to content

fix(connectors): path-aware OAuth metadata discovery for custom MCP servers (RFC 8414) - #398

Open
ch0rch wants to merge 1 commit into
peaberry-studio:mainfrom
opendraft-tech:fix/custom-mcp-oauth-path-discovery
Open

ch0rch wants to merge 1 commit into
peaberry-studio:mainfrom
opendraft-tech:fix/custom-mcp-oauth-path-discovery

Conversation

@ch0rch

@ch0rch ch0rch commented Aug 2, 2026

Copy link
Copy Markdown

Problem

Custom MCP connectors fail to complete OAuth against servers that live under a URL path (e.g. https://mcp.facebook.com/ads). discoverOAuthMetadata (apps/web/src/lib/connectors/oauth-metadata.ts) only queries /.well-known/oauth-authorization-server at the host root and, on 404, falls back to <origin>/authorize|token|register — discarding the server's path segment.

Observed against Meta's official Ads MCP:

  1. Dynamic client registration is attempted at https://mcp.facebook.com/register → 404 → "Dynamic client registration failed for this MCP server".
  2. The authorize redirect goes to https://mcp.facebook.com/authorize → Meta responds "MCP server not found for path: /authorize".

The server's real metadata is published at the RFC 8414 path-aware location https://mcp.facebook.com/.well-known/oauth-authorization-server/ads, with authorization_endpoint and token_endpoint pointing at facebook.com/graph.facebook.com.

Fix

Discovery now tries, in order:

  1. RFC 8414 path-aware location: /.well-known/oauth-authorization-server/<path>
  2. Root document: /.well-known/oauth-authorization-server (previous behavior)
  3. Existing <origin>-based defaults (unchanged last resort)

Endpoints from discovered metadata are used as-is, so servers whose authorization/token endpoints live on a different host (as Meta's do) work correctly. Behavior for servers without a path component is unchanged.

Testing

  • Added coverage for path-based MCP server metadata discovery (path-aware hit, root fallback, default fallback).
  • OAuth connector suites: 42 pass. Full connector surface: 1078 pass (two suites fail identically on the clean v5.0.0 tag — pre-existing).
  • Manually verified the discovery chain against https://mcp.facebook.com/ads (metadata resolves; end-to-end OAuth additionally requires a pre-registered Meta app, since Meta's dynamic client registration is allowlisted).

… custom MCP servers

MCP servers hosted under a path (e.g. https://mcp.facebook.com/ads) publish
their OAuth authorization server metadata at the path-aware well-known URL
(/.well-known/oauth-authorization-server/ads). Discovery previously only
queried the root document and, on 404, fell back to <origin>/authorize,
<origin>/token and <origin>/register, dropping the server path entirely.

For the Meta Ads MCP this produced a registration call against
https://mcp.facebook.com/register (404) and an authorize redirect to
https://mcp.facebook.com/authorize, which Meta's router rejects with
"MCP server not found for path: /authorize".

Try the RFC 8414 path-inserted metadata URL first when the MCP server URL
has a path component, then fall back to the root document, keeping the
existing origin-based defaults as the last resort.

@Inakitajes Inakitajes 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.

Thanks so much for contributing—this is a great catch. The path-aware lookup correctly identifies the metadata endpoint used by Meta Ads and fixes the immediate failure.

However, I don’t think we can merge this as-is yet:

  1. The full test suite currently fails. Running pnpm test results in 7 failures in tests/connectors-oauth.test.ts. Some mocks do not account for the additional metadata request, while other assertions still expect the root URL. Please update these integration tests.
  2. The MCP resource URL is not necessarily the authorization-server issuer. MCP requires clients to discover Protected Resource Metadata first (RFC 9728), read authorization_servers, and then apply RFC 8414 discovery to the selected issuer. Applying RFC 8414 directly to every MCP URL works for this Meta case, but it is not generally correct.
  3. The returned issuer is not validated. RFC 8414 §3.3 requires the metadata issuer to exactly match the issuer used to construct the discovery URL. Meta currently returns https://www.facebook.com from metadata discovered through https://mcp.facebook.com/ads, so supporting Meta may require a deliberate, narrowly documented compatibility exception rather than weakening validation globally.
  4. Fallback behavior is too permissive. The path-aware request currently falls back to the root document after any failure, including 401, 403, 429, or 500. This could silently select unrelated root metadata during an outage or configuration error. The fallback should at least be restricted to an explicit “not found” response and covered by tests.

Before merging, could you please:

  • update all affected integration tests;
  • add an end-to-end test covering path-aware discovery, client registration/static fallback, authorization URL generation, and token exchange;
  • implement RFC 9728 protected-resource discovery before RFC 8414 discovery, or clearly scope this as a provider-specific compatibility workaround;
  • define and test strict fallback semantics;
  • add issuer-validation coverage and document any Meta-specific exception.

Also, the PR branch is currently behind the target branch. Please rebase it onto the latest peaberry-studio/arche:main and resolve any resulting conflicts before requesting another review. This will ensure the updated implementation and tests are validated against the current codebase.

Thanks again, he underlying issue is real, and the proposed URL construction is useful.

We just need to make sure the solution is safe and correct beyond the single Meta endpoint.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants