mcp-context-forge icon indicating copy to clipboard operation
mcp-context-forge copied to clipboard

[BUG]: Virtual server OAuth audience auto-learn cannot bootstrap with Authentik (aud == client_id)

Open kimsehwan96 opened this issue 7 months ago โ€ข 2 comments

๐Ÿž Bug Summary

After v1.0.0 (which includes #4404 / #4410), virtual-server OAuth still fails with Audience doesn't match when using Authentik. The auto-learn mechanism never engages on the first request, and a manual DB write is still required.

The fallback path builds expected_audiences = [canonical_url, oauth_config.client_id], but the virtual-server admin form doesn't expose client_id. With client_id empty, the list collapses to [canonical_url], which never matches an Authentik token whose aud == client_id. Verification fails before _persist_learned_server_audience runs, so auto-learn cannot bootstrap.

This is a follow-up to #4171, which was closed assuming #4404/#4410 fully resolved the Authentik case ("No UI change needed, no manual DB manipulation"). In practice it doesn't, because there is no path to seed client_id for virtual servers.


๐Ÿงฉ Affected Component

Select the area of the project impacted:

  • [x] mcpgateway - API
  • [x] mcpgateway - UI (admin panel)
  • [ ] mcpgateway.wrapper - stdio wrapper
  • [x] Federation or Transports
  • [ ] CLI, Makefiles, or shell scripts
  • [ ] Container setup (Docker/Podman/Compose)
  • [ ] Other (explain below)

๐Ÿ” Steps to Reproduce

  1. Deploy v1.0.0 with Authentik (or any IdP without RFC 8707 support that mints aud == client_id).
  2. Register a virtual server via the admin UI with oauth_enabled = true. The form has no client_id field, so oauth_config ends up with neither client_id nor resource set.
  3. Connect a client (e.g. Claude.ai custom connector) and complete the OAuth flow. Authentik issues a valid token with aud = "<authentik-client-id>".
  4. Client sends the token โ†’ 401 Audience doesn't match. Auto-learn never fires.
  5. Workaround: UPDATE servers SET oauth_config = jsonb_set(oauth_config, '{client_id}', '"<authentik-client-id>"') WHERE id = '<vs-id>'; After this, the next request matches via the client_id fallback, auto-learn runs, and resource is persisted.

๐Ÿค” Expected Behavior

Either approach closes the gap:

  • Expose client_id on the virtual-server admin form, like the gateway form already does. Smallest change; feeds the existing fallback path without touching audience logic. Matches the original suggestion in #4171.
  • Or accept a list[str] on oauth_config.resource (RFC 7519 ยง4.1.3). Lets operators set the accepted audiences explicitly instead of relying on first-write learning. PyJWT already supports list audiences with any-element semantics.

๐Ÿ““ Logs / Error Output

WARN [mcpgateway.utils.verify_credentials] OAuth access token verification failed: Audience doesn't match

๐Ÿง  Environment Info

You can retrieve most of this from the /version endpoint.

Key Value
Version or commit v1.0.0
Runtime Python 3.12, uvicorn
Platform / OS Linux (UBI 10), AWS EKS, linux/arm64
Container Docker

๐Ÿงฉ Additional Context (optional)

  • Reproduces with any IdP that mints aud == client_id and ignores the resource parameter from the OAuth authorization request.
  • The gateway-side OAuth form already accepts client_id. The asymmetry between gateway and virtual-server forms is the root cause.

kimsehwan96 avatar May 06 '26 00:05 kimsehwan96

Additional 1.0.8 reproduction: unrelated Admin UI edits delete API-configured OAuth fields

I reproduced a related destructive-update path on ContextForge 1.0.8. This is not specific to Authentik and can affect any external IdP token whose accepted audience was configured through the API.

Setup

An OAuth-protected Virtual Server was configured with the API using this generic shape:

{
  "oauth_enabled": true,
  "oauth_config": {
    "authorization_servers": ["https://idp.example.com/realms/example"],
    "scopes_supported": ["openid", "email", "profile"],
    "resource": "contextforge",
    "client_id": "contextforge"
  }
}

The client then presented a valid access token with an audience such as:

{
  "iss": "https://idp.example.com/realms/example",
  "aud": ["contextforge", "client-application"],
  "typ": "Bearer"
}

The API-supplied configuration is accepted and Streamable HTTP authentication works correctly before an Admin UI edit.

Reproduction

  1. Open the OAuth-enabled Virtual Server in the Admin UI.
  2. Make an unrelated change, for example add or remove an associated tool.
  3. Save the Virtual Server without intending to modify OAuth.
  4. Read the Virtual Server through the API or inspect the stored oauth_config.
  5. Retry the same MCP authentication request.

Actual behavior

The Admin UI update reconstructs oauth_config from only the form fields it represents. API-configured keys that are not represented or restored by the form are removed. On 1.0.8, this removed both resource and client_id while retaining fields such as authorization_servers and scopes_supported.

The next Streamable HTTP request fails with:

OAuth access token verification failed: Audience doesn't match

Without oauth_config.resource or oauth_config.client_id, mcpgateway/transports/streamablehttp_transport.py falls back to the canonical Virtual Server URL. That URL does not match the otherwise-valid token audience.

This occurs before OPA, plugin hooks, tool execution, or downstream MCP invocation.

Expected behavior

An Admin UI edit to associations, metadata, or another unrelated field must preserve the complete existing OAuth configuration. Only OAuth fields explicitly changed or cleared by the administrator should be updated or removed.

Relevant code

  • mcpgateway/admin.py
    • admin_edit_server() creates a new oauth_config dictionary from submitted form fields instead of merging with existing_server.oauth_config.
  • mcpgateway/admin_ui/servers.js
    • The edit modal restores only a subset of the stored OAuth configuration.
  • mcpgateway/templates/admin.html
    • The 1.0.8 Virtual Server form has no field matching the verifier's resource contract.
  • mcpgateway/transports/streamablehttp_transport.py
    • Inbound audience verification reads resource, then client_id; the existing audience form value is not used by this path.

Relationship to PR #4610

PR #4610 correctly adds a Virtual Server Resource (Audience) field, restores oauth_config.resource into the edit form, and round-trips string/list resource values. That fixes the immediate resource loss in this reproduction.

However, admin_edit_server() in the PR still reconstructs oauth_config. API-configured client_id, provider-specific options, and future OAuth keys not represented by the UI can still be silently deleted during an unrelated edit.

Suggested completion criteria

  1. Load and copy existing_server.oauth_config during an edit.
  2. Overlay only OAuth fields explicitly submitted by the form.
  3. Remove a key only when the administrator explicitly clears that field.
  4. Clear/deactivate the complete OAuth configuration only when OAuth is explicitly disabled.
  5. Align the form field with resource; avoid persisting an inert audience key for this inbound verification path.

Regression tests should verify that:

  • an unrelated tool-association edit preserves resource;
  • an unrelated edit preserves API-configured client_id and unknown/future OAuth keys;
  • resource round-trips as both str and list[str];
  • explicitly clearing resource removes only that key;
  • explicitly disabling OAuth follows the documented clearing behavior; and
  • API behavior remains unchanged.

vgunjic avatar Aug 27 '26 13:08 vgunjic

Same on v1.0.11 with a generic OIDC provider (not Authentik), tokens also have aud == client_id. The virtual-server OAuth form has no client_id field, so the fallback audience is just [canonical_url] and never matches.

It has to be seeded via REST (PUT /servers/{id} with oauth_config.client_id). And because the edit form rebuilds oauth_config from its fields, any later edit (for example changing the tool selection) drops client_id again and the tools silently disappear until it is re-applied. Same rebuild behaviour as #6048.

Two changes would fix it: expose client_id (and resource) on the form, and keep the oauth_config keys the form does not render on save.

acirtautas avatar Oct 29 '26 07:10 acirtautas