MCP server authentication#

With authentication enabled your MCP client is required to authenticate through your identity provider (IdP), such as Keycloak, Auth0, Okta, or Microsoft Entra ID, and to present the resulting token with every request. When disabled, any party able to reach the MCP server’s address may use its tools.

By default (KX_MCP_AUTH unset), the MCP server accepts every connection without requiring a login. This is appropriate for local development or a network reachable only by your own team. For any other deployment, such as one that is shared or reachable over the internet, set KX_MCP_AUTH to one of the modes below.

Note

The walkthroughs for Keycloak and Microsoft Entra ID are worked examples to get you started, not complete or hardened deployment guides. Settings such as token lifetimes, scopes, redirect URIs and who may sign in are policy decisions for your organization. Review your setup with your security team and your IdP administrator before using it with production data.

Choosing an inbound auth mode#

Start with the type of caller you need to support:

  • For a headless or machine caller with no browser or human present, use jwks. A machine obtains a token directly from the IdP and the MCP server validates it; the proxy modes reject such direct tokens. See Headless / non-interactive clients.

  • For interactive users, start with jwks and pre-registration if every MCP client lets you enter a client ID (see the table below). Otherwise use oidc_proxy for a generic OIDC IdP, or entra for Microsoft Entra ID. These modes broker browser-based login for OAuth-capable MCP clients.

If you need both kinds of caller, see Combining a proxy mode with machine callers before choosing a mode.

In jwks mode the MCP client signs users in with your IdP directly, so the IdP has to recognise the MCP client. The MCP specification describes how an MCP client gets a client ID:

  • Pre-registration: you create a client in your IdP once, and users enter its client ID in their MCP client. Works with any IdP.

  • Client ID Metadata Documents (CIMD): the MCP client’s ID is an HTTPS URL to a document the MCP client hosts, which the IdP fetches at login, so nothing is registered in advance. Newer, so IdP and client support is uneven.

  • Dynamic client registration (DCR): the MCP client registers itself. Older, deprecated in the specification, and often disabled by IdPs.

oidc_proxy and entra avoid the question: you register one app with the IdP, and the MCP server handles client registration for the MCP clients itself.

The tables below say when to use each mode and which of these methods each MCP client supports.

Inbound auth modes

Mode

Use when

jwks

Use when every MCP client you need supports either pre-registration (you enter a client ID) or CIMD. The MCP server only checks tokens, using the signing keys your IdP publishes; the login itself happens between the MCP client and the IdP. It is also the mode for machine callers that use client credentials. With Keycloak, pre-registration works on any version, while CIMD needs 26.7 or later and is still an experimental feature. See the Keycloak walkthrough.

oidc_proxy

Use for interactive, OAuth-capable MCP clients with a generic OIDC IdP (Keycloak, Auth0, Okta), when any client you need supports neither pre-registration nor CIMD. One client application is registered with the IdP in advance, and the MCP server itself handles the login on the MCP client’s behalf. It does not accept tokens issued directly by the IdP; see Combining a proxy mode with machine callers. See IdP setup: Keycloak (oidc_proxy) for a worked example.

entra

Same browser-login pattern as oidc_proxy, specific to Microsoft Entra ID, which has no open DCR. Use jwks instead for Entra machine callers that already hold a token. See IdP setup: Microsoft Entra ID.

static

No IdP involved - the MCP server checks tokens against a public key you supply directly. Useful for tests or self-minted tokens; not recommended for production.

Client registration support

Client

Pre-registration (enter a client ID)

CIMD

Metadata host to allow in your IdP

Claude Code

Yes

Yes (Keycloak 26.7 or later)

claude.ai

Codex

Yes (callback uses 127.0.0.1; see Codex)

Not with Keycloak today (known Keycloak limitations). See Codex

chatgpt.com

Cursor

Yes, via static credentials (see Cursor)

No

n/a

GitHub Copilot

Yes, in VS Code and the CLI (see GitHub Copilot)

Sends a CIMD client ID. See the warning in IdP setup: Keycloak (jwks)

vscode.dev and code.visualstudio.com (VS Code); github.com (CLI)

Junie

