oauthKeycloakProvider() options, defaults, and errors.
Prepare the Keycloak realm
You need Keycloak 26.4 or later, a reachable realm, and admin access. Keycloak 26.4 added the RFC 8414 authorization-server metadata that MCP clients need for this flow. For local setup, start with the Keycloak getting started guides. Enable Dynamic Client Registration for the hosts your MCP clients use:- Go to Realm settings > Client registration > Anonymous Access Policies.
- Open Trusted Hosts.
- Add local client hosts such as
localhostand127.0.0.1. - Keep Client URIs Must Match enabled.
http://localhost:6274 for local inspector workflows.
For production, avoid anonymous DCR only after you have confirmed your MCP clients can send a Keycloak Initial Access Token during Dynamic Client Registration. The mcp-use server provider only configures token verification; the client performs registration directly with Keycloak.
Set environment variables
aud claim through an audience mapper.
aud value, valid-looking tokens will be rejected.
Configure the MCP server
Use Keycloak roles in tools
The provider maps Keycloak realm roles ontoctx.auth.user.roles. It maps resource roles onto ctx.auth.user.permissions as client:role strings.
Verify the setup
Run the server and connect with an OAuth-capable MCP client.- The client discovers Keycloak OAuth metadata.
- The client registers with Keycloak.
- Authenticated tool calls include
ctx.auth.user.userId. - Role-protected tools reject users without the required role.
Production checks
Before deploying:- Serve Keycloak and the MCP server over HTTPS.
- Use Initial Access Tokens or stricter registration policies only with clients that can pass the registration token to Keycloak.
- Add an audience mapper before setting
MCP_USE_OAUTH_KEYCLOAK_AUDIENCE. - Decide whether tools should check realm roles, resource roles, or both.
Next steps
Runnable Keycloak example
Compare your setup with a working mcp-use Keycloak server.
Keycloak Client Registration
Review Keycloak Dynamic Client Registration.
User Context
Use Keycloak roles and permissions inside tools.
Keycloak provider API reference
Look up exact provider options and defaults.