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

[FEATURE]: Separate discovery credential for token-exchange gateways

Open lharrison13 opened this issue 1 month ago • 2 comments

Type of feature

  • [x] Enhancement to existing functionality

Summary: Discover tools on a token-exchange gateway without an end-user token Goal: Let a grant_type: "token-exchange" gateway list tools, resources and prompts with a service credential. Keep tool calls on the per-user RFC 8693 exchange. Why now: Token exchange released in 1.0.6. Discovery needs a subject token that the authorization server accepts. With an external IdP as the authorization server, such as IBM Security Verify or Keycloak, the gateway cannot supply one outside a user request. Operators run API calls with an IdP user token for every tool refresh.

Scope change on 2026-10-27: this issue first also asked for Admin UI form support for the token-exchange fields. That part is dropped. See "Out of scope" below.

Current behavior

  • Registration skips the discovery probe for the token-exchange grant and persists the gateway with an empty tool list (mcpgateway/services/gateway_service.py:5787). Tools appear only after POST /gateways/{id}/tools/refresh or POST /oauth/fetch-tools/{id}.
  • Both routes exchange the caller's inbound JWT as the subject token. The Admin UI session cookie and an API token each hold a ContextForge-issued JWT, including after an SSO login. An authorization server that does not trust the ContextForge issuer rejects the exchange, so discovery fails.
  • Health checks skip the grant for the same reason (gateway_service.py:5354), so a broken upstream surfaces on the first user call.
  • A refresh requires gateways.update (main.py:7757). An operator has to grant catalog write permission to a principal that only needs discovery.
  • The only valid subject_token_source on gateway paths is inbound_user_jwt (GatewayService._VALID_SUBJECT_TOKEN_SOURCES, gateway_service.py:1009).

User story: Discover tools with a separate service credential

As a: platform admin I want: a token-exchange gateway to discover tools with a service credential, separate from the per-user exchange used for tool calls So that: registration, manual refresh, scheduled refresh and health checks work without an end user, and tool calls still act as the calling user

Proposed configuration

Add an optional discovery object to a token-exchange oauth_config:

{
  "grant_type": "token-exchange",
  "token_url": "https://idp.example.com/oauth2/token",
  "client_id": "downstream-mcp-client",
  "client_secret": "<encrypted-at-rest>",
  "target_audience": "downstream-mcp-client",
  "discovery": {
    "grant_type": "client_credentials",
    "scopes": ["mcp.discover"]
  }
}
  • discovery.grant_type supports client_credentials first.
  • discovery reuses the gateway token_url, client_id, client_secret and token_endpoint_auth_method unless it overrides them.
  • When discovery is absent, behavior stays as it is today.

Implementation notes

  • Validate and default the object in _validate_token_exchange_config() (gateway_service.py:1018).
  • Cache the service token keyed by gateway only, separate from TokenExchangeCache (mcpgateway/services/token_exchange_cache.py).
  • Use the token at the places that skip today: the registration probe (gateway_service.py:5787), the health check (gateway_service.py:5354) and the manual refresh path.
  • Secret handling needs no new code. _mask_oauth_config() (mcpgateway/schemas.py:4022) and _protect_oauth_config_value() (mcpgateway/services/encryption_service.py) both recurse into nested objects, so discovery.client_secret encrypts at rest and masks on read.
  • _validate_token_exchange_config() raises PublicValidationError, but the gateway create and update handlers have no branch for it. It falls into the generic ValueError branch and returns 400 {"message": "Unable to process input"} (mcpgateway/main.py:7680). Add a PublicValidationError branch ahead of it, as mcpgateway/routers/tokens.py:310 already does, so a caller learns which field failed.

Security requirements

  • Use the discovery token only for initialize, tools/list, resources/list, prompts/list and health checks.
  • Never use the discovery token for tools/call, resource reads, prompt gets, or any other request that runs on behalf of a user.
  • Keep invocation fail-closed. A call without a usable user subject token returns an error and never falls back to the discovery token. tool_service.py:4388 and gateway_service.py:1263 already raise there.
  • Store discovery tokens apart from the per-user token-exchange cache. Key them by gateway.
  • Audit discovery token requests through the structured token-exchange event with a correlation_id, marked as discovery (mcpgateway/utils/token_exchange_audit.py).
  • Keep creating or changing the discovery object restricted to platform admins (_enforce_token_exchange_admin_only, gateway_service.py:1129).

