[BUG]: Virtual server OAuth audience auto-learn cannot bootstrap with Authentik (aud == client_id)
๐ 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
- Deploy v1.0.0 with Authentik (or any IdP without RFC 8707 support that mints
aud == client_id). - Register a virtual server via the admin UI with
oauth_enabled = true. The form has noclient_idfield, sooauth_configends up with neitherclient_idnorresourceset. - Connect a client (e.g. Claude.ai custom connector) and complete the OAuth flow. Authentik issues a valid token with
aud = "<authentik-client-id>". - Client sends the token โ 401
Audience doesn't match. Auto-learn never fires. - 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 theclient_idfallback, auto-learn runs, andresourceis persisted.
๐ค Expected Behavior
Either approach closes the gap:
- Expose
client_idon 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]onoauth_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_idand ignores theresourceparameter 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.
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
- Open the OAuth-enabled Virtual Server in the Admin UI.
- Make an unrelated change, for example add or remove an associated tool.
- Save the Virtual Server without intending to modify OAuth.
- Read the Virtual Server through the API or inspect the stored
oauth_config. - 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.pyadmin_edit_server()creates a newoauth_configdictionary from submitted form fields instead of merging withexisting_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
resourcecontract.
- The 1.0.8 Virtual Server form has no field matching the verifier's
mcpgateway/transports/streamablehttp_transport.py- Inbound audience verification reads
resource, thenclient_id; the existingaudienceform value is not used by this path.
- Inbound audience verification reads
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
- Load and copy
existing_server.oauth_configduring an edit. - Overlay only OAuth fields explicitly submitted by the form.
- Remove a key only when the administrator explicitly clears that field.
- Clear/deactivate the complete OAuth configuration only when OAuth is explicitly disabled.
- Align the form field with
resource; avoid persisting an inertaudiencekey for this inbound verification path.
Regression tests should verify that:
- an unrelated tool-association edit preserves
resource; - an unrelated edit preserves API-configured
client_idand unknown/future OAuth keys; resourceround-trips as bothstrandlist[str];- explicitly clearing
resourceremoves only that key; - explicitly disabling OAuth follows the documented clearing behavior; and
- API behavior remains unchanged.
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.