mcp-keycloak
Enables secure MCP tool calls (add and multiply numbers) by validating OAuth2 tokens via Keycloak token introspection.
README
MCP Keycloak Resource Server (mcp-keycloak)
This project implements a Model Context Protocol (MCP) protected Resource Server secured by an OAuth 2.0 Authorization Server (Keycloak) using Token Introspection (RFC 7662).
The server is built with the Python mcp SDK using the FastMCP framework, showcasing how to restrict access to MCP tools by requiring a valid Bearer token.
Architecture Overview
+--------------------+ 1. Request Token +--------------------+
| | ------------------------------> | |
| MCP Client | | Keycloak |
| (e.g., test_client)| <------------------------------ | (Auth Server, :8080)
| | 2. Access Token +--------------------+
+--------------------+ ^
| |
| 3. Call Tool (with Bearer Token) |
v | 4. Introspect
+--------------------+ | Token
| MCP Server | ------------------------------------------+
| (Resource, :3000) | <------------------------------------------
+--------------------+ 5. Active: True/False
- Keycloak (Authorization Server): Serves on port
8080. It handles client credential grants and token introspection. A pre-configured database is included in the project directory (keycloak_data/) to make spin-up seamless. - MCP Resource Server (Resource Server): Built with
FastMCP, running on port3000. When a client calls a protected tool, the server intercepts the request and validates theAuthorization: Bearer <token>header against Keycloak's introspection endpoint. - Token Verifier (
IntrospectionTokenVerifier): Implements standard OAuth 2.0 token introspection (RFC 7662). It validates token activity, resource/audience restrictions (aud), scope limits, and expiration times.
Prerequisites
- Python: Version
3.12or higher (compatible withuv) - Docker & Docker Compose: To run Keycloak
Pre-Configured Keycloak Clients
The embedded Keycloak database is pre-configured with three OAuth 2.0 clients within the master realm. The full, exact JSON configuration of these clients is exported and available in the keycloak_config.json file.
These clients use the Client Credentials Grant flow (via Service Accounts) and are configured with specific client secrets, scopes, and audience mappers:
1. mcp-client (Primary Testing Client)
- Client ID:
mcp-client - Client Secret:
64gq3p8y3siZKZoKH6X9Bq2oqIPfukBZYMnmyCVCJw1QgjfNzdympB0eaZ1aXtFl0yNUWzGr3S7Gov3gR4kLfe - Authentication Flow: Client Credentials (Service Account enabled)
- Assigned Scopes:
mcp:tools - Audience Mappers:
mcp-serverhttp://localhost:3000(derived resource server URL)
2. mcp-client-2 (Alternative Testing Client)
- Client ID:
mcp-client-2 - Client Secret:
FebOB24OaG3F4J9dMZPoZ7qhzQvUKQ5HgJ6IbQ6yXQBd5tg88f8tPSEA0aUGK9AuEebxHRBwsPdLuDjQpiLFhR - Authentication Flow: Client Credentials (Service Account enabled)
- Assigned Scopes:
mcp:tools - Audience Mappers:
mcp-serverhttp://localhost:3000
3. mcp-server (Resource Server Introspection)
- Client ID:
mcp-server - Client Secret:
5XflSzJJrvQD8iNz4t7RMI4i9rNvdnLQs5PSo8EMGkfUDKr02SjaPs4fKA9kWNO1G06nhDzTHMeHrnz9H4h5ut - Authentication Flow: Client Credentials (Service Account enabled) — used by the Resource Server to authenticate introspection requests.
- Assigned Scopes:
mcp:tools - Audience Mappers:
mcp-serverhttp://localhost:3000
Installation & Setup
1. Start Keycloak
Run the Keycloak container in development mode using the provided docker-compose.yml:
docker compose up -d
Keycloak starts on http://127.0.0.1:8080. Admin credentials are:
- Username:
admin - Password:
admin
Note: The container mounts ./keycloak_data, preserving the pre-configured realm, clients (mcp-server, mcp-client, and mcp-client-2), and settings.
2. Install Project Dependencies
If you are using uv:
uv sync
Alternatively, standard pip can be used:
pip install -e .
Running the Server
Start the MCP Resource Server:
uv run main.py
By default, the server starts on http://localhost:3000 using the streamable-http transport (supporting Streamable HTTP MCP communication).
Server Configuration
Configuration options can be customized via environment variables defined in config.py:
| Environment Variable | Default Value | Description |
|---|---|---|
HOST |
localhost |
MCP Server hostname |
PORT |
3000 |
MCP Server port |
AUTH_HOST |
localhost |
Keycloak server host |
AUTH_PORT |
8080 |
Keycloak server port |
AUTH_REALM |
master |
Keycloak Realm to use |
OAUTH_CLIENT_ID |
mcp-server |
Client ID the Resource Server uses for Introspection |
OAUTH_CLIENT_SECRET |
5XflSzJJrv... |
Client Secret for Introspection client |
MCP_SCOPE |
mcp:tools |
Scope required to invoke the MCP tools |
OAUTH_STRICT |
false |
Enable strict OAuth validations |
TRANSPORT |
streamable-http |
MCP Transport protocol (streamable-http or sse) |
Secure MCP Tools Provided
The server registers two secure arithmetic tools:
-
add_numbers- Arguments:
a: float,b: float - Operation: Adds two numbers together.
- Output: Returns JSON containing parameters, sum result, and timestamp.
- Arguments:
-
multiply_numbers- Arguments:
x: float,y: float - Operation: Multiplies two numbers.
- Output: Returns JSON containing parameters, product result, and timestamp.
- Arguments:
Testing with the Test Client
A CLI-based test client (test_client.py) is included to simulate MCP client interactions.
1. View Help Options
To see all available CLI flags:
uv run test_client.py --help
2. Automatically Fetch Token & List Secure Tools
To retrieve a client token via Keycloak's client credentials flow (using client credentials configured for mcp-client) and list the available tools on the MCP server:
uv run test_client.py --fetch-token
To use the alternative mcp-client-2 client, specify the client credentials flags:
uv run test_client.py --fetch-token --client-id mcp-client-2 --client-secret FebOB24OaG3F4J9dMZPoZ7qhzQvUKQ5HgJ6IbQ6yXQBd5tg88f8tPSEA0aUGK9AuEebxHRBwsPdLuDjQpiLFhR
3. Call a Protected Tool
To execute a secure tool using an automatically fetched token:
Addition Example:
uv run test_client.py --fetch-token --call add_numbers --args '{"a": 15.5, "b": 24.5}'
Multiplication Example:
uv run test_client.py --fetch-token --call multiply_numbers --args '{"x": 6.0, "y": 7.0}'
Technical Details
Token Validation Rules (token_verifier.py)
When an incoming MCP request is received, IntrospectionTokenVerifier.verify_token(token) validates:
- HTTP status: The introspection request successfully responds with an HTTP
200status. - Activity: The response JSON includes
"active": true. - Audience (
aud): The audience claim matches the resource server URL (derived dynamically, e.g.,http://localhost:3000/). - Scopes: Validated to ensure the client has the required
mcp:toolsscope.
推荐服务器
Baidu Map
百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
Playwright MCP Server
一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。
Magic Component Platform (MCP)
一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。
Audiense Insights MCP Server
通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。
VeyraX
一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。
graphlit-mcp-server
模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。
Kagi MCP Server
一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。
e2b-mcp-server
使用 MCP 通过 e2b 运行代码。
Neon MCP Server
用于与 Neon 管理 API 和数据库交互的 MCP 服务器
Exa MCP Server
模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。