# IdP setup: Keycloak (oidc_proxy) The steps below walk through configuring Keycloak for the `oidc_proxy` inbound auth mode; see `MCP server authentication`. Use this instead of the `jwks walkthrough` when an MCP client you need supports neither pre-registration (entering a client ID) nor Client ID Metadata Documents; see `the client support table`. Rather than each client identifying itself to Keycloak, you register **one** confidential client, and the MCP server itself brokers the login on its behalf, so users enter no client ID. This is the same OAuth-proxy pattern as `IdP setup: Microsoft Entra ID`, but pinned to a generic OIDC issuer instead of Microsoft. Prerequisites: * A running Keycloak instance. `oidc_proxy` only relies on standard OIDC discovery, client-credentials auth, and the authorization-code flow, all present in much older Keycloak versions too (the `jwks walkthrough` needs 26.7 or later only for its CIMD path). The examples below assume it is reachable at `http://localhost:8080`. Substitute your actual Keycloak URL throughout. * Admin access to create a realm and client. * The MCP server’s public URL as clients will reach it (e.g. `http://localhost:8000`); you will need this for the client’s redirect URI. Refer to the `Keycloak Server Administration Guide` for general Keycloak setup and concepts. ## Step 1: Create a realm Skip this if you are reusing a realm already set up per `IdP setup: Keycloak (jwks)`’s Step 1. 1. Log in to the Keycloak Admin Console (e.g. `http://localhost:8080/admin`). 2. Click the dropdown at the top of the sidebar (initially labeled **Keycloak**, for the default realm) → **Create realm**. 3. Enter a **Realm name** (e.g. `onetick`) and click **Create**. The console switches into the new realm automatically: the sidebar dropdown now shows its name, and every step below happens inside it. ## Step 2: Create a confidential client 1. In your realm, go to **Clients** → **Create client**. 2. Set **Client type** to `OpenID Connect` and enter a **Client ID** (e.g. `onetick-mcp-proxy`). Click **Next**. 3. Enable **Client authentication** (makes this a confidential client). Leave **Standard flow** enabled (Authorization Code). Click **Next**. 4. Under **Valid redirect URIs**, add your resource URL with `/auth/callback` appended; the MCP server always uses this exact path, and there is no setting to change it: ``` http://localhost:8000/auth/callback ``` 5. Under **Web origins**, add the resource URL without a trailing path (e.g. `http://localhost:8000`). 6. Click **Save**, then open the **Credentials** tab and copy the **Client secret**; you will need it in Step 4. ## Step 3: Add an audience mapper Unlike the `jwks walkthrough`, where the mapper lives on a realm-wide default scope, here there is only one client, so the mapper can live directly on it: 1. Open the client from Step 2 and go to its **Client scopes** tab. 2. Click **-dedicated** → **Configure a new mapper** → select **Audience**. 3. Fill in the fields: * **Name**: `onetick-mcp-aud` * **Included Custom Audience**: `onetick-mcp-aud`. This field must be set; leave **Included Client Audience** empty. * Leave **Add to ID token** off and **Add to access token** on. 4. Click **Save**. ## Step 4: Create a test user Skip this if you already have a user in the realm. Otherwise follow `IdP setup: Keycloak (jwks)`’s Step 4. ## Step 5: Configure and verify Add the following to your `.env` file, using the values from the steps above: ``` KX_MCP_AUTH=oidc_proxy # Keycloak realm issuer URL; the discovery document is derived from this KX_MCP_AUTH_ISSUER=http://localhost:8080/realms/onetick # This MCP server's own public URL, as MCP clients connect to it. Must match the # redirect URI registered in Step 2, minus the /auth/callback suffix. KX_MCP_AUTH_RESOURCE_URL=http://localhost:8000 KX_MCP_AUTH_CLIENT_ID=onetick-mcp-proxy KX_MCP_AUTH_CLIENT_SECRET= # The audience value from the mapper in Step 3. Recommended; left unset, the MCP server # does not check the aud claim of the Keycloak token it receives KX_MCP_AUTH_AUDIENCE=onetick-mcp-aud ``` #### NOTE These examples use `localhost` URLs. For a deployment other machines can reach, see `Exposing the MCP server beyond localhost`; the redirect URI registered with the IdP must change too. #### NOTE `KX_MCP_AUTH_RESOURCE_URL` is required in this mode. The MCP server serves `/authorize`, `/token` and `/register` itself under that URL, so it has to be the address clients really connect to (including any proxy in front), or those endpoints won’t resolve. The MCP server also fetches the issuer’s discovery document at startup (10s timeout) and won’t start if it can’t reach it. Restart the MCP server, then connect with an MCP client (e.g. Claude Code; see `MCP Client setup`). Note one behavioral difference from `jwks`: the MCP server shows its own consent screen before redirecting to Keycloak’s login page. That is FastMCP’s `OAuthProxy` base, not a configuration bug, and there is no `KX_MCP_AUTH_*` variable to skip it. After signing in, the MCP server accepts the connection and the tools become available. #### WARNING By default, **every user in the realm can sign in**, and the MCP server does not check who the user is. Restrict access in Keycloak according to your organization’s policy before using the MCP server with real data; see `Controlling who can connect`. ### Common pitfalls Audience left unset : The MCP server starts and logs a warning. It still accepts only tokens it minted itself (see “A token you already hold from Keycloak is rejected” below), so other clients’ Keycloak tokens are not let in. But it no longer checks the `aud` claim of the Keycloak token it receives at login, which removes a safety check. Set `KX_MCP_AUTH_AUDIENCE` (Step 3). Client secret rotation : The MCP server’s OAuth-state store (client registrations, PKCE verifiers, encrypted upstream tokens) is keyed off `KX_MCP_AUTH_CLIENT_SECRET` by default. Rotating the secret re-keys the store, so clients silently re-register, and tokens the MCP server has already issued stop working. Set `KX_MCP_AUTH_JWT_SIGNING_KEY` explicitly if you want secret rotation and the state store to be independent. A token you already hold from Keycloak is rejected : Expected: the bearer a client receives under `oidc_proxy` is minted by the MCP server itself (`iss`/`aud` name the MCP server, not Keycloak); Keycloak’s own token is kept server-side. A token obtained directly from Keycloak by some other means will not be accepted here even though it would pass under `jwks`. If every MCP client you need lets you enter a client ID, prefer the simpler `IdP setup: Keycloak (jwks)` (`jwks`) walkthrough; the MCP server then only validates tokens and holds no client secret. ## ``Client credentials (machine-to-machine, no browser)`` As the pitfall above notes, `oidc_proxy` only accepts bearers it minted itself; a token a service obtains directly from Keycloak via the OAuth2 **client credentials** grant (no browser, no human) is rejected here, same as any other Keycloak-issued token. There is nothing to configure for that case under `oidc_proxy` specifically: point that traffic at a `jwks`-mode server instead and follow `IdP setup: Keycloak (jwks)`’s own “Client credentials” section for the client setup and token request. See `Combining a proxy mode with machine callers` for how to run both alongside each other.