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
jwksand pre-registration if every MCP client lets you enter a client ID (see the table below). Otherwise useoidc_proxyfor a generic OIDC IdP, orentrafor 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 |
|---|---|
|
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. |
|
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. |
|
Same browser-login pattern as |
|
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) |
|
Codex |
Yes (callback uses |
Not with Keycloak today (known Keycloak limitations). See Codex |
|
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) |
|
Junie |
Yes, via |
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.
jwksmode is the only one where you set this yourself;oidc_proxydiscovers it automatically from the issuer, andentraderives 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_proxyandentraneed one client app pre-registered with the IdP, and the MCP server uses its credentials to broker the login:KX_MCP_AUTH_CLIENT_IDandKX_MCP_AUTH_CLIENT_SECRET. There is no JWKS endpoint to set; the MCP server finds the signing keys itself.oidc_proxyalso takesKX_MCP_AUTH_ISSUER,KX_MCP_AUTH_RESOURCE_URLandKX_MCP_AUTH_AUDIENCE. For an issuer whose discovery document is not at the standard URL, setKX_MCP_AUTH_CONFIG_URLinstead of deriving it from the issuer; see IdP setup: Keycloak (oidc_proxy).entratakesKX_MCP_AUTH_TENANT_ID(the Entra tenant GUID) andKX_MCP_AUTH_RESOURCE_URL, and requiresKX_MCP_AUTH_REQUIRED_SCOPES; see IdP setup: Microsoft Entra ID.
staticskips the issuer entirely. SetKX_MCP_AUTH_PUBLIC_KEY(inline PEM) orKX_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).
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.
Give the token to your MCP client as a static bearer. The mechanism varies by client:
Claude Code: the
--headeroption; see Authentication.Codex: the
bearer_token_env_varsetting; see Authentication.GitHub Copilot: the
--headeroption; see Authentication.Cursor and Junie: a
headersentry inmcp.json; see Authentication and Authentication.
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
jwksmode for both. Interactive users sign in through pre-registration or CIMD (see the client support table), and machine callers use client credentials.jwksvalidates any well-formed bearer regardless of how it was obtained, interactive or not.Or run two server instances (or ports): one in
oidc_proxy/entramode for browser-driven clients, one injwksmode 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 |
|---|---|
|
Your MCP server’s public URL exactly as clients reach it, including any reverse proxy, with no
|
|
Your IdP’s public issuer URL (e.g. |
|
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 ( |
|
Entra issuer and JWKS ( |
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_URLmust match the URL clients actually connect toIf 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_URLmust not include/mcpSet 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/mcphere doubles it into.../mcp/mcp, which fails the MCP client’s resource check with an error likeProtected 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_foundfrom Keycloak, with a URL as the client IDThe 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 thecimdfeature so the MCP client uses your pre-registered ID. The Keycloak log shows the ID that was sent: look forLOGIN_ERRORwitherror="client_not_found".
Invalid parameter: redirect_urifrom KeycloakThe redirect URI the MCP client sent is not registered on the Keycloak client. Keycloak compares exactly, so check the host (
localhostvs127.0.0.1), the port, the path and any trailing slash. Read theredirect_uriparameter 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_scopefrom KeycloakThe 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
audclaim does not matchKX_MCP_AUTH_AUDIENCE, theissclaim does not matchKX_MCP_AUTH_ISSUER(localhostand127.0.0.1differ), the token is expired, the signing key is not in the JWKS the MCP server fetched, or a scope inKX_MCP_AUTH_REQUIRED_SCOPESis missing from the token’sscopeclaim. Decode the token (kx auth introspect <token>) and compareaud,iss,expandscopeagainst 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. Injwksmode that isKX_MCP_AUTH_JWKS_URI, plusKX_MCP_AUTH_ISSUERwheneverKX_MCP_AUTH_RESOURCE_URLis set; foroidc_proxyandentra, 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).