MCP server for KeyCloak Admin REST API via Service Account
Project description
keycloak-mcp
English | 日本語
An MCP (Model Context Protocol) server for the KeyCloak Admin REST API.
Authenticates via a Service Account (Client Credentials Grant), so no human password or TOTP is involved. Also Infinispan-safe — it never creates user sessions and never hits the userinfo endpoint.
Features
Users
| Tool | Description |
|---|---|
count_users |
Total user count in the realm |
search_users |
Partial-match search (username / email / name) |
get_user |
Full detail for an exact username |
reset_password |
Reset one user's password |
reset_passwords_batch |
Bulk reset from CSV (username,password per line; blank password is generated) |
get_user_sessions |
Active sessions for one user, timestamps in local time |
logout_user |
Kill all active sessions for one user |
Groups
| Tool | Description |
|---|---|
list_user_groups |
Which groups a user belongs to |
list_users_by_group |
Members of a group |
Security
| Tool | Description |
|---|---|
get_brute_force_status |
Whether a user is currently locked by brute-force detection |
get_login_failures_by_ip |
Failure breakdown by source IP (site-labeled when KEYCLOAK_SITES_INI is set) |
detect_login_loops |
Flag users who logged in too many times in a short window (redirect loops) |
Events
| Tool | Description |
|---|---|
get_events |
Filter by type, username, client, IP, and date range. Username is resolved to user ID internally. Failure events include KeyCloak's error field (e.g. invalid_user_credentials). |
get_login_stats |
Login success/failure totals, paginated across all results |
get_login_stats_by_hour |
Logins bucketed by hour of day (local time) |
get_login_stats_by_client |
Logins bucketed by client / SP |
get_password_update_events |
UPDATE_PASSWORD history |
Admin Events
get_events only sees user events. Actions driven by an admin — or by a service account writing custom attributes — don't show up there. The admin-event endpoint fills that gap.
| Tool | Description |
|---|---|
get_admin_events |
Filter by operation (CREATE / UPDATE / DELETE / ACTION), resource type (USER / CLIENT / ROLE / GROUP / …), resource path, and date range |
get_user_attribute_history |
UPDATE/ACTION events scoped to one user — handy for tracking when a custom attribute (e.g. temp_password) was written by an automated pipeline |
Both tools accept max_repr to control the representation payload: positive = truncate to N chars (default 500), 0 = omit, negative = include in full.
Sessions & Clients
| Tool | Description |
|---|---|
get_session_stats |
Active session count per client |
get_client_sessions |
Active sessions for one client (SP) |
list_clients |
SAML and OIDC clients in the realm |
get_realm_roles |
Realm-level roles |
Setup
# uv
uv pip install keycloak-mcp
# pip
pip install keycloak-mcp
From source:
git clone https://github.com/shigechika/keycloak-mcp.git
cd keycloak-mcp
# uv
uv sync
# pip
pip install -e .
Configuration
| Variable | Description | Default |
|---|---|---|
KEYCLOAK_URL |
Base URL, e.g. https://sso.example.com |
required |
KEYCLOAK_REALM |
Realm name | master |
KEYCLOAK_CLIENT_ID |
Service Account client ID | required |
KEYCLOAK_CLIENT_SECRET |
Client secret | required |
KEYCLOAK_SITES_INI |
INI file for IP-to-site labeling (see below) | unset |
KeyCloak client setup
- Create a new client in the KeyCloak admin console.
- Turn on Client authentication and Service account roles.
- Give it
view-users,view-events,view-clients, and — only if you need password reset —manage-users.
IP-to-site labeling (optional)
Point KEYCLOAK_SITES_INI at an INI file if you want IP addresses in tool output to be tagged with your site names. Tools like get_user_sessions, get_events, and get_login_failures_by_ip pick it up automatically; anything outside your declared ranges is labeled external. Leave the variable unset and IPs are shown as-is.
See sites.ini.example. A minimal file:
[hq]
name = HQ (Tokyo)
ipv4 = 192.0.2.0/24, 198.51.100.0/24
ipv6 = 2001:db8:1::/48
[vpn]
name = VPN
ipv4 = 10.0.0.0/8, 172.16.0.0/12
One site per [section]. name is the display label (falls back to the section name). ipv4 / ipv6 take comma-separated CIDRs; a single host is /32 or /128. Matching is first-match in file order — put specific ranges before broad ones.
Usage
Claude Code
In .mcp.json:
{
"mcpServers": {
"keycloak-mcp": {
"type": "stdio",
"command": "keycloak-mcp",
"env": {
"KEYCLOAK_URL": "https://sso.example.com",
"KEYCLOAK_CLIENT_ID": "keycloak-mcp",
"KEYCLOAK_CLIENT_SECRET": ""
}
}
}
}
Claude Desktop
In claude_desktop_config.json:
{
"mcpServers": {
"keycloak-mcp": {
"command": "keycloak-mcp",
"env": {
"KEYCLOAK_URL": "https://sso.example.com",
"KEYCLOAK_CLIENT_ID": "keycloak-mcp",
"KEYCLOAK_CLIENT_SECRET": ""
}
}
}
}
From a shell
export KEYCLOAK_URL=https://sso.example.com
export KEYCLOAK_CLIENT_ID=keycloak-mcp
export KEYCLOAK_CLIENT_SECRET=your-secret
keycloak-mcp
CLI
keycloak-mcp --version # Print version and exit
keycloak-mcp --help # Show usage and required environment variables
keycloak-mcp --check # Verify env vars and authentication, then exit
keycloak-mcp # Run the MCP STDIO server (default)
No-argument mode is the normal one — that's how MCP clients launch it.
--check exit codes: 0 success, 1 configuration error, 2 authentication error.
Development
git clone https://github.com/shigechika/keycloak-mcp.git
cd keycloak-mcp
# uv
uv sync --dev
uv run pytest -v
uv run ruff check .
# pip
python3 -m venv .venv
.venv/bin/pip install -e . && .venv/bin/pip install pytest pytest-cov respx ruff
.venv/bin/pytest -v
.venv/bin/ruff check .
License
MIT
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file keycloak_mcp-0.6.0.tar.gz.
File metadata
- Download URL: keycloak_mcp-0.6.0.tar.gz
- Upload date:
- Size: 26.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b9fcb7aca748eb386e238da305a88813e91a112263207516c0518f9dc073f6a8
|
|
| MD5 |
c90b7ff6e7fc1ccf77b5ab6a08d45eaf
|
|
| BLAKE2b-256 |
b4ba1d25ff48b8d1c6eeca50c0db042fc5a0106c4e0314deaeaa2ea81bcfc864
|
Provenance
The following attestation bundles were made for keycloak_mcp-0.6.0.tar.gz:
Publisher:
release.yml on shigechika/keycloak-mcp
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
keycloak_mcp-0.6.0.tar.gz -
Subject digest:
b9fcb7aca748eb386e238da305a88813e91a112263207516c0518f9dc073f6a8 - Sigstore transparency entry: 1419622452
- Sigstore integration time:
-
Permalink:
shigechika/keycloak-mcp@9b13cc9d371856d335df19f0385beb2bd678bddf -
Branch / Tag:
refs/tags/v0.6.0 - Owner: https://github.com/shigechika
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@9b13cc9d371856d335df19f0385beb2bd678bddf -
Trigger Event:
release
-
Statement type:
File details
Details for the file keycloak_mcp-0.6.0-py3-none-any.whl.
File metadata
- Download URL: keycloak_mcp-0.6.0-py3-none-any.whl
- Upload date:
- Size: 18.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
105929068ec06eede5a9f483ce8c829482010054d524765f2237a8696ecaf7cf
|
|
| MD5 |
5e2cdf045cb0b49d413bc99f0c10f78b
|
|
| BLAKE2b-256 |
918edfba5931e458438fd8f023325fab88563115e686531801bf68f26324e9e2
|
Provenance
The following attestation bundles were made for keycloak_mcp-0.6.0-py3-none-any.whl:
Publisher:
release.yml on shigechika/keycloak-mcp
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
keycloak_mcp-0.6.0-py3-none-any.whl -
Subject digest:
105929068ec06eede5a9f483ce8c829482010054d524765f2237a8696ecaf7cf - Sigstore transparency entry: 1419622549
- Sigstore integration time:
-
Permalink:
shigechika/keycloak-mcp@9b13cc9d371856d335df19f0385beb2bd678bddf -
Branch / Tag:
refs/tags/v0.6.0 - Owner: https://github.com/shigechika
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@9b13cc9d371856d335df19f0385beb2bd678bddf -
Trigger Event:
release
-
Statement type: