Skip to main content

MCP server for Keycloak Identity and Access Management

Project description

Keycloak MCP Server

MCP server for Keycloak Identity and Access Management instances. Auto-generates 401 tools from the Keycloak OpenAPI specification using FastMCP.from_openapi(). Includes CodeMode for efficient AI interaction with large tool sets, and supports dual authentication modes (OAuth2 HTTP transport for user sessions, client_credentials for stdio service accounts).

Configuration

Authentication: Keycloak supports two modes depending on transport:

  • stdio (service account): Uses client_credentials or password grant to obtain tokens automatically
  • HTTP (user OAuth): Uses OIDC proxy to authenticate end-users via browser flow

Option 1: config.json

cp config.json.example config.json
# Edit config.json with your Keycloak credentials

Option 2: Environment Variables

export KEYCLOAK_HOST="keycloak.example.com"
export KEYCLOAK_REALM="master"
export KEYCLOAK_CLIENT_ID="admin-cli"
export KEYCLOAK_CLIENT_SECRET="your-client-secret"
# Or use password grant:
export KEYCLOAK_USERNAME="admin"
export KEYCLOAK_PASSWORD="admin-password"
export KEYCLOAK_VERIFY_SSL="true"
export KEYCLOAK_CA_CERT_PATH=""
export KEYCLOAK_TIMEOUT="30"

Authentication Modes

stdio Mode (Service Account)

For AI assistants connecting via stdio transport. The server acquires tokens automatically using either:

  • Client credentials grant: Set client_id + client_secret. Best for service-to-service.
  • Password grant: Set username + password. Falls back to admin-cli as client_id.

Tokens are cached and refreshed automatically before expiry.

HTTP Mode (User OAuth Flow)

For web-based access where end-users authenticate via browser:

python -m bibliocommons_mcp_keycloak --transport http --port 8000

Uses OIDC proxy with your Keycloak realm's well-known configuration. Users authenticate through the standard Keycloak login page.

Installation

Option 1: Using uv (Recommended)

uv is a fast Python package manager. Install it first:

# macOS/Linux
curl -LsSf https://astral.sh/uv/install.sh | sh

# Windows
powershell -c "irm https://astral.sh/uv/install.ps1 | iex"

No additional installation needed - uvx will handle dependencies automatically.

Option 2: Using pip

pip install bibliocommons-mcp-keycloak
# or for development
pip install -e ".[dev]"

AI Client Setup

Quick Links: Amazon Q | Claude Desktop | VS Code | GitHub Copilot | Cline | Zed | Cursor | Kiro IDE | Kiro CLI

Amazon Q Developer (VS Code)

{
  "amazonQ.mcp.servers": {
    "keycloak": {
      "command": "uvx",
      "args": ["--from", "/absolute/path/to/keycloak", "bibliocommons-mcp-keycloak"]
    }
  }
}

Claude Desktop

Configuration file:

  • macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
  • Windows: %APPDATA%\Claude\claude_desktop_config.json
{
  "mcpServers": {
    "keycloak": {
      "command": "uvx",
      "args": ["--from", "/absolute/path/to/keycloak", "bibliocommons-mcp-keycloak"]
    }
  }
}

VS Code (with MCP Extension)

{
  "mcp.servers": {
    "keycloak": {
      "command": "uvx",
      "args": ["--from", "/absolute/path/to/keycloak", "bibliocommons-mcp-keycloak"]
    }
  }
}

GitHub Copilot (VS Code)

{
  "github.copilot.chat.mcp.servers": {
    "keycloak": {
      "command": "uvx",
      "args": ["--from", "/absolute/path/to/keycloak", "bibliocommons-mcp-keycloak"]
    }
  }
}

Cline (VS Code Extension)

{
  "cline.mcpServers": {
    "keycloak": {
      "command": "uvx",
      "args": ["--from", "/absolute/path/to/keycloak", "bibliocommons-mcp-keycloak"]
    }
  }
}

Zed Editor

{
  "mcpServers": {
    "keycloak": {
      "command": "uvx",
      "args": ["--from", "/absolute/path/to/keycloak", "bibliocommons-mcp-keycloak"]
    }
  }
}

Cursor IDE

{
  "mcp.servers": {
    "keycloak": {
      "command": "uvx",
      "args": ["--from", "/absolute/path/to/keycloak", "bibliocommons-mcp-keycloak"]
    }
  }
}

Kiro IDE

{
  "mcpServers": {
    "keycloak": {
      "command": "uvx",
      "args": ["--from", "/absolute/path/to/keycloak", "bibliocommons-mcp-keycloak"]
    }
  }
}

Kiro CLI

Create or edit ~/.kiro/settings/mcp.json:

{
  "mcpServers": {
    "keycloak": {
      "command": "uvx",
      "args": ["--from", "/absolute/path/to/keycloak", "bibliocommons-mcp-keycloak"]
    }
  }
}

Configuration Notes

  • Replace /absolute/path/to/keycloak/ with the actual path to your server directory
  • Add --config /path/to/config.json to the args array to use a specific config file
  • Add --read-only to the args array to disable destructive tools
  • After adding the configuration, restart your AI client for changes to take effect

Standalone MCP Server

python -m bibliocommons_mcp_keycloak

Read-Only Mode

Disable all write operations (POST, PUT, DELETE, PATCH) for safe, audit-only operation:

# CLI flag
python -m bibliocommons_mcp_keycloak --read-only

# Environment variable
export KEYCLOAK_READ_ONLY=true

In read-only mode, only GET-based tools are exposed. All mutating operations are excluded.

OpenAPI Spec Caching

The server downloads the Keycloak OpenAPI spec on first run and caches it locally as openapi.json. To use a custom cache location:

export KEYCLOAK_SPEC_CACHE="/path/to/openapi.json"

To refresh the cache, delete the file and restart the server.

Security Notes

  • Create a dedicated service account in Keycloak for MCP access with minimal required roles
  • Never commit config.json with real credentials (excluded by .gitignore)
  • Use config.json.example as a template
  • Prefer environment variables for production and CI environments
  • Use --read-only mode when write access is not needed
  • Tokens are cached in memory only and never written to disk
  • Set verify_ssl=true in production; only disable for development with self-signed certificates

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

bibliocommons_mcp_keycloak-1.0.0.tar.gz (12.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

bibliocommons_mcp_keycloak-1.0.0-py3-none-any.whl (8.7 kB view details)

Uploaded Python 3

File details

Details for the file bibliocommons_mcp_keycloak-1.0.0.tar.gz.

File metadata

File hashes

Hashes for bibliocommons_mcp_keycloak-1.0.0.tar.gz
Algorithm Hash digest
SHA256 ff84bd709193397adf694cbe4dcda3397e09525f57aef15175bf7e50c7f9bd52
MD5 2d989b179a888fbce6e9fa085f37cd29
BLAKE2b-256 cdc2befb40afd662c8d2b0a06f2ffab96b42bbcc2adef20142859d3a44beb0b6

See more details on using hashes here.

File details

Details for the file bibliocommons_mcp_keycloak-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for bibliocommons_mcp_keycloak-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 025d7e4079953531612e62cffcc54495c769d9871ab799d6c7ab51f877c66e40
MD5 02fc66a26f6bbb49ab1a7889c6cc54ce
BLAKE2b-256 0b9743cfe584e5ec10c4382bbd369d04b2092b3bbe503deb9e0719d99c481c8e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page