Skip to content

How the OAuth proxy works

The OAuth proxy is a facade over your identity provider (IdP). Every OAuth hop for one MCP server and one IdP lives under a single prefix in the Core Proxy:

http://localhost:8100/v1/proxy/oauth/{mcp_server_id}/{idp_server_id}
Method + path (relative to the prefix) Purpose Auth
GET /.well-known/oauth-protected-resource RFC 9728 protected-resource metadata pointing at this facade none
GET /.well-known/openid-configuration IdP discovery document with issuer, authorization_endpoint, token_endpoint and registration_endpoint rewritten to this facade none
GET /authorize Redirects (307) to the IdP for registered clients; bridges CIMD clients none
GET /callback IdP redirect target for bridged CIMD flows only none
POST /token authorization_code or refresh_token grant; returns an MCP JWT optional API key or gateway JWT
POST /revoke Marks the caller’s stored token for this MCP server revoked MCP JWT
GET /tokens Lists the caller’s stored token records (metadata only) API key or gateway JWT, plus the MCP JWT
POST /register RFC 7591 registration, forwarded to the IdP’s registration_endpoint X-Tenant-ID header

The OAuth paths skip the tenant middleware: the tenant is resolved from the MCP server record named in the path, so OAuth clients do not need to send X-Tenant-ID (except to /register).

MCP client Brutor Core Proxy IdP (HubSpot, GitHub, ...)
│ GET .well-known/* │ │
│────────────────────────────────>│ build from IdP record │
│ (facade URLs) │ │
│<────────────────────────────────│ │
│ GET /authorize ───────────────>│ 307 ─────────────────────────────>│ user consents
│<──────────────────────── code ─────────────────────────────────────── │
│ POST /token (code) │ │
│────────────────────────────────>│ POST token_endpoint (code) │
│ │───────────────────────────────────>│
│ │ access_token, refresh_token │
│ │<───────────────────────────────────│
│ │ GET userinfo → stable subject │
│ │ encrypt + store, mint MCP JWT │
│ MCP JWT │ │
│<────────────────────────────────│ │
│ MCP call + MCP JWT ───────────>│ decrypt stored token ──> upstream MCP server
  1. Discovery. The gateway builds both discovery documents from the IdP record you configured (and its cached discovery document, if any). It does not fetch the upstream MCP server’s metadata. Sparse IdPs get defaults: response_types_supported: ["code"], grant_types_supported: ["authorization_code", "refresh_token"], token_endpoint_auth_methods_supported: ["client_secret_post", "none"], code_challenge_methods_supported: ["S256"].

  2. Consent. A registered client_id is redirected to the IdP’s own authorization endpoint with the query string unchanged, so the IdP redirects back to the client’s redirect_uri with a code.

  3. Token exchange. The client posts the code to the facade’s /token. The gateway adds the client secret from the OAuth App Client record when the client type needs one, exchanges the code with the IdP, and calls the IdP’s userinfo_endpoint to get a stable subject for the user.

  4. Custody. The provider’s access token, refresh token and client secret are encrypted with ENCRYPTION_KEY and upserted into oauth_tokens, one row per (tenant, IdP subject, MCP server). The row records the IdP’s issuer.

  5. MCP JWT. The client receives an MCP JWT signed with JWT_SECRET. That is the only credential it holds for this server.

  6. Use. On each call to /v1/proxy/mcp/{mcp_server_id}, the gateway verifies the MCP JWT, loads the stored token, refreshes it if it has expired, and sets Authorization: Bearer <provider access token> on the upstream request. The caller’s own Authorization and X-API-Key headers are always stripped before the request leaves the gateway.

The token returned by /token is HMAC-signed (JWT_ALGORITHM, default HS256) with JWT_SECRET, issued by brutor-ai-gateway, and valid for JWT_EXPIRES_IN seconds (default 3600). Decoded, its payload looks like this:

{
"sub": "52731918",
"iat": 1793284800,
"exp": 1793288400,
"iss": "brutor-ai-gateway",
"jti": "5b0e7a9c-3d1f-4c52-9a77-0f3c2e8d4b11",
"tenant_id": "default",
"user_id": "52731918",
"mcp_server_id": "mcp-system-hubspot-default",
"username": "martin@example.com",
"email": "martin@example.com",
"type": "mcp_auth"
}
Claim Meaning Example
sub / user_id The user’s subject at the IdP, read from userinfo (sub, then id, id_str, user, user_id, userid, hub_id) 52731918
mcp_server_id The one MCP server this token unlocks mcp-system-hubspot-default
tenant_id Tenant of the MCP server default
username, email, name Optional profile fields from userinfo (login, preferred_username, username or user for the name) octocat
type Always mcp_auth. A token of this type is never accepted as a gateway session or API credential mcp_auth

Refresh is server-side and invisible to the client:

  • When the stored access token has expired and a refresh token is stored, the gateway refreshes it with the IdP during the MCP call, under a per-token lock so concurrent calls trigger one refresh, and stores the new tokens.
  • If the IdP answers the refresh as revoked, the row is marked revoked.
  • If the IdP’s issuer has changed since the token was minted, the gateway revokes the row instead of refreshing it against a different authorization server.
  • When no usable token remains, the MCP call returns 401 oauth_reauth_required and the user has to authorize again.

The MCP JWT has its own lifetime (JWT_EXPIRES_IN). To renew it without a new consent, post it back to /token with grant_type=refresh_token (see Refresh the MCP JWT).

The gateway cannot list the tools of an OAuth-protected server until it holds a user’s token. The first successful token exchange for a server runs MCP capability discovery with that token and stores the tools, resources and prompts on the server record. Later MCP calls re-run discovery in the background when the stored capabilities are missing or expired. Until a first user has authorized, tool-level controls on that server have no tool list to show.

Every token exchange and refresh writes a proxy log row with type oauth, the oauth_flow_type (authorization_code or refresh_token), the IdP server ID and the IdP user’s ID, username and email, plus a proxy metric oauth_token_exchange. MCP calls that carried an MCP JWT record the IdP user on the MCP proxy log. When an agent principal drives a call with a brokered token, the token row records that agent’s use. See Logs.

Variable Where Effect Example
ENCRYPTION_KEY Core Proxy and Control Plane (must match) Encrypts stored access tokens, refresh tokens and client secrets k7Qe...base64...=
JWT_SECRET Core Proxy and Control Plane (must match) Signs the MCP JWT 9f1c...64 hex chars...
JWT_EXPIRES_IN Core Proxy MCP JWT lifetime in seconds; also the expires_in returned by /token 3600
JWT_ALGORITHM Core Proxy HS256, HS384 or HS512 HS256
PROXY_PUBLIC_URL Core Proxy Base URL written into discovery documents. A value containing localhost is ignored and the URL is rebuilt from X-Forwarded-Proto / X-Forwarded-Host / Host https://gateway.example.com
REDIS_URL Core Proxy Idempotency lock on code exchange; required for CIMD bridging redis://redis:6379/0