Repository navigation
ClientArchitecture
This document describes the three OIDC clients configured in Keycloak for McpServer authentication and their respective OAuth 2.0 flows.
McpServer uses Keycloak as its identity provider with three distinct client configurations:
- mcp-server-api — Confidential client for JWT validation on the API server
- mcp-director — Public client for CLI authentication via Device Authorization Flow
- mcp-web — Confidential client for browser-based authentication via Authorization Code Flow
All three clients are configured to work with the mcpserver realm and include the mcp-server-api audience in their tokens.
This client validates JWT tokens issued by Keycloak on the McpServer API endpoints. It does not directly authenticate users but provides the API server with the credentials needed to verify tokens.
- Client Type: Confidential (has a client secret)
-
Client ID:
mcp-server-api - Service Accounts: Enabled (for token introspection if needed)
- Standard Flow: Disabled (does not authenticate users directly)
- Direct Access Grants: Disabled
-
Audience: Self-referencing (tokens issued to other clients must include
mcp-server-apias audience)
The API server validates incoming JWT tokens using the client secret and authority URL configured in appsettings.yaml:
Mcp:
Auth:
Authority: http://localhost:7080/realms/mcpserver
Audience: mcp-server-api
ClientSecret: <retrieved-from-keycloak>
RequireHttpsMetadata: falseLocation: lib/McpServer/appsettings.yaml (or environment variables/user secrets)
Code Reference: lib/McpServer/src/McpServer.Services/Options/OidcAuthOptions.cs
This client authenticates CLI users via the OAuth 2.0 Device Authorization Flow. Users run the Director CLI, receive a user code, navigate to a verification URL in their browser, and authenticate through Keycloak. Once authenticated, the CLI receives an access token.
- Client Type: Public (no client secret required)
-
Client ID:
mcp-director - Device Authorization Grant: Enabled
- Standard Flow: Disabled
- Direct Access Grants: Disabled
- Service Accounts: Disabled
-
mcp-server-api-audience: Adds
mcp-server-apito theaudclaim -
realm-roles: Maps user realm roles into the
realm_rolesclaim
The Device Authorization Flow is a multi-step process designed for CLI tools and devices without a web browser:
-
Device Authorization Request: The CLI calls
/auth/device(proxied to Keycloak) with the client ID and scopes -
User Code Displayed: Keycloak returns a
user_code,verification_uri, anddevice_code -
User Authentication: The user navigates to the
verification_uriin a browser, enters theuser_code, and authenticates with Keycloak -
Token Polling: The CLI polls
/auth/token(proxied to Keycloak) with thedevice_codeuntil the user completes authentication -
Token Issued: Once authenticated, Keycloak returns an
access_tokenandrefresh_token
The Director CLI caches tokens locally and automatically refreshes them when expired.
CLI Implementation: src/McpServerManager.Director/Auth/OidcAuthService.cs
The Director CLI discovers OIDC configuration from the MCP server:
director auth loginThis:
- Calls
GET /auth/configon the MCP server to retrieve authority and client ID - Initiates the Device Flow by calling
POST /auth/device - Displays the user code and verification URI
- Polls
POST /auth/tokenuntil the user completes authentication - Caches the token locally in
~/.mcp-director/token.json
Configuration Discovery: lib/McpServer/src/McpServer.Support.Mcp/Controllers/AuthConfigController.cs
The MCP server exposes /auth/config to provide OIDC metadata to CLI clients:
{
"enabled": true,
"authority": "http://localhost:7080/realms/mcpserver",
"clientId": "mcp-director",
"scopes": "openid profile email",
"deviceAuthorizationEndpoint": "http://localhost:7147/auth/device",
"tokenEndpoint": "http://localhost:7147/auth/token"
}The MCP server proxies Device Flow requests to Keycloak so CLI clients only need to know the MCP server URL (e.g., http://localhost:7147), not the Keycloak URL.
This client authenticates browser-based users via the OAuth 2.0 Authorization Code Flow. Users navigate to the McpServer Web UI, click "Login", are redirected to Keycloak for authentication, and redirected back to the Web UI with an authorization code that is exchanged for tokens.
- Client Type: Confidential (has a client secret)
-
Client ID:
mcp-web - Standard Flow: Enabled (Authorization Code Flow)
- Direct Access Grants: Disabled
- Service Accounts: Disabled
-
Redirect URIs:
-
http://localhost:*(for local development) -
<MCP_SERVER_URL>/*(for deployed environments)
-
-
Web Origins:
http://localhost:*<MCP_SERVER_URL>
-
mcp-server-api-audience: Adds
mcp-server-apito theaudclaim -
realm-roles: Maps user realm roles into the
realm_rolesclaim
The Authorization Code Flow is the standard OAuth 2.0 flow for browser-based applications:
- Login Initiated: User clicks "Login" in the Web UI
- Redirect to Keycloak: User is redirected to Keycloak's authorization endpoint
- Authentication: User enters credentials in Keycloak
- Authorization Code: Keycloak redirects back to the Web UI with an authorization code
- Token Exchange: The Web UI backend exchanges the authorization code for an access token and refresh token using the client secret
- Token Storage: Tokens are stored in an HTTP-only session cookie
Web UI Configuration: src/McpServerManager.Web/appsettings.Development.json
{
"Authentication": {
"Schemes": {
"OpenIdConnect": {
"Authority": "http://localhost:7080/realms/mcpserver",
"ClientId": "mcp-web",
"ClientSecret": "<retrieved-from-keycloak>"
}
}
}
}Code Reference: src/McpServerManager.Web/Program.cs
The Web UI uses ASP.NET Core's OpenID Connect middleware to handle the Authorization Code Flow automatically. The middleware:
- Redirects unauthenticated requests to Keycloak
- Handles the callback with the authorization code
- Exchanges the code for tokens using the client secret
- Stores tokens in a secure session cookie
- Automatically refreshes tokens when expired
The Web UI can also discover OIDC configuration from the MCP server by setting DiscoverAuthorityFromMcpAuthConfig: true in appsettings.json.
All three clients issue tokens that include:
-
Audience (
aud):mcp-server-api(via the audience mapper) -
Realm Roles (
realm_roles): User's assigned roles (admin,agent-manager,viewer) -
Subject (
sub): Unique user identifier -
Username (
preferred_username): User's username -
Email (
email): User's email address
The MCP server validates these tokens using the mcp-server-api client credentials and the Keycloak authority URL.
- Client secret must be kept secure and never exposed in client applications
- Configured via environment variables, user secrets, or secure configuration management
- Only used server-side for token validation
- Public client (no client secret)
- Device Flow ensures user authentication happens in a browser, not in the CLI
- Tokens cached locally in
~/.mcp-director/token.jsonwith appropriate file permissions
- Client secret must be kept secure and never exposed to the browser
- Stored server-side only (in
appsettings.Development.jsonor user secrets) - Tokens stored in HTTP-only, secure session cookies (not accessible to JavaScript)
- Setup Scripts — Running the Keycloak setup automation
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