Yes, via oauth in mcp.json (undocumented by Junie; see Junie)

Not documented by Junie

n/a

If an interactive client you need supports neither pre-registration nor CIMD, use oidc_proxy (or entra).

This reflects what the MCP client vendors documented when this page was written; client support changes quickly, so check your MCP client’s current documentation.

See Inbound authentication variables for the full variable list.

Configure the MCP server#

The MCP server is configured through KX_MCP_AUTH* variables in your .env file. The example below is for jwks; the settings for the other modes are listed after it, and each IdP walkthrough has a complete .env for its mode. Three terms recur below and in the walkthroughs:

  • Issuer - the URL identifying your IdP (or realm, for Keycloak), taken directly from your IdP’s own configuration.

  • JWKS endpoint - where the IdP publishes the public keys used to verify a token’s signature. jwks mode is the only one where you set this yourself; oidc_proxy discovers it automatically from the issuer, and entra derives it internally.

  • Audience - an identifier that must appear inside the token, ensuring a token issued for a different application is not accepted here in error.

# Inbound modes: unset (off, default) | static | jwks | oidc_proxy | entra
KX_MCP_AUTH=jwks

# Your IdP's issuer URL (also used to validate the token's `iss` claim)
KX_MCP_AUTH_ISSUER=<your-idp-issuer-url>

# Your IdP's JWKS endpoint (required) - from its own OIDC discovery document
KX_MCP_AUTH_JWKS_URI=<your-idp-jwks-url>

# Audience claim the MCP server requires in every access token
KX_MCP_AUTH_AUDIENCE=<your-audience>

# This MCP server's own public URL (its origin, no /mcp suffix; FastMCP appends the mount
# path itself, so including it here doubles up as .../mcp/mcp and breaks client discovery).
# Required for clients to discover the IdP and log themselves in; see the note below.
KX_MCP_AUTH_RESOURCE_URL=http://localhost:8000

# Scopes that must be present in the token. Optional under jwks, so commented out here.
# KX_MCP_AUTH_REQUIRED_SCOPES=<scope>

Note

The URLs above are localhost placeholders. For a deployment other machines can reach, see Exposing the MCP server beyond localhost for what to use in place of KX_MCP_AUTH_ISSUER, KX_MCP_AUTH_JWKS_URI and KX_MCP_AUTH_RESOURCE_URL.

Note

In jwks mode, set KX_MCP_AUTH_RESOURCE_URL so that MCP clients which support discovery (e.g. Claude Code) can find your identity provider and sign the user in through the browser, without any credentials being shared in advance. You can leave it unset if your MCP clients already have a token from somewhere else and the MCP server only needs to validate it. In oidc_proxy and entra modes the MCP server serves /authorize, /token and /register itself under this URL, so it must be the address clients actually reach (including any proxy in front), and the MCP server will not start without it; see IdP setup: Keycloak (oidc_proxy).

Every token’s signature is checked against the public keys published at KX_MCP_AUTH_JWKS_URI, and its iss claim must match KX_MCP_AUTH_ISSUER.

Settings for other modes

  • oidc_proxy and entra need one client app pre-registered with the IdP, and the MCP server uses its credentials to broker the login: KX_MCP_AUTH_CLIENT_ID and KX_MCP_AUTH_CLIENT_SECRET. There is no JWKS endpoint to set; the MCP server finds the signing keys itself.

    • oidc_proxy also takes KX_MCP_AUTH_ISSUER, KX_MCP_AUTH_RESOURCE_URL and KX_MCP_AUTH_AUDIENCE. For an issuer whose discovery document is not at the standard URL, set KX_MCP_AUTH_CONFIG_URL instead of deriving it from the issuer; see IdP setup: Keycloak (oidc_proxy).

    • entra takes KX_MCP_AUTH_TENANT_ID (the Entra tenant GUID) and KX_MCP_AUTH_RESOURCE_URL, and requires KX_MCP_AUTH_REQUIRED_SCOPES; see IdP setup: Microsoft Entra ID.

  • static skips the issuer entirely. Set KX_MCP_AUTH_PUBLIC_KEY (inline PEM) or KX_MCP_AUTH_PUBLIC_KEY_PATH (a path to one) in place of a JWKS endpoint.

