44 Automates Keycloak realm and client setup for McpServer OIDC authentication.
55
66. DESCRIPTION
7- Creates the mcpserver realm, configures mcp-director (public client for Device Flow)
8- and mcp-web (confidential client for web UI) clients with appropriate protocol mappers,
9- redirect URIs, and audience claims. Displays client secrets in the setup summary.
7+ Creates the mcpserver realm, configures mcp-server-api (confidential client for JWT validation),
8+ mcp-director (public client for Device Flow), and mcp-web (confidential client for web UI)
9+ clients with appropriate protocol mappers, redirect URIs, and audience claims.
10+ Displays client secrets in the setup summary.
1011
1112. PARAMETER KeycloakUrl
1213 Base URL of the Keycloak server (default: http://localhost:7080)
@@ -85,7 +86,7 @@ function Invoke-KeycloakApi {
8586 }
8687}
8788
88- Write-Host " [1/9 ] Authenticating with Keycloak..." - ForegroundColor Yellow
89+ Write-Host " [1/10 ] Authenticating with Keycloak..." - ForegroundColor Yellow
8990
9091$tokenResponse = Invoke-RestMethod - Uri " $KeycloakUrl /realms/master/protocol/openid-connect/token" - Method Post - Body @ {
9192 grant_type = " password"
@@ -97,7 +98,7 @@ $tokenResponse = Invoke-RestMethod -Uri "$KeycloakUrl/realms/master/protocol/ope
9798$token = $tokenResponse.access_token
9899Write-Host " ✓ Authenticated as $AdminUser " - ForegroundColor Green
99100
100- Write-Host " [2/9 ] Creating realm '$RealmName '..." - ForegroundColor Yellow
101+ Write-Host " [2/10 ] Creating realm '$RealmName '..." - ForegroundColor Yellow
101102
102103$existingRealm = try {
103104 Invoke-KeycloakApi - Method Get - Path " /admin/realms/$RealmName " - Token $token
@@ -120,7 +121,32 @@ if ($existingRealm) {
120121 Write-Host " ✓ Realm '$RealmName ' created" - ForegroundColor Green
121122}
122123
123- Write-Host " [3/9] Creating mcp-director client (public, Device Flow)..." - ForegroundColor Yellow
124+ Write-Host " [3/10] Creating mcp-server-api client (confidential, JWT validation)..." - ForegroundColor Yellow
125+
126+ $apiClientConfig = @ {
127+ clientId = " mcp-server-api"
128+ publicClient = $false
129+ serviceAccountsEnabled = $true
130+ standardFlowEnabled = $false
131+ directAccessGrantsEnabled = $false
132+ attributes = @ {
133+ " oauth2.device.authorization.grant.enabled" = " false"
134+ }
135+ }
136+
137+ $apiClient = Invoke-KeycloakApi - Method Post - Path " /admin/realms/$RealmName /clients" - Token $token - Body $apiClientConfig
138+ Write-Host " ✓ Client 'mcp-server-api' created" - ForegroundColor Green
139+
140+ $apiClients = Invoke-KeycloakApi - Method Get - Path " /admin/realms/$RealmName /clients?clientId=mcp-server-api" - Token $token
141+ $apiClientId = $apiClients [0 ].id
142+
143+ Write-Host " [4/10] Retrieving mcp-server-api client secret..." - ForegroundColor Yellow
144+
145+ $apiClientSecret = Invoke-KeycloakApi - Method Get - Path " /admin/realms/$RealmName /clients/$apiClientId /client-secret" - Token $token
146+ $apiSecretValue = $apiClientSecret.value
147+ Write-Host " ✓ Client secret retrieved" - ForegroundColor Green
148+
149+ Write-Host " [5/10] Creating mcp-director client (public, Device Flow)..." - ForegroundColor Yellow
124150
125151$directorClientConfig = @ {
126152 clientId = " mcp-director"
@@ -140,7 +166,7 @@ Write-Host " ✓ Client 'mcp-director' created" -ForegroundColor Green
140166$directorClients = Invoke-KeycloakApi - Method Get - Path " /admin/realms/$RealmName /clients?clientId=mcp-director" - Token $token
141167$directorClientId = $directorClients [0 ].id
142168
143- Write-Host " [4/9 ] Adding protocol mappers to mcp-director..." - ForegroundColor Yellow
169+ Write-Host " [6/10 ] Adding protocol mappers to mcp-director..." - ForegroundColor Yellow
144170
145171$audienceMapper = @ {
146172 name = " mcp-server-api-audience"
@@ -173,7 +199,7 @@ $realmRolesMapper = @{
173199Invoke-KeycloakApi - Method Post - Path " /admin/realms/$RealmName /clients/$directorClientId /protocol-mappers/models" - Token $token - Body $realmRolesMapper
174200Write-Host " ✓ Added realm-roles mapper" - ForegroundColor Green
175201
176- Write-Host " [5/9 ] Creating mcp-web client (confidential, Standard Flow)..." - ForegroundColor Yellow
202+ Write-Host " [7/10 ] Creating mcp-web client (confidential, Standard Flow)..." - ForegroundColor Yellow
177203
178204$webClientConfig = @ {
179205 clientId = " mcp-web"
@@ -197,13 +223,13 @@ Write-Host " ✓ Client 'mcp-web' created" -ForegroundColor Green
197223$webClients = Invoke-KeycloakApi - Method Get - Path " /admin/realms/$RealmName /clients?clientId=mcp-web" - Token $token
198224$webClientId = $webClients [0 ].id
199225
200- Write-Host " [6/9 ] Retrieving mcp-web client secret..." - ForegroundColor Yellow
226+ Write-Host " [8/10 ] Retrieving mcp-web client secret..." - ForegroundColor Yellow
201227
202228$webClientSecret = Invoke-KeycloakApi - Method Get - Path " /admin/realms/$RealmName /clients/$webClientId /client-secret" - Token $token
203229$webSecretValue = $webClientSecret.value
204230Write-Host " ✓ Client secret retrieved" - ForegroundColor Green
205231
206- Write-Host " [7/9 ] Adding protocol mappers to mcp-web..." - ForegroundColor Yellow
232+ Write-Host " [9/10 ] Adding protocol mappers to mcp-web..." - ForegroundColor Yellow
207233
208234$webAudienceMapper = @ {
209235 name = " mcp-server-api-audience"
@@ -236,7 +262,7 @@ $webRealmRolesMapper = @{
236262Invoke-KeycloakApi - Method Post - Path " /admin/realms/$RealmName /clients/$webClientId /protocol-mappers/models" - Token $token - Body $webRealmRolesMapper
237263Write-Host " ✓ Added realm-roles mapper" - ForegroundColor Green
238264
239- Write-Host " [8/9 ] Creating realm roles..." - ForegroundColor Yellow
265+ Write-Host " [10/10 ] Creating realm roles..." - ForegroundColor Yellow
240266
241267$roles = @ (" admin" , " agent-manager" , " viewer" )
242268
@@ -256,7 +282,7 @@ foreach ($role in $roles) {
256282 }
257283}
258284
259- Write-Host " [9/9 ] Setup complete!" - ForegroundColor Green
285+ Write-Host " [10/10 ] Setup complete!" - ForegroundColor Green
260286Write-Host " "
261287Write-Host " ========================================" - ForegroundColor Cyan
262288Write-Host " Setup Summary" - ForegroundColor Cyan
@@ -266,9 +292,13 @@ Write-Host "Realm: $RealmName" -ForegroundColor White
266292Write-Host " Authority: $KeycloakUrl /realms/$RealmName " - ForegroundColor White
267293Write-Host " "
268294Write-Host " Clients configured:" - ForegroundColor White
295+ Write-Host " • mcp-server-api (confidential, JWT validation)" - ForegroundColor White
269296Write-Host " • mcp-director (public, Device Flow)" - ForegroundColor White
270297Write-Host " • mcp-web (confidential, Standard Flow)" - ForegroundColor White
271298Write-Host " "
299+ Write-Host " mcp-server-api client secret:" - ForegroundColor Yellow
300+ Write-Host " $apiSecretValue " - ForegroundColor Cyan
301+ Write-Host " "
272302Write-Host " mcp-web client secret:" - ForegroundColor Yellow
273303Write-Host " $webSecretValue " - ForegroundColor Cyan
274304Write-Host " "
@@ -291,6 +321,7 @@ Write-Host ' "Mcp": {' -ForegroundColor DarkGray
291321Write-Host ' "Auth": {' - ForegroundColor DarkGray
292322Write-Host " `" Authority`" : `" $KeycloakUrl /realms/$RealmName `" ," - ForegroundColor DarkGray
293323Write-Host ' "Audience": "mcp-server-api",' - ForegroundColor DarkGray
324+ Write-Host " `" ClientSecret`" : `" $apiSecretValue `" ," - ForegroundColor DarkGray
294325Write-Host ' "RequireHttpsMetadata": false,' - ForegroundColor DarkGray
295326Write-Host ' "DirectorClientId": "mcp-director"' - ForegroundColor DarkGray
296327Write-Host ' }' - ForegroundColor DarkGray
0 commit comments