NEW in v2.1.0 - Complete guide to integrating Keycloak SSO with your MCP server for production-ready authentication.
Overview
Keycloak provides enterprise-grade authentication with:- OpenID Connect / OAuth2 - Standards-compliant authentication
- Single Sign-On (SSO) - One login for all applications
- User Management - Centralized identity and access management
- Social Login - Google, GitHub, Facebook, and more
- Multi-Factor Authentication - TOTP, WebAuthn, SMS
- Role & Group Management - Hierarchical organization
Architecture
Quick Start
1
Deploy Keycloak
- Docker Compose
- Kubernetes
- Cloud Run
2
Run Setup Script
- Realm:
mcp-server-langgraph - Client:
langgraph-client(confidential, OIDC) - Scopes:
openid,profile,email,roles - Redirect URIs: Configured for your domain
3
Configure Environment
4
Test Authentication
Manual Configuration
If you prefer to configure Keycloak manually via the admin console:1. Create Realm
- Open Keycloak admin console
- Hover over realm dropdown (top left)
- Click “Create Realm”
- Name:
mcp-server-langgraph - Click “Create”
2. Create Client
- Go to Clients → Create client
- Configure:
- Client ID:
langgraph-client - Client type: OpenID Connect
- Client authentication: ON (confidential)
- Client ID:
- Click “Next”
- Configure access:
- Standard flow: ON (authorization code)
- Direct access grants: ON (for testing)
- Service accounts: OFF
- Click “Next”
- Set redirect URIs:
- Valid redirect URIs:
https://yourdomain.com/* - Web origins:
https://yourdomain.com
- Valid redirect URIs:
- Click “Save”
3. Get Client Secret
- Go to Clients →
langgraph-client - Click Credentials tab
- Copy Client secret
- Save to environment variables
4. Configure Client Scopes
- Go to Clients →
langgraph-client→ Client scopes - Add scopes:
openid(required)profile(user info)email(email address)roles(role mapping)
- Click Add dedicated scope → Create
rolesscope if missing
5. Create Test User
- Go to Users → Add user
- Enter username:
alice - Click “Create”
- Go to Credentials tab
- Click Set password
- Enter password, set Temporary: OFF
- Click “Save”
6. Assign Roles
- Go to Users → Select
alice - Click Role mapping tab
- Assign roles (these map to OpenFGA):
adminuserviewer
Token Flow
Authorization Code Flow (Recommended)
For web applications:Direct Grant Flow (Testing Only)
For testing or CLI applications:Token Refresh
Refresh tokens have longer lifetime and can be used to get new access tokens:Role Mapping to OpenFGA
NEW v2.1.0: Automatic synchronization of Keycloak roles/groups to OpenFGA.Configuration
Editconfig/role_mappings.yaml:
Automatic Sync
When user authenticates, roles are synced to OpenFGA:Manual Sync
Trigger sync for existing users:Production Deployment
High Availability
Deploy Keycloak with 2+ replicas:SSL/TLS
Always use HTTPS in production:Performance Tuning
Backup & Recovery
Monitoring
Health Checks
Metrics
Keycloak exposes Prometheus metrics:Login rate
rate(keycloak_logins_total[5m])Failed logins
rate(keycloak_login_failures_total[5m])Token creation rate
rate(keycloak_tokens_created_total[5m])Active sessions
keycloak_sessions_activeTroubleshooting
Connection refused
Connection refused
Invalid client secret
Invalid client secret
Token verification failed
Token verification failed
Check:
- KEYCLOAK_SERVER_URL matches Keycloak issuer
- Realm name is correct
- Token not expired
- JWKS endpoint accessible
SSL verification errors
SSL verification errors
Development only:Production: Fix SSL certificate or add CA cert
Next Steps
Session Management
Setup Redis session store
Authentication Guide
Learn about auth modes
Authorization
Configure OpenFGA permissions
Production Checklist
Pre-deployment verification
Production Ready: Keycloak provides enterprise-grade SSO for your MCP server!