Restart the MCP server after changing any of these.

Verify#

Connect with an MCP client (e.g. Claude Code - see MCP Client setup). The MCP client should report that authentication is required and open a browser window to complete the login. In oidc_proxy and entra modes the MCP server shows its own consent screen first. After signing in, the MCP server accepts the connection and the tools become available.

Headless / non-interactive clients#

For a client without OAuth discovery support, or for a headless/non-interactive session (e.g. claude -p, a CI job, or any automation with no human present to complete a browser login), skip the MCP client’s own OAuth flow. This needs the MCP server to run in jwks mode: oidc_proxy and entra reject tokens obtained directly from the IdP (see Combining a proxy mode with machine callers).

  1. Get a token with an OAuth2 client-credentials grant against your IdP. This uses a separate confidential client with its own secret, not the interactive client that people log in with. The steps are IdP-specific: Keycloak or Entra.

  2. Give the token to your MCP client as a static bearer. The mechanism varies by client:

    For any other MCP client, see its page under MCP Client setup or its own documentation.

The token’s lifetime is set by your IdP, so agree an appropriate value with your IdP admin (in Keycloak it is the access token lifespan, which can be set per client; the realm default is five minutes). A static bearer doesn’t refresh itself, so request a new token and update the MCP client before it expires.

If the MCP server sets KX_MCP_AUTH_REQUIRED_SCOPES, the token must carry those scopes.

See Inbound authentication variables for the full list of KX_MCP_AUTH* variables.

Combining a proxy mode with machine callers#

oidc_proxy and entra only accept bearers the MCP server minted itself while brokering a browser login. A token a service obtains directly from the IdP (e.g. via an OAuth2 client credentials grant, with no browser or human involved) is rejected, same as any other IdP-issued token. KX_MCP_AUTH also selects one mode per server process, so a single server running oidc_proxy or entra can’t broker interactive logins and accept those direct tokens at the same time.

If you need to support both human and machine callers, either:

  • Run one MCP server in jwks mode for both. Interactive users sign in through pre-registration or CIMD (see the client support table), and machine callers use client credentials. jwks validates any well-formed bearer regardless of how it was obtained, interactive or not.

  • Or run two server instances (or ports): one in oidc_proxy/entra mode for browser-driven clients, one in jwks mode for machine clients, pointed at the same backend.

See IdP setup: Keycloak (oidc_proxy) or IdP setup: Microsoft Entra ID for the provider-specific steps to register a machine caller.

Controlling who can connect#

The MCP server checks that a token is valid for this MCP server (signature, issuer, audience, expiry, and any required scopes). It does not check who the user is, so anyone your IdP will issue such a token to can connect. By default that is everyone who can sign in to the IdP’s realm or tenant. Who should have access is a decision for your organization: agree it with your security team and IdP administrator, and enforce it in the IdP.

Exposing the MCP server beyond localhost#

The walkthroughs use localhost URLs. When other machines will connect, replace them as follows. Use https throughout: Keycloak requires it for non-local requests by default, and Entra only accepts http redirect URIs for localhost.

Setting

Public value

KX_MCP_AUTH_RESOURCE_URL (jwks, oidc_proxy, entra)

