Repository navigation
SetupScripts
This document describes how to run the Keycloak setup scripts that automate the creation and configuration of the mcpserver realm and its three OIDC clients.
The setup scripts automate the following tasks:
- Create the
mcpserverrealm in Keycloak - Configure the
mcp-server-apiclient (confidential, JWT validation) - Configure the
mcp-directorclient (public, Device Flow) - Configure the
mcp-webclient (confidential, Authorization Code Flow) - Add protocol mappers for audience and realm roles
- Create realm roles (
admin,agent-manager,viewer) - Display client secrets for manual configuration
Before running the setup scripts, ensure:
-
Keycloak is running (default:
http://localhost:7080) -
Admin credentials are available (default:
admin/admin) - PowerShell 5.1+ (Windows) or bash with curl (Linux/macOS)
scripts/Setup-McpKeycloak.ps1
.\scripts\Setup-McpKeycloak.ps1This uses default values:
- Keycloak URL:
http://localhost:7080 - Admin username:
admin - Admin password:
admin - Realm name:
mcpserver - QBrain.AI URL:
http://localhost:7147
.\scripts\Setup-McpKeycloak.ps1 `
-KeycloakUrl "http://keycloak:8080" `
-AdminUser "admin" `
-AdminPassword "<your-keycloak-admin-password>" `
-RealmName "mcpserver" `
-McpServerUrl "https://mcp.example.com"| Parameter | Description | Default |
|---|---|---|
-KeycloakUrl |
Base URL of the Keycloak server | http://localhost:7080 |
-AdminUser |
Keycloak admin username | admin |
-AdminPassword |
Keycloak admin password | admin |
-RealmName |
Name of the realm to create | mcpserver |
-McpServerUrl |
Base URL of the MCP server for redirect URIs | http://localhost:7147 |
========================================
McpServer Keycloak Setup
========================================
Keycloak URL: http://localhost:7080
Realm: mcpserver
QBrain.AI URL: http://localhost:7147
[1/10] Authenticating with Keycloak...
✓ Authenticated as admin
[2/10] Creating realm 'mcpserver'...
✓ Realm 'mcpserver' created
[3/10] Creating mcp-server-api client (confidential, JWT validation)...
✓ Client 'mcp-server-api' created
[4/10] Retrieving mcp-server-api client secret...
✓ Client secret retrieved
[5/10] Creating mcp-director client (public, Device Flow)...
✓ Client 'mcp-director' created
[6/10] Adding protocol mappers to mcp-director...
✓ Added audience mapper
✓ Added realm-roles mapper
[7/10] Creating mcp-web client (confidential, Standard Flow)...
✓ Client 'mcp-web' created
[8/10] Retrieving mcp-web client secret...
✓ Client secret retrieved
[9/10] Adding protocol mappers to mcp-web...
✓ Added audience mapper
✓ Added realm-roles mapper
[10/10] Creating realm roles...
✓ Created role 'admin'
✓ Created role 'agent-manager'
✓ Created role 'viewer'
[10/10] Setup complete!
========================================
Setup Summary
========================================
Realm: mcpserver
Authority: http://localhost:7080/realms/mcpserver
Clients configured:
• mcp-server-api (confidential, JWT validation)
• mcp-director (public, Device Flow)
• mcp-web (confidential, Standard Flow)
mcp-server-api client secret:
<client-secret-from-keycloak>
mcp-web client secret:
z9y8x7w6-v5u4-3210-zyxw-vu9876543210
Redirect URIs (mcp-web):
• http://localhost:*
• http://localhost:7147/*
Web Origins (mcp-web):
• http://localhost:*
• http://localhost:7147
Roles created: admin, agent-manager, viewer
Next steps:
1. Create users in Keycloak admin console (http://localhost:7080/admin)
2. Assign realm roles to users
3. Configure McpServer appsettings.json:
"Mcp": {
"Auth": {
"Authority": "http://localhost:7080/realms/mcpserver",
"Audience": "mcp-server-api",
"ClientSecret": "<client-secret-from-keycloak>",
"RequireHttpsMetadata": false,
"DirectorClientId": "mcp-director"
}
}
========================================
scripts/setup-mcp-keycloak.sh
./scripts/setup-mcp-keycloak.shexport KEYCLOAK_URL="http://keycloak:8080"
export ADMIN_USER="admin"
export ADMIN_PASSWORD="<your-keycloak-admin-password>"
export REALM_NAME="mcpserver"
export MCP_SERVER_URL="https://mcp.example.com"
./scripts/setup-mcp-keycloak.sh| Variable | Description | Default |
|---|---|---|
KEYCLOAK_URL |
Base URL of the Keycloak server | http://localhost:7080 |
ADMIN_USER |
Keycloak admin username | admin |
ADMIN_PASSWORD |
Keycloak admin password | admin |
REALM_NAME |
Name of the realm to create | mcpserver |
MCP_SERVER_URL |
Base URL of the MCP server for redirect URIs | http://localhost:7147 |
The bash script produces the same output as the PowerShell script, with colored text for improved readability.
Both scripts are idempotent and can be run multiple times safely:
- If the realm already exists, it skips creation
- If roles already exist, they are skipped
- Client secrets remain unchanged if clients already exist
Note: Existing protocol mappers are not updated. To reconfigure, delete the clients in Keycloak and re-run the script.
After running the setup script:
- Navigate to the Keycloak admin console:
http://localhost:7080/admin - Log in with admin credentials
- Select the
mcpserverrealm - Go to Users → Add user
- Set username, email, and other attributes
- Go to Credentials → Set password (uncheck "Temporary")
- In the user detail page, go to Role Mappings
- Under Realm Roles, assign one or more roles:
-
admin— Full administrative access -
agent-manager— Manage agents and sessions -
viewer— Read-only access
-
Update lib/McpServer/appsettings.yaml with the client secret displayed in the setup summary:
Mcp:
Auth:
Authority: http://localhost:7080/realms/mcpserver
Audience: mcp-server-api
ClientSecret: <client-secret-from-keycloak>
RequireHttpsMetadata: false
DirectorClientId: mcp-directorOr use environment variables:
export Mcp__Auth__Authority="http://localhost:7080/realms/mcpserver"
export Mcp__Auth__Audience="mcp-server-api"
export Mcp__Auth__ClientSecret="<client-secret-from-keycloak>"
export Mcp__Auth__RequireHttpsMetadata="false"
export Mcp__Auth__DirectorClientId="mcp-director"For the ASP.NET Core Web host, set Authentication:Schemes:OpenIdConnect:Authority and Authentication:Schemes:OpenIdConnect:ClientId. Supply the Web client secret from Keycloak through local host configuration or the Authentication__Schemes__OpenIdConnect__ClientSecret environment variable. Keep the secret out of source control.
- Verify Keycloak is running:
curl http://localhost:7080/health - Check admin credentials in Keycloak admin console
- Ensure the
admin-cliclient is enabled in themasterrealm
This is expected if re-running the script. The script skips existing clients but does not update them.
To force recreation:
- Delete the clients in Keycloak admin console
- Re-run the setup script
This is expected and safe. The script skips realm creation if it already exists.
The setup script configures the following token lifespans in the mcpserver realm:
- Access Token Lifespan: 3600 seconds (1 hour)
- SSO Session Idle Timeout: 3600 seconds (1 hour)
- SSO Session Max Lifespan: 36000 seconds (10 hours)
- Refresh Token Max Reuse: 0 (refresh tokens can be reused)
- Revoke Refresh Token: false (refresh tokens are not revoked after use)
These values ensure tokens remain valid for at least 1 hour, which is enforced by the MCP server's token lifetime validation.
To adjust these values:
- Navigate to the Keycloak admin console:
http://localhost:7080/admin - Select the
mcpserverrealm - Go to Realm Settings → Tokens
- Adjust the lifespan values as needed
- Client Architecture — Understanding the three OIDC clients
This wiki was generated by QBrain. Explore the QBrain project on GitHub.
- Home
- Functional Requirements
- Technical Requirements
- Testing Requirements
- TR per FR Mapping
- Requirements Matrix
- McpServerManager
- Director
- Director Requirements
- Web UI Requirements
- MCP Web Architecture
- Operations
- Keycloak Client Architecture
- Keycloak Setup Scripts
- MVVM Data Resolution Pattern
- DI Convergence Matrix
- DI Convergence Diagram