Keycloak MCP Server
# Keycloak MCP Server
<p align="center">
<img src="https://img.shields.io/badge/Keycloak-REST_API-00B2B2?style=for-the-badge&logo=keycloak&logoColor=white" alt="Keycloak version">
<img src="https://img.shields.io/badge/MCP-Model_Context_Protocol-5A67D8?style=for-the-badge" alt="MCP">
<img src="https://img.shields.io/badge/Python-3.11%2B-3776AB?style=for-the-badge&logo=python&logoColor=white" alt="Python">
</p>
<p align="center">
<strong>A <a href="https://modelcontextprotocol.io">Model Context Protocol (MCP)</a> server that exposes the <a href="https://www.keycloak.org/docs-api/latest/rest-api/index.html">Keycloak Admin REST API</a> as typed MCP tools. 415 tools covering all API categories.</strong>
</p>
## Features
- **Complete API coverage**: All 415 Keycloak Admin REST API endpoints
- **Dual transport**: stdio (Claude Code, Claude Desktop) and Streamable HTTP (GitHub Copilot, other MCP clients)
- **MCP SDK 2.x**: built on the `mcp` 2.x Python SDK
- **Auto-authentication**: Supports both password and client credentials flows with automatic token refresh
- **Zero configuration tools**: Each tool is self-describing with full input schemas
### API Categories
| Category | Tools |
| -------------------------- | ------- |
| Attack Detection | 3 |
| Authentication Management | 39 |
| Authorization Services | 31 |
| Client Certificates | 7 |
| Client Initial Access | 3 |
| Client Registration Policy | 1 |
| Client Role Mappings | 10 |
| Client Scopes | 10 |
| Clients | 35 |
| Components | 6 |
| Groups | 11 |
| Identity Providers | 17 |
| Keys | 1 |
| Organizations | 49 |
| Protocol Mappers | 21 |
| Realms Admin | 45 |
| Roles | 28 |
| Roles by ID | 10 |
| Scope Mappings | 45 |
| Users | 34 |
| Workflows | 9 |
| **Total** | **415** |
## Compatibility
The tools follow the Keycloak Admin REST API of **Keycloak 26.8**, where all 415 work. They were also run against real containers of Keycloak 24.0.5, 25.0.6, 26.0.8, 26.2.5, 26.3.5, 26.4.7, 26.5.3, 26.6.1, 26.7.5 and 26.8.2. Versions older than 24 were not tested.
On an older server the tools that do not exist there simply return Keycloak's 404/405 error; everything else keeps working. Tools that need a newer version say so in their description ("Requires Keycloak X+"). The version is the first one where the tool was seen working; it may have appeared in an earlier patch release of the same line.
| Tools | First tested version |
| --- | --- |
| Everything not listed below (users, clients, roles, groups, realms, authentication, ...) | 24.0.5 |
| Organizations (CRUD, members, identity providers) and `get_user_unmanaged_attributes` | 25 (organizations are a preview feature in 25, stable in 26.0) |
| `count_organization_members`, `list_member_organizations`, `list_user_organizations_by_member` | 26.0 |
| `count_organizations` | 26.3 |
| Organization invitations, Workflows, `upload_identity_provider_certificate`, `import_authz_resource_server` | 26.5 |
| Organization groups, `migrate_workflow_scheduled_resources` | 26.6 |
| Organization group role mappings, `update_organization_member_membership_type`, `generate_client_example_saml_response` | 26.7 |
| `update_organization_identity_provider_link` | 26.8 |
Some features are disabled by default and must be enabled on the server (`--features=...`): `workflows` (Workflows), `client-types` (`list_client_types`, `update_client_types`) and `admin-fine-grained-authz` (the `*_permissions` tools). Without them Keycloak answers 501 or 404.
## Install with AI
Give your AI (Claude Code, Copilot, Cursor, ...) this prompt and it will install and register the server for you:
> Install the Keycloak MCP server from https://github.com/Cayetano97/Keycloack-MCP-Server in this environment. Follow the section "Instructions for the AI agent" of its README.
### Instructions for the AI agent
Goal: the `keycloak` MCP server runs over stdio in the user's MCP client and can reach their Keycloak. Do the steps in order and stop to ask the user where indicated.
1. **Check prerequisites.** Run `uv --version`. If it is missing, ask the user before installing it (Windows: `powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"`; macOS/Linux: `curl -LsSf https://astral.sh/uv/install.sh | sh`). `uv` fetches Python 3.11+ by itself, so Python is not a separate requirement.
2. **Ask the user for the connection details. Never invent or guess them.**
- `KEYCLOAK_URL`: base URL of the server, with `http://` or `https://`, no trailing `/realms/...` (e.g. `https://sso.example.com`).
- Credentials, one of:
- Password flow: `KEYCLOAK_ADMIN_USERNAME` + `KEYCLOAK_ADMIN_PASSWORD` (client `admin-cli` by default).
- Client credentials flow (recommended for anything beyond local use): `KEYCLOAK_CLIENT_ID` + `KEYCLOAK_CLIENT_SECRET`. The client needs "Service accounts roles" enabled and the realm-management roles it will use.
- Optional: `KEYCLOAK_ADMIN_REALM` (default `master`) and `KEYCLOAK_VERIFY_SSL=false` only for self-signed certificates.
3. **Register the server** in the user's MCP client. Do not run a separate install step: `uvx` downloads and runs it on demand.
- Claude Code:
```bash
claude mcp add keycloak \
-e KEYCLOAK_URL=<url> \
-e KEYCLOAK_ADMIN_USERNAME=<user> -e KEYCLOAK_ADMIN_PASSWORD=<password> \
-- uvx --from git+https://github.com/Cayetano97/Keycloack-MCP-Server keycloak-mcp-server
```
Replace the two credential variables with `-e KEYCLOAK_CLIENT_ID=<id> -e KEYCLOAK_CLIENT_SECRET=<secret>` for the client credentials flow. Add `--scope user` to make it available in every project.
- Any other client (Claude Desktop, Cursor, VS Code, ...): add this to its MCP config file (`claude_desktop_config.json`, `.cursor/mcp.json`, `.vscode/mcp.json`, ...; VS Code uses a top-level `servers` key instead of `mcpServers`):
```json
{
"mcpServers": {
"keycloak": {
"command": "uvx",
"args": ["--from", "git+https://github.com/Cayetano97/Keycloack-MCP-Server", "keycloak-mcp-server"],
"env": {
"KEYCLOAK_URL": "<url>",
"KEYCLOAK_ADMIN_USERNAME": "<user>",
"KEYCLOAK_ADMIN_PASSWORD": "<password>"
}
}
}
}
```
Merge into the existing file; do not overwrite other servers.
4. **Verify.**
- Check that the server starts: `uvx --from git+https://github.com/Cayetano97/Keycloack-MCP-Server keycloak-mcp-server --help` must print usage without errors.
- Check the credentials against Keycloak (replace the values; a JSON response with an `access_token` means they work):
```bash
curl -s -X POST "<url>/realms/master/protocol/openid-connect/token" \
-d grant_type=password -d client_id=admin-cli -d username=<user> -d password=<password>
```
For the client credentials flow use `-d grant_type=client_credentials -d client_id=<id> -d client_secret=<secret>`.
- Tell the user to restart or reload their MCP client, then call `list_realms` to confirm the tools work.
5. **Handle secrets carefully.** Never print the password or secret back in your replies, never commit them, and if the config file lives inside a git repository, make sure it is git-ignored (or use `--scope user` / the user's global config).
Common failures:
| Symptom | Cause |
| --- | --- |
| `uv: command not found` | `uv` not installed or not on `PATH`; restart the terminal after installing it. |
| `invalid_grant` / `unauthorized_client` | Wrong credentials, or the client does not allow that grant (enable "Direct access grants" for password flow, "Service accounts roles" for client credentials). |
| SSL certificate errors | Self-signed certificate: set `KEYCLOAK_VERIFY_SSL=false`. |
| Connection refused / timeout | Wrong `KEYCLOAK_URL` or port. On Keycloak 16 and older the URL needs the `/auth` suffix. |
| `403` on some tools | The account lacks the matching `realm-management` role. |
| `501` / `404` on a tool | The feature is disabled on the server or the Keycloak version is too old (see [Compatibility](#compatibility)). |
## Installation
### Option 1: Run directly with `uvx` (Recommended)
You can run the server without a manual installation using astral's `uv`:
```bash
uvx --from git+https://github.com/Cayetano97/Keycloack-MCP-Server keycloak-mcp-server
```
_(You can pass environment variables inline or keep them in your MCP config file. Note: the name `keycloak-mcp-server` on PyPI belongs to a different project, so install from this repository.)_
### Option 2: Install via pip
If you prefer a global or virtual environment installation:
```bash
pip install git+https://github.com/Cayetano97/Keycloack-MCP-Server.git
```
### Option 3: Install from source (For development)
```bash
git clone https://github.com/Cayetano97/Keycloack-MCP-Server.git
cd Keycloack-MCP-Server
uv sync
```
## Configuration
Set environment variables (or create a `.env` file based on `.env.example`):
```bash
# Required
export KEYCLOAK_URL=http://localhost:8080
# Authentication - Option A: Password flow
export KEYCLOAK_ADMIN_USERNAME=admin
export KEYCLOAK_ADMIN_PASSWORD=admin
# Authentication - Option B: Client credentials flow
export KEYCLOAK_CLIENT_ID=my-client
export KEYCLOAK_CLIENT_SECRET=my-secret
# Optional
export KEYCLOAK_ADMIN_REALM=master # default: master
export KEYCLOAK_VERIFY_SSL=true # default: true
```
## Usage
### Claude Code (stdio)
Add to your Claude Code MCP configuration (a project-level `.mcp.json`, or `claude mcp add`), or to `claude_desktop_config.json` for Claude Desktop:
```json
{
"mcpServers": {
"keycloak": {
"command": "python",
"args": ["-m", "keycloak_mcp_server"],
"env": {
"KEYCLOAK_URL": "http://localhost:8080",
"KEYCLOAK_ADMIN_USERNAME": "admin",
"KEYCLOAK_ADMIN_PASSWORD": "admin"
}
}
}
}
```
Or if installed with uv:
```json
{
"mcpServers": {
"keycloak": {
"command": "uv",
"args": [
"run",
"--directory",
"/path/to/keycloak-mcp-server",
"python",
"-m",
"keycloak_mcp_server"
],
"env": {
"KEYCLOAK_URL": "http://localhost:8080",
"KEYCLOAK_ADMIN_USERNAME": "admin",
"KEYCLOAK_ADMIN_PASSWORD": "admin"
}
}
}
}
```
### Command Line
```bash
# stdio mode (default)
python -m keycloak_mcp_server
# Streamable HTTP mode
python -m keycloak_mcp_server --transport streamable-http --host 127.0.0.1 --port 8080
# Using the entry point
keycloak-mcp-server --transport streamable-http --port 8080
```
## Examples
Once connected, you can use natural language to interact with Keycloak:
- _"List all realms"_ → calls `list_realms`
- _"Create a user called john in the master realm"_ → calls `create_user`
- _"Show me all clients in the production realm"_ → calls `list_clients`
- _"What roles does user X have?"_ → calls `get_user_role_mappings`
- _"Add the admin role to the developers group"_ → calls `add_group_realm_role_mappings`
## Project Structure
```
src/keycloak_mcp_server/
├── __init__.py # Package entry point
├── __main__.py # CLI entry point
├── config.py # Environment-based configuration
├── client.py # Async HTTP client with auto-auth
├── server.py # MCP server setup and tool registration
└── endpoints/ # Endpoint definitions by category
├── __init__.py # Base classes (EndpointDef, Param)
├── attack_detection.py
├── authentication.py
├── authorization_services.py
├── certificates.py
├── client_initial_access.py
├── client_registration_policy.py
├── client_role_mappings.py
├── client_scopes.py
├── clients.py
├── component.py
├── groups.py
├── identity_providers.py
├── key.py
├── organizations.py
├── protocol_mappers.py
├── realms.py
├── roles.py
├── roles_by_id.py
├── scope_mappings.py
├── users.py
└── workflows.py
```
## License
MIT
TDQS
Scored across 415 tools
The tool set is enormous and riddled with near-duplicate scope/role-mapping operations (e.g., client, client template, client scope, and organization group scope mappings) whose descriptions are almost indistinguishable. Many tools differ only by resource qualifier or version, making misselection highly likely.
Most names use a snake_case verb_noun pattern, but the convention is stretched by very long compound names and inconsistent orderings such as get_client_template_scope_mappings_all versus get_client_scope_mappings_all. A few tools also use 'delete_' where sibling tools use 'remove_' for the same operation.
415 tools is an extreme mismatch for any practical MCP surface. Even for the broad Keycloak admin domain, this volume forces excessive tool selection overhead and strongly suggests raw API exposure rather than a curated set.
The surface appears to cover nearly every Keycloak administrative area: users, groups, roles, clients, client scopes, identity providers, organizations, authorization services, workflows, events, and localization. No obvious major domain gap is apparent.