# IdP setup: Keycloak (jwks) The steps below walk through configuring Keycloak so it can issue and validate tokens for the MCP server, for the `jwks` inbound auth mode; see `MCP server authentication`. There are two ways to tell Keycloak about the MCP client. Steps 1, 2, 4 and 5 are common to both; only Step 3 differs. | Path | How the client gets a `client_id` | Requirements | |------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------| | **Pre-registration** (recommended) | You create one public client in Keycloak and enter its client ID in each MCP client. | Any Keycloak version. The MCP client must let you supply a client ID (see `the client support table`). | | **CIMD** | The client identifies itself with an HTTPS metadata URL and Keycloak creates the client at login. Nothing to enter in the MCP client. | Keycloak 26.7 or later with the experimental `cimd` feature, and an MCP client that supports CIMD. | #### NOTE `jwks` needs every MCP client you use to support pre-registration or CIMD; see `the client support table`. If one supports neither, use `oidc_proxy` instead: see `IdP setup: Keycloak (oidc_proxy)`, and `choosing an inbound auth mode` for the comparison. For Microsoft Entra ID, see `IdP setup: Microsoft Entra ID`. Prerequisites: * A running Keycloak instance. The examples below assume it is reachable at `http://localhost:8080`. Substitute your actual Keycloak URL throughout. * Admin access to create a realm, client scopes and clients (and, for CIMD, client policies). * CIMD path only: Keycloak 26.7 or later started with the `cimd` feature enabled (`--features=cimd` or `KC_FEATURES=cimd`), and outbound HTTPS from Keycloak to the host serving your MCP client’s metadata document (the host in the client’s `client_id` URL), since Keycloak fetches the document at login time. Refer to the `Keycloak Server Administration Guide` for general Keycloak setup and concepts. ## Step 1: Create a realm 1. Log in to the Keycloak Admin Console (e.g. `http://localhost:8080/admin`). 2. In the sidebar go to **Manage realms** → **Create realm**. 3. Enter a **Realm name** (e.g. `onetick`) and click **Create**. The console switches into the new realm automatically, and every step below happens inside it. ## Step 2: Add an audience mapper The MCP server validates the `aud` claim in every access token, so the tokens Keycloak issues must carry the audience the MCP server expects. The audience mapper goes on a **default client scope** rather than on an individual client: Keycloak adds a realm default scope to every client created after this step, which covers the pre-registered client from Step 3A and any client Keycloak creates from a metadata document in Step 3B (there is no fixed client to attach a mapper to in that case). 1. In the sidebar go to **Client scopes** → **Create client scope**. 2. **Name**: `onetick-mcp-audience`. **Protocol**: `OpenID Connect`. Leave the rest default. 3. Click **Save**, open the new scope’s **Mappers** tab, and click **Configure a new mapper** (not **Add predefined mapper**; the predefined ones do not let you set a custom static audience value). Select **Audience** from the list. 4. 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. 5. Click **Save**, then go back to the top-level **Client scopes** list. Find `onetick-mcp-audience` and change its **Assigned type** to **Default** so every client picks it up. #### WARNING A realm default scope also reaches clients created later for other applications in the same realm, and their tokens then carry an audience the MCP server accepts. Use a realm dedicated to the MCP server. If you only use pre-registration (Step 3A), you can instead leave the scope’s **Assigned type** as **None** and add it as a **Default** scope to just the clients that need it: `onetick-mcp`, and `onetick-mcp-service` if you add machine callers. Do this from each client’s **Client scopes** tab → **Add client scope**. ## ``Step 3A: Pre-register the MCP client (recommended)`` Pre-registration is the other client registration option in the `MCP specification`: the client’s ID is created in advance in the IdP and the user supplies it to the MCP client. It needs no experimental Keycloak feature, no client policies and no outbound access from Keycloak, and it works on any Keycloak version. The cost is that each MCP client must be given the client ID once. Create **one public client** and share its client ID with your users. It is public (no secret) and uses PKCE, the same as a client created from a metadata document would be. 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`). Click **Next**. 3. Under **Capability config**, leave **Client authentication** off (public client), enable **Standard flow** and leave the other flows off. Turn on **Require PKCE** (method `S256`) so Keycloak rejects any login that does not use it; MCP clients always do. Click **Next**. (Keycloak versions before 26.6 have no PKCE option on this page; after saving, set **Advanced** tab → **Proof Key for Code Exchange Code Challenge Method** to `S256` instead.) 4. Under **Login settings**, add **Valid redirect URIs**. The callback URL depends on the MCP client, so add one entry per client you want to support; the one Keycloak client accepts them all. For Claude Code with the callback port pinned to `8765`, add `http://localhost:8765/callback`. Click **Save**. 5. Open the **Client scopes** tab and confirm `onetick-mcp-audience` is listed as an assigned scope. It is added automatically because Step 2 made it a default scope. **The callback URL is client dependent.** Each MCP client uses its own, so a single URI will not work for all of them. A Keycloak client can have several redirect URIs, so one client can serve every MCP client you support: add one URI per client, and Keycloak accepts a login if the client’s redirect URI matches any entry. Keycloak compares them exactly, including the host (`localhost` and `127.0.0.1` differ), the path and any trailing slash. Use a fixed port where the client lets you pin one, and register that exact URI rather than a wildcard; do not register broad patterns such as `*`. | Client | Redirect URI to register | How to fix the port | |---------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------| | Claude Code | `http://localhost:8765/callback` | `--callback-port 8765` on `claude mcp add`. See `Claude Code`. | | Codex | Usually `http://127.0.0.1:8765/callback`; register the exact callback Codex reports, whichmay have a server-specific suffix depending on the issuer’s metadata. | `mcp_oauth_callback_port = 8765` at the top level of `config.toml`. See `Codex`. | | Cursor (desktop and CLI) | `http://localhost:8787/callback`, and for older installs `cursor://anysphere.cursor-mcp/oauth/callback` | Fixed by Cursor. See `Cursor`. | | GitHub Copilot CLI | `http://127.0.0.1:8765/` (root path, trailing slash, no `/callback`) | `oauth.callbackPort` in `mcp-config.json`. See `GitHub Copilot`. | | GitHub Copilot in VS Code | `http://127.0.0.1:33418/` (with the trailing slash) and `https://vscode.dev/redirect` | Fixed by VS Code. | | Junie | `http://localhost:62345/callback` through `http://localhost:62364/callback` (20 URIs) | Not configurable: Junie uses the first free port in that range. See `Junie`. | The port `8765` is an arbitrary choice used in every example in this guide; any free port works as long as the client is configured with the same port you register. Use a different port per client if two might log in at the same time. To see what a client actually sends, read the `redirect_uri` parameter in the browser’s address bar when its login fails; see `the troubleshooting entry`. Give your users the client ID (`onetick-mcp` in the example). They enter it when adding the MCP server in their MCP client; the client pages under `MCP Client setup` show where. No client secret is needed. For Claude Code: ``` claude mcp add --transport http --client-id onetick-mcp --callback-port 8765 \ OneTick-Cloud http://localhost:8000/mcp ``` #### NOTE Pre-registration only works with MCP clients that let you supply a client ID instead of registering one themselves. A client that offers no such setting falls back to DCR, which fails against this realm. See `the client support table`, and use the `oidc_proxy walkthrough` for any client that supports neither pre-registration nor CIMD. #### WARNING If your IdP advertises CIMD support (Keycloak does when started with the `cimd` feature), clients that support CIMD use it **instead of** the client ID you give them, and the login fails with `client_not_found` (see `the troubleshooting entry`). VS Code and the Copilot CLI behave this way. Either allow those clients’ metadata hosts as in Step 3B, or start Keycloak without the `cimd` feature so pre-registration is used. ## ``Step 3B: Allow metadata-document clients (CIMD)`` Use this instead of Step 3A when your Keycloak is 26.7 or later and every MCP client you need supports CIMD. The client’s `client_id` is an HTTPS URL pointing at a JSON document the client hosts (name, redirect URIs). On first login, Keycloak fetches that document and creates a public client from it, so nothing is registered up front and users enter no client ID. Keycloak only accepts a URL as a `client_id` when a client policy applies the `client-id-metadata-document` executor to it. You create a profile holding the executor, then a policy that selects which client IDs it applies to. Restricting the policy to the hosts of the MCP clients you actually use is what stops an arbitrary web page from registering itself as a client. Both JSON blocks below contain a `` placeholder. Replace it with the host of the metadata document URL your MCP client uses as its `client_id`; your client’s documentation gives the URL. List one host per allowed client. For example: * Claude Code uses `https://claude.ai/oauth/claude-code-client-metadata`, so the host is `claude.ai`. * To allow several clients, list each host: `["claude.ai", "app.example.com"]`. * VS Code uses `https://vscode.dev/oauth/client-metadata.json`, so allow `vscode.dev`. Its metadata document also has a `logo_uri` on `code.visualstudio.com`, which the profile’s list needs too. * The Copilot CLI uses `https://github.com/copilot/cli/client-metadata.json`, so allow `github.com`. The profile’s `cimd-allow-permitted-domains` list also needs `localhost` and `127.0.0.1`, as in the example below. Keycloak checks the redirect URIs in the client’s metadata document against this list too, and desktop and CLI clients redirect to a loopback address. Leaving them out makes the login fail with `Invalid Client ID: domain not allowed`. The policy’s `client-id-uri-allow-permitted-domains` list takes only the client hosts. See `the client support table` for which clients support CIMD. 1. Go to **Realm settings** → **Client policies** → **Profiles** → **Create client profile**. Name it `mcp-cimd-profile`, save it, then open the profile’s **JSON editor** tab and replace its contents with: ``` { "name": "mcp-cimd-profile", "description": "Accepts MCP clients as public PKCE clients identified by their metadata URL.", "executors": [ { "executor": "client-id-metadata-document", "configuration": { "cimd-allow-http-scheme": false, "cimd-allow-permitted-domains": ["", "localhost", "127.0.0.1"], "cimd-restrict-same-domain": false, "cimd-required-properties": [], "only-allow-confidential-client": false } } ] } ``` 2. Go to the **Policies** tab → **Create client policy**. Name it `mcp-cimd-policy`, save it, then open its **JSON editor** tab and replace its contents with: ``` { "name": "mcp-cimd-policy", "description": "Runs the CIMD executor only for HTTPS client IDs on trusted MCP client hosts.", "enabled": true, "conditions": [ { "condition": "client-id-uri", "configuration": { "is-negative-logic": false, "client-id-uri-scheme": ["https"], "client-id-uri-allow-permitted-domains": [""] } } ], "profiles": ["mcp-cimd-profile"] } ``` No further client registration setup is needed. Anonymous DCR can stay disabled in the realm; Keycloak rejects it by default (its Trusted Hosts policy has no allowed hosts), and the flow here does not use it. #### NOTE An MCP client only uses CIMD if it supports it. Clients that do not will fall back to DCR, which fails against a realm configured as above. If a client you need does not support CIMD, use the `oidc_proxy walkthrough` instead, where you register one client with Keycloak yourself and the MCP server presents it to MCP clients. #### WARNING Keycloak’s CIMD support is experimental and has known gaps: * Keycloak rejects a metadata document that contains properties it does not recognise, such as `token_endpoint_auth_methods_supported`. Codex’s document contains it, so Codex cannot log in, including on 26.8.0. The issue is still open; see `keycloak/keycloak#51236`. * Keycloak does not enforce PKCE for clients it creates from a metadata document, so a login to such a client can complete without it. Adding a `pkce-enforcer` executor to the profile above has no effect. The issue is still open as of 26.7.3, and describes a workaround; see `keycloak/keycloak#52795`. Pre-registration (Step 3A) does not have this gap. Check the `client support table` before choosing this mode, and re-check it after you upgrade Keycloak. ## Step 4: Create a test user If no users exist in the realm yet, create one to verify the login flow: 1. In the sidebar go to **Users** → **Add user**. 2. Fill in the fields: * **Username**: e.g. `testuser` * **Email**: e.g. `testuser@example.com` * **First name** and **Last name**: e.g. `Test` and `User`. Recent Keycloak versions require both, and a user created without them is stopped at an **Update Account Information** page on first sign-in. * Enable **Email verified**. 3. Click **Create**. 4. Open the **Credentials** tab → **Set password**. 5. Enter a password, disable **Temporary**, and click **Save**. ## Step 5: Configure and verify Using the values from the steps above, fill in the `KX_MCP_AUTH*` variables from the `Configure the MCP server` section on the previous page, then restart the MCP server. For this walkthrough’s example realm/audience, that’s: ``` KX_MCP_AUTH=jwks KX_MCP_AUTH_ISSUER=http://localhost:8080/realms/onetick KX_MCP_AUTH_JWKS_URI=http://localhost:8080/realms/onetick/protocol/openid-connect/certs KX_MCP_AUTH_AUDIENCE=onetick-mcp-aud KX_MCP_AUTH_RESOURCE_URL=http://localhost:8000 ``` #### NOTE These examples use `localhost` URLs. For a deployment other machines can reach, replace `KX_MCP_AUTH_ISSUER`, `KX_MCP_AUTH_JWKS_URI` and `KX_MCP_AUTH_RESOURCE_URL` with your public URLs, and set Keycloak’s own hostname to match; see `Exposing the MCP server beyond localhost`. There is no `KX_MCP_AUTH_CLIENT_ID`/`_CLIENT_SECRET` to set here under either path: in `jwks` mode the MCP server only validates tokens, and the client ID is something the MCP client presents to Keycloak, not to the MCP server. Connect with your MCP client (see `MCP Client setup`). With pre-registration, supply the client ID from Step 3A when adding the MCP server. With CIMD, no extra setting is needed, and the client must support CIMD. The client should report that authentication is required and open a browser window to complete the login. After signing in, the MCP server accepts the connection and the tools become available. To confirm CIMD was used, go to **Clients** in the admin console. After the first login there is a public client whose **Client ID** is the client’s metadata URL. With pre-registration, the login instead appears under the `onetick-mcp` client’s **Sessions** tab. #### NOTE Set `KX_MCP_AUTH_ISSUER` to the full realm URL including the `/realms/` path, not just the Keycloak base URL (e.g. `http://localhost:8080/realms/onetick`, not `http://localhost:8080`). #### 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`. ## ``Client credentials (machine-to-machine, no browser)`` Everything above brokers an **interactive** login: a human in a browser. For a caller with no human present (a CI job, a scheduled task, another service acting on its own behalf), use the OAuth2 **client credentials** grant instead. Unlike Entra (see `IdP setup: Microsoft Entra ID`’s equivalent section), this needs no separate auth mode. The page above already configures `jwks`, which validates any well-formed bearer regardless of how it was obtained, so a client-credentials token works against the same server, with the same `KX_MCP_AUTH*` config and no changes needed. 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-service`). Click **Next**. 3. Under **Capability config**, enable **Client authentication** (makes this a confidential client) and **Service accounts roles** (this is what turns on the client-credentials grant). Leave **Standard flow** off; no browser redirect is involved. Click **Next**. 4. On the **Login settings** page (Root URL, Home URL, redirect URIs, Web origins), leave everything blank; these only matter for browser-based flows, none of which are enabled here. Click **Save**. 5. Open the **Credentials** tab and copy the **Client secret**. 6. The service account client inherits every client scope assigned type **Default** in the realm, so the `onetick-mcp-audience` scope you created in `Step 2: Add an audience mapper` applies automatically; no extra mapper step is needed. That step is the only one from the interactive setup that this path depends on. 7. The caller requests a token directly from Keycloak, with no browser involved: ``` curl -s -X POST \ "http://localhost:8080/realms/onetick/protocol/openid-connect/token" \ -d "grant_type=client_credentials" \ -d "client_id=onetick-mcp-service" \ -d "client_secret=" ``` 8. The caller sends the resulting `access_token` as a normal bearer to the MCP server: no discovery, no redirect, no consent screen. See `Headless / non-interactive clients` for how to hand this to your MCP client. #### NOTE The token expires after the access token lifespan, five minutes by default, and the client-credentials grant returns no refresh token, so the caller requests a new one when it expires. To change the lifespan, open **Clients** → `onetick-mcp-service` → **Advanced** tab → **Advanced settings** → **Access Token Lifespan**. This overrides the realm value, so interactive users keep the short default. The realm-wide setting is under **Realm settings** → **Tokens**.