Skip to main content
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

Access: http://localhost:8080/admin (admin/admin)
2

Run Setup Script

This script creates:
  • 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

  1. Open Keycloak admin console
  2. Hover over realm dropdown (top left)
  3. Click “Create Realm”
  4. Name: mcp-server-langgraph
  5. Click “Create”

2. Create Client

  1. Go to ClientsCreate client
  2. Configure:
    • Client ID: langgraph-client
    • Client type: OpenID Connect
    • Client authentication: ON (confidential)
  3. Click “Next”
  4. Configure access:
    • Standard flow: ON (authorization code)
    • Direct access grants: ON (for testing)
    • Service accounts: OFF
  5. Click “Next”
  6. Set redirect URIs:
    • Valid redirect URIs: https://yourdomain.com/*
    • Web origins: https://yourdomain.com
  7. Click “Save”

3. Get Client Secret

  1. Go to Clientslanggraph-client
  2. Click Credentials tab
  3. Copy Client secret
  4. Save to environment variables

4. Configure Client Scopes

  1. Go to Clientslanggraph-clientClient scopes
  2. Add scopes:
    • openid (required)
    • profile (user info)
    • email (email address)
    • roles (role mapping)
  3. Click Add dedicated scope → Create roles scope if missing

5. Create Test User

  1. Go to UsersAdd user
  2. Enter username: alice
  3. Click “Create”
  4. Go to Credentials tab
  5. Click Set password
  6. Enter password, set Temporary: OFF
  7. Click “Save”

6. Assign Roles

  1. Go to Users → Select alice
  2. Click Role mapping tab
  3. Assign roles (these map to OpenFGA):
    • admin
    • user
    • viewer

Token Flow

For web applications:

Direct Grant Flow (Testing Only)

For testing or CLI applications:
Never use Direct Grant in production! Use Authorization Code flow with PKCE for web/mobile apps.

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

Edit config/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:
Configure app:

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_active

Troubleshooting

Check:
  • KEYCLOAK_SERVER_URL matches Keycloak issuer
  • Realm name is correct
  • Token not expired
  • JWKS endpoint accessible
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!