Acceptance criteria

Scenario: Registration discovers tools with the discovery credential
  Given a token-exchange gateway config with discovery.grant_type "client_credentials"
  When a platform admin registers the gateway
  Then the gateway obtains a client-credentials token and lists the upstream tools
  And the tools are saved in the catalog

Scenario: Refresh works without an accepted user subject token
  Given a token-exchange gateway with a discovery credential
  And a caller whose bearer token is a ContextForge-issued JWT
  When the caller triggers a tool refresh
  Then discovery uses the discovery credential
  And the catalog is updated

Scenario: Tool calls never use the discovery token
  Given a token-exchange gateway with a discovery credential
  When a user calls a tool
  Then the upstream receives a token exchanged from that user's token
  And the upstream never receives the discovery token

Scenario: Tool call without a user subject token fails closed
  Given a token-exchange gateway with a discovery credential
  When a request without a usable user JWT calls a tool
  Then the call is rejected
  And no request with the discovery token reaches the upstream

Scenario: Health checks run with the discovery credential
  Given a token-exchange gateway with a discovery credential
  When the health check runs
  Then it connects with the discovery token
  And a token-exchange gateway without a discovery credential is still skipped

Scenario: Discovery failure does not affect tool calls
  Given the discovery token request fails
  When a user calls a tool
  Then the per-user exchange runs as before

Scenario: Adding a discovery credential re-discovers the catalog
  Given a token-exchange gateway whose tools were discovered
  When a platform admin adds a discovery credential to its oauth_config
  Then the update re-discovers with the discovery credential
  And the tools stay in the catalog

Design sketch

sequenceDiagram
    participant Admin as API client / scheduler
    participant GW as ContextForge
    participant AS as Authorization server
    participant MCP as Upstream MCP server
    participant User as MCP client (user)

    Admin->>GW: Register gateway / refresh tools / health check
    GW->>AS: client_credentials (discovery)
    AS-->>GW: Service token
    GW->>MCP: initialize + tools/list (service token)
    MCP-->>GW: Tool list
    User->>GW: tools/call (user token)
    GW->>AS: RFC 8693 exchange (user subject token)
    AS-->>GW: User-scoped token
    GW->>MCP: tools/call (user-scoped token)

Out of scope

Admin UI and web UI form fields for the token-exchange grant and for discovery. The REST API already accepts every token-exchange field, so no backend work is needed for a form to exist:

  • GatewayCreate and GatewayUpdate take oauth_config as an open object (mcpgateway/schemas.py:3326, :3739).
  • _validate_token_exchange_config() validates it and applies defaults (gateway_service.py:1018).
  • GET /gateways/{id} returns the stored config with sensitive values masked (convert_gateway_to_read, gateway_service.py:6396), so a form can prefill each field.
  • An update rewrites oauth_config only when the request supplies it (gateway_service.py:3330), and protect_oauth_config_for_storage() preserves a masked secret that is sent back unchanged.
  • A non-admin submission is already rejected with 403 (main.py:7532 on create, :7670 on update).

The remaining gap is limited to the Jinja templates and the form builder in mcpgateway/admin.py, which the React web UI replaces. A UI issue can be opened when the fields are wanted there.

Catalog pruning when the discovery probe is skipped is split out to #7347. That defect is independent of this feature and affects a gateway without a discovery credential.


MCP standards check

  • [x] Change adheres to current MCP specifications
  • [x] No breaking changes to existing MCP-compliant integrations

The upstream MCP server must accept a service token for the list methods and must still require a user token for tool calls. That is a server-side policy, not a protocol change.

A client_credentials discovery token carries the service identity and no user claims. An upstream server that requires user claims on every request needs a discovery profile that accepts a service token for initialize, tools/list, resources/list and prompts/list. This answers the question in https://github.com/IBM/mcp-context-forge/issues/7236#issuecomment-6223693504.