Your MCP server’s public URL exactly as clients reach it, including any reverse proxy, with no /mcp suffix (e.g. https://mcp.example.com). It is optional in jwks when clients already hold a token, and required in the two proxy modes.

KX_MCP_AUTH_ISSUER (jwks, oidc_proxy)

Your IdP’s public issuer URL (e.g. https://keycloak.example.com/realms/onetick). It must match the iss claim in the tokens, so also set Keycloak’s own hostname setting (KC_HOSTNAME) to its public URL; otherwise tokens still carry the localhost issuer.

KX_MCP_AUTH_JWKS_URI (jwks)

A URL the MCP server can reach. It may be an internal address, but the issuer above must stay the public one.

Redirect URI registered with the IdP (oidc_proxy, entra)

<KX_MCP_AUTH_RESOURCE_URL>/auth/callback, exactly. For Keycloak also update Web origins to the resource URL.

Entra issuer and JWKS (entra)

No change; they are Microsoft’s endpoints.

For jwks with CIMD, the client metadata hosts you allow in Keycloak (such as claude.ai) are the MCP client vendors’, not yours, and the localhost and 127.0.0.1 entries stay: desktop and CLI clients still redirect to their own machine. Keycloak also needs outbound HTTPS to those hosts.

Common pitfalls#

KX_MCP_AUTH_RESOURCE_URL must match the URL clients actually connect to

If it differs from the client-facing address (for example because the MCP server sits behind a reverse proxy and this is set to the internal bind address instead of the public one), discovery advertises the wrong resource and clients cannot find the IdP to log in.

KX_MCP_AUTH_RESOURCE_URL must not include /mcp

Set it to the bare origin (e.g. http://localhost:8000), not the full MCP endpoint URL. FastMCP appends the mount path itself when building the advertised resource; including /mcp here doubles it into .../mcp/mcp, which fails the MCP client’s resource check with an error like Protected resource http://localhost:8000/mcp/mcp does not match expected http://localhost:8000/mcp (or origin). If you fix this after an MCP client has already attempted (and failed) to connect, remove and re-add the MCP server (claude mcp remove / claude mcp add); some MCP clients cache OAuth/discovery state from the failed attempt and will not pick up the corrected metadata on a plain retry.

client_not_found from Keycloak, with a URL as the client ID

The MCP client sent a metadata-document URL (for example https://vscode.dev/oauth/client-metadata.json) as its client ID, because the IdP advertises CIMD, and Keycloak has no client for it. The client ID you configured is being skipped. Either allow that MCP client’s metadata host in the CIMD client policy (see IdP setup: Keycloak (jwks)), or start Keycloak without the cimd feature so the MCP client uses your pre-registered ID. The Keycloak log shows the ID that was sent: look for LOGIN_ERROR with error="client_not_found".

Invalid parameter: redirect_uri from Keycloak

The redirect URI the MCP client sent is not registered on the Keycloak client. Keycloak compares exactly, so check the host (localhost vs 127.0.0.1), the port, the path and any trailing slash. Read the redirect_uri parameter in the browser’s address bar when the error appears, and add that exact value to Valid redirect URIs. Each MCP client’s URI is listed in the redirect URI table. If the port changes on every attempt, the client is not using a fixed callback port; set one as described on its client page.

Browser cannot connect to 127.0.0.1:<port> after logging in

Keycloak accepted the login but nothing is listening on the MCP client’s callback port, so the redirect has nowhere to go. The MCP client opens that listener only while its login command runs, and only on the port it is configured to use. Check the MCP client’s callback-port setting is in the right place in its config (see its client page), and keep the login command running until it reports success.

invalid_scope from Keycloak

The MCP client asked for scopes the Keycloak client is not assigned, often because it requested every scope the realm lists. Restrict the scopes the MCP client requests (for example openid); the audience scope is a default scope, so it is still applied. See the MCP client’s page.

Token rejected with 401 invalid_token

The response does not say why. The cause is one of: the aud claim does not match KX_MCP_AUTH_AUDIENCE, the iss claim does not match KX_MCP_AUTH_ISSUER (localhost and 127.0.0.1 differ), the token is expired, the signing key is not in the JWKS the MCP server fetched, or a scope in KX_MCP_AUTH_REQUIRED_SCOPES is missing from the token’s scope claim. Decode the token (kx auth introspect <token>) and compare aud, iss, exp and scope against the MCP server’s settings. Some MCP clients treat a 401 as “start the login flow”, so a rejected static token can surface as a dynamic client registration (DCR) error instead.

Startup validation

The MCP server checks the KX_MCP_AUTH* variables for the selected mode at startup and refuses to start if one it needs is missing. In jwks mode that is KX_MCP_AUTH_JWKS_URI, plus KX_MCP_AUTH_ISSUER whenever KX_MCP_AUTH_RESOURCE_URL is set; for oidc_proxy and entra, see Configure the MCP server. If the MCP server exits immediately after you enable authentication, the startup log names the missing variable.

For a full worked example of setting up an identity provider for the jwks mode above, see IdP setup: Keycloak (jwks).