Alternatives considered

  • one_time_auth: it uses separate credentials for discovery, but it clears auth_type, auth_value and oauth_config after registration. It cannot coexist with token exchange and cannot refresh later.
  • Discover with a bearer token, then switch to token exchange: the update path re-initializes the gateway, token exchange returns an empty tool list, and catalog reconciliation prunes the discovered tools. See #7347.
  • A client_credentials gateway: it discovers tools, but tool calls then use the service identity and lose the user identity.
  • The authorization server trusts ContextForge as a JWT issuer: this needs RS256 signing keys, certificate upload, a custom subject token type and user mapping in the IdP. The same gateway then receives different subject token types from MCP clients and from internal callers.
  • Keep the API route: POST /gateways/{id}/tools/refresh with an IdP user token works today. It needs a signed-in user with gateways.update for every refresh, and nothing refreshes automatically.

Additional context

Docs to update in the same change: docs/docs/architecture/oauth-design.md ("Tool discovery for token-exchange gateways") and docs/docs/manage/identity-propagation.md ("Converting a Gateway to On-Behalf-Of"). State how discovery works with and without a discovery credential, and when a refresh needs an authorization server that trusts the ContextForge JWT issuer.

Existing tests already cover the admin-only gate (tests/unit/mcpgateway/services/test_gateway_service.py, tests/unit/mcpgateway/services/test_catalog_service.py), the token_url validation (tests/unit/mcpgateway/services/test_token_exchange_integration.py) and fail-closed invocation (test_gateway_service.py:4060, tests/unit/mcpgateway/routers/test_oauth_router.py:2282). New deny-path coverage needed here: the discovery token never reaches tools/call, and a discovery token request failure leaves the per-user exchange working.

Seen with IBM Security Verify and with Keycloak as the authorization server. Each exchange authenticates with the target application's client_id and client_secret.

lharrison13 avatar Oct 14 '26 19:10 lharrison13

We'd use this, mainly User Story 2. Our setup, as a concrete case:

  • ContextForge 1.0.11 with an external Keycloak as the authorization server. Every downstream MCP server sits behind a token-exchange gateway (RFC 8693, a separate audience per server), so all registrations hit the same problem.
  • Discovery needs a real end-user Keycloak token today. Registration and every POST /gateways/{id}/tools/refresh run with a fresh user bearer, through a helper script, because the Admin UI's session JWT is not accepted by the IdP.
  • That user needs management rights just to refresh. Our users are otherwise pure MCP clients, so we have to give a principal team membership plus gateways.read/gateways.update only to bootstrap discovery. A service credential limited to initialize and */list would let principals stay least-privilege.
  • Health checks skip these gateways, so a broken upstream shows up only on the first user call.
  • An empty or skipped discovery on update has deleted a gateway's tools for us. The "switching the auth config does not drop discovered tools" scenario matters a lot to us. If possible, please apply the same rule to every path that reconciles a token-exchange gateway's catalog, not only the auth switch.

We agree with the security rules as written: discovery-only use, never for tools/call, fail closed without a user token, a separate cache, and audit events marked as discovery. One question to plan the receiver side: is the discovery token expected to carry no user identity at all? Our MCP servers currently require user identity claims on every request, so they would need an explicit "discovery profile" that accepts a service token only for initialize and the list methods.

vgunjic avatar Oct 16 '26 20:10 vgunjic

Moved the catalog pruning bug to #7347.

#7347 covers tools deleted on PUT /gateways/{id} and POST /gateways/{id}/state when the discovery probe is skipped. As @vgunjic requested, the fix applies to every path that reconciles a token-exchange catalog. It does not depend on the discovery credential and can be merged first, since gateways without a discovery credential still skip the probe.

This issue now covers the discovery credential. The Admin UI form work is dropped because the REST API already accepts every token-exchange field. The missing support is in the Jinja templates, which the new web UI replaces. See the out-of-scope section for details.

The MCP standards check section answers whether the discovery token carries user identity.

a-effort avatar Oct 27 '26 17:10 a-effort