This guide covers setting up Keycloak as an OAuth2/OpenID Connect identity provider for the Solr MCP Server running in HTTP mode.
- Overview
- Prerequisites
- Quick Start
- Keycloak Setup
- Spring Boot Configuration
- Running the Server
- Testing Authentication
- Connecting an MCP Client
- Configuring a Spring AI MCP Client
- User Management Options
- GitHub Identity Provider Setup
- Role-Based Access Control (Optional)
- Troubleshooting
The Solr MCP Server supports OAuth2 authentication via JWT tokens. Keycloak acts as the authorization server, issuing tokens that the MCP server validates. This enables:
- Centralized user management
- Single Sign-On (SSO) across multiple applications
- Integration with external identity providers (GitHub, Google, LDAP, etc.)
- Fine-grained role-based access control
User MCP Client Keycloak Solr MCP Server
│ │ │ │
│─── Request Access ────►│ │ │
│ │─── Auth Request ───►│ │
│◄────────────────────── Login Page ◄──────────│ │
│─── Credentials ───────────────────────────────► │
│ │◄─── JWT Token ──────│ │
│ │─── API Request + Token ────────────────────►│
│ │ │ (validates JWT) │
│ │◄─────────────────────────── Response ────────│
│◄─── Result ────────────│ │ │
Both of these will stop the server from starting if you get them wrong, so they are worth internalising before you run anything:
- The realm must exist before
bootRun. The MCP server builds itsNimbusJwtDecodereagerly, during Spring context refresh, by fetching$OAUTH2_ISSUER_URI/.well-known/openid-configuration. If that URL does not resolve, the application context fails to refresh and the process exits. This is not a lazy, first-request failure. - Keycloak needs ~20-40s to become ready.
docker run -dreturns immediately; the realm endpoints do not exist yet. Poll before continuing.
The Quick Start below is ordered to satisfy both.
- Docker (for running Keycloak)
- Java 25 (for Solr MCP Server)
- Gradle (for building the server)
curlandjq(for the scripted setup below)
You do not need to start Solr yourself. application-http.properties sets
spring.docker.compose.enabled=true, so ./gradlew bootRun starts the Solr,
ZooKeeper, LGTM and Keycloak containers from compose.yaml before the
application context comes up.
The
httpprofile now brings its own Keycloak.compose.yamldefines akeycloakservice that importskeycloak/solr-mcp-realm.jsonon startup, so the realm, both clients and the audience mapper exist before the server asks for a token — and because the service declares a healthcheck, Spring Boot waits for it rather than failing on an unresolvable issuer. Point the server at the imported realm and start it; Spring Boot's Docker Compose support brings Keycloak up (it sits behind thehttpcompose profile, so start it by hand withdocker compose --profile http up -dif you are not usingbootRun):export PROFILES=http export OAUTH2_ISSUER_URI=http://localhost:8180/realms/solr-mcp ./gradlew bootRunThe imported realm provides
solr-mcp-service(confidential, service accounts, secretdev-only-not-a-secret) for machine-to-machine callers,solr-mcp-client(public) for MCP Inspector, andtestuser/testpassword. These are development credentials committed on purpose; a real deployment provisions its own. With this realm, skip the client-creation steps below and usesolr-mcp-service/dev-only-not-a-secretwherever a confidential client is needed.The manual walkthrough below remains the reference for what that import contains, and for setting the same thing up against an existing Keycloak. It binds its own container to the same port, 8180, so use one or the other: if you follow it, start the server with
SPRING_DOCKER_COMPOSE_ENABLED=falseso Boot does not bring up a second Keycloak.
This block is runnable end to end — copy the whole thing. It waits for Keycloak, creates the realm, client, audience mapper and test user, verifies the realm resolves, and only then starts the server.
# ── 1. Start Keycloak ────────────────────────────────────────────────────────
docker run -d --name keycloak \
-p 8180:8080 \
-e KC_BOOTSTRAP_ADMIN_USERNAME=admin \
-e KC_BOOTSTRAP_ADMIN_PASSWORD=admin \
quay.io/keycloak/keycloak:26.0 start-dev
KC=http://localhost:8180
# Must match the MCP server's canonical resource URI. Confirm it at runtime with
# curl -s http://localhost:8080/.well-known/oauth-protected-resource | jq -r .resource
MCP_AUDIENCE=http://localhost:8080/mcp
# Keycloak's container is up long before its HTTP endpoints are. Wait for them.
echo "Waiting for Keycloak..."
until curl -sf "$KC/realms/master/.well-known/openid-configuration" >/dev/null; do
sleep 2
done
echo "Keycloak ready."
# ── 2. Configure the realm, client and user ──────────────────────────────────
ADMIN_TOKEN=$(curl -s -X POST "$KC/realms/master/protocol/openid-connect/token" \
-d client_id=admin-cli -d username=admin -d password=admin \
-d grant_type=password | jq -r .access_token)
# 2a. Realm
curl -s -X POST "$KC/admin/realms" \
-H "Authorization: Bearer $ADMIN_TOKEN" -H "Content-Type: application/json" \
-d '{"realm":"solr-mcp","enabled":true}'
# 2b. Public client for testing / MCP Inspector.
#
# protocolMappers: Keycloak does not honour the RFC 8707 `resource=`
# parameter yet, so an Audience protocol mapper is the supported way to put
# the MCP server's resource URI into the token's `aud` claim. The MCP server
# runs validateAudienceClaim(true) and rejects tokens without it.
#
# directAccessGrantsEnabled: required by the password grant used in
# "Testing Authentication". It defaults to true in the admin console but to
# false over the Admin REST API, so it must be set explicitly here.
curl -s -X POST "$KC/admin/realms/solr-mcp/clients" \
-H "Authorization: Bearer $ADMIN_TOKEN" -H "Content-Type: application/json" \
-d "{
\"clientId\": \"solr-mcp-client\",
\"publicClient\": true,
\"directAccessGrantsEnabled\": true,
\"redirectUris\": [\"http://localhost:6274/*\"],
\"webOrigins\": [\"http://localhost:6274\"],
\"protocolMappers\": [{
\"name\": \"mcp-audience\",
\"protocol\": \"openid-connect\",
\"protocolMapper\": \"oidc-audience-mapper\",
\"config\": {
\"included.custom.audience\": \"$MCP_AUDIENCE\",
\"access.token.claim\": \"true\",
\"id.token.claim\": \"false\"
}
}]
}"
# 2c. Test user.
#
# firstName and lastName are REQUIRED. Keycloak 24+ enforces a declarative
# user profile in which both are mandatory; a user created without them is
# flagged VERIFY_PROFILE and every password grant fails with
# "Account is not fully set up".
curl -s -X POST "$KC/admin/realms/solr-mcp/users" \
-H "Authorization: Bearer $ADMIN_TOKEN" -H "Content-Type: application/json" \
-d '{
"username": "testuser",
"email": "test@example.com",
"firstName": "Test",
"lastName": "User",
"enabled": true,
"emailVerified": true,
"credentials": [
{"type": "password", "value": "testpassword", "temporary": false}
]
}'
# ── 3. Verify the realm resolves BEFORE starting the server ──────────────────
# The server fails to boot if this is not a 200. See "Two ordering constraints".
if curl -sf "$KC/realms/solr-mcp/.well-known/openid-configuration" >/dev/null; then
echo "Realm ready."
else
echo "Realm NOT ready — do not start the server yet."
fi
# ── 4. Run the Solr MCP Server ───────────────────────────────────────────────
export PROFILES=http
export HTTP_SECURITY_ENABLED=true
export OAUTH2_ISSUER_URI=http://localhost:8180/realms/solr-mcp
./gradlew bootRunTo tear the whole thing down again:
docker rm -f keycloak
docker compose downThe Quick Start automates everything in this section. The steps below are the equivalent admin-console walkthrough, for when you want to understand or adjust what the script did.
Development Mode (Docker):
docker run -d --name keycloak \
-p 8180:8080 \
-e KC_BOOTSTRAP_ADMIN_USERNAME=admin \
-e KC_BOOTSTRAP_ADMIN_PASSWORD=admin \
quay.io/keycloak/keycloak:26.0 start-devAccess the admin console at http://localhost:8180 and log in with admin/admin.
Keycloak takes roughly 20-40 seconds to start serving. docker run -d returns
before that, so wait for the endpoint rather than assuming it is up:
until curl -sf http://localhost:8180/realms/master/.well-known/openid-configuration >/dev/null; do
sleep 2
doneProduction Mode:
For production deployments, use a proper database backend and TLS:
docker run -d --name keycloak \
-p 8443:8443 \
-e KC_BOOTSTRAP_ADMIN_USERNAME=admin \
-e KC_BOOTSTRAP_ADMIN_PASSWORD=<secure-password> \
-e KC_DB=postgres \
-e KC_DB_URL=jdbc:postgresql://db-host:5432/keycloak \
-e KC_DB_USERNAME=keycloak \
-e KC_DB_PASSWORD=<db-password> \
-e KC_HOSTNAME=keycloak.example.com \
quay.io/keycloak/keycloak:26.0 start- Log into Keycloak Admin Console:
http://localhost:8180/admin - Click the dropdown in the top-left (shows "master")
- Click Create realm
- Realm name:
solr-mcp - Click Create
Create the realm before starting the MCP server. The server resolves the issuer eagerly at startup and will not boot against a realm that does not exist. See Troubleshooting.
You need at least one client for applications to authenticate against.
The MCP server itself does not need a Keycloak client. It is a pure resource server:
HttpSecurityConfigurationconsumes only the issuer URI and validates tokens against the realm's JWKS endpoint. There is no client-id or client-secret anywhere in the server configuration. Create clients only for the applications that call it.
- Navigate to Clients → Create client
- Configure:
- Client ID:
solr-mcp-client - Client type:
OpenID Connect
- Client ID:
- Click Next
- Client authentication: OFF (public client)
- Authentication flow: leave Direct access grants enabled — the password grant used in Testing Authentication needs it
- Click Next
- Configure access settings:
- Valid redirect URIs:
http://localhost:6274/*,http://localhost:* - Web origins:
http://localhost:6274
- Valid redirect URIs:
- Click Save
Then add the audience mapper described next — without it, tokens from this client are rejected.
The MCP server runs with validateAudienceClaim(true) and requires every token
to carry an aud claim matching its canonical resource URI. Confirm that URI
from the running server:
curl -s http://localhost:8080/.well-known/oauth-protected-resource | jq -r .resource
# http://localhost:8080/mcpThat value is derived from the URL the client calls — scheme, host and port
plus /mcp — not from a fixed setting. A token whose aud is
http://localhost:8080/mcp is rejected with The aud claim is not valid when
the client dials http://127.0.0.1:8080/mcp, and a server reached through a
public hostname expects that hostname. Use the same spelling in the mapper and
in every client.
Keycloak does not honour the RFC 8707 resource= parameter yet
(keycloak#41526), so the
supported approach is an Audience protocol mapper:
- Navigate to Clients →
solr-mcp-client→ Client scopes - Click the dedicated scope (
solr-mcp-client-dedicated) - Add mapper → By configuration → Audience
- Configure:
| Field | Value |
|---|---|
| Name | mcp-audience |
| Included Custom Audience | http://localhost:8080/mcp |
| Add to access token | ON |
- Click Save
Verify the claim landed:
TOKEN=$(curl -s -X POST "http://localhost:8180/realms/solr-mcp/protocol/openid-connect/token" \
-d client_id=solr-mcp-client -d username=testuser \
-d password=testpassword -d grant_type=password | jq -r .access_token)
jwt_payload() {
python3 -c "import sys,base64,json;p=sys.argv[1].split('.')[1];p+='='*(-len(p)%4);\
print(json.dumps(json.loads(base64.urlsafe_b64decode(p))))" "$1"
}
jwt_payload "$TOKEN" | jq .aud
# [ "http://localhost:8080/mcp", "account" ]Decode with
base64urlsemantics and re-pad, as above. The commoncut -d. -f2 | base64 -done-liner fails on real Keycloak tokens — JWT payloads are unpadded base64url, sobase64 -derrors out and, with2>/dev/null, silently feeds nothing tojq. The resultingjq: parse errorlooks like a malformed token when the token is fine.
If aud is only "account", the mapper is not applied and every request will
fail with 401 ... "The aud claim is not valid".
See http.md for the
equivalent setup on Auth0 and Okta.
- Navigate to Users → Add user
- Configure:
- Username:
testuser - Email:
test@example.com - Email verified: ON
- First name:
Test - Last name:
User
- Username:
- Click Create
- Go to the Credentials tab
- Click Set password
- Enter password and disable Temporary
- Click Save
First and last name are required. Keycloak's default user profile marks them mandatory, and a user missing either one cannot obtain a token at all — the token endpoint returns
{"error":"invalid_grant","error_description":"Account is not fully set up"}, which does not obviously point at the missing name fields.Step 6 matters for the same reason. Leaving Temporary ON attaches an
UPDATE_PASSWORDrequired action, which fails the token request with that same message. The Troubleshooting section shows how to tell the two apart.
The Solr MCP Server is pre-configured to work with any OAuth2/OIDC provider,
and no file edits are required — the environment variables in the Quick
Start are enough. For reference, these are the relevant entries in
application-http.properties:
# Security toggle — HTTP mode is secured by default.
http.security.enabled=${HTTP_SECURITY_ENABLED:true}
# OAuth2 issuer. Format for Keycloak: https://<keycloak-host>/realms/<realm-name>
# The default is deliberately EMPTY: with no issuer set, the filter chain still
# gates every non-permitAll endpoint (401/403) instead of crashing at startup on
# an unreachable placeholder URL.
spring.security.oauth2.resourceserver.jwt.issuer-uri=${OAUTH2_ISSUER_URI:}No code changes are required — HttpSecurityConfiguration handles JWT
validation by discovering Keycloak's JWKS endpoint from the issuer URI.
Note the consequence of that discovery being eager: once OAUTH2_ISSUER_URI
is set to a non-empty value, it must be resolvable at startup. An empty
value is safe; an unreachable one is fatal.
With Security Enabled:
export PROFILES=http
export HTTP_SECURITY_ENABLED=true
export OAUTH2_ISSUER_URI=http://localhost:8180/realms/solr-mcp
./gradlew bootRunWithout Security (Development Only):
export PROFILES=http
export HTTP_SECURITY_ENABLED=false
./gradlew bootRunThese
exports persist in your shell and are inherited by the Gradle daemon. If a later./gradlew buildbehaves oddly,unsetthem and run./gradlew --stopfirst.
Using Resource Owner Password Grant (for testing):
curl -X POST "http://localhost:8180/realms/solr-mcp/protocol/openid-connect/token" \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "client_id=solr-mcp-client" \
-d "username=testuser" \
-d "password=testpassword" \
-d "grant_type=password"Response:
{
"access_token": "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9...",
"expires_in": 300,
"refresh_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
"token_type": "Bearer"
}# Store token in variable
TOKEN=$(curl -s -X POST "http://localhost:8180/realms/solr-mcp/protocol/openid-connect/token" \
-d "client_id=solr-mcp-client" \
-d "username=testuser" \
-d "password=testpassword" \
-d "grant_type=password" | jq -r '.access_token')
# List the available tools
curl -X POST http://localhost:8080/mcp \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-H "Accept: application/json, text/event-stream" \
-d '{"jsonrpc":"2.0","method":"tools/list","id":1}'The Accept header is required — the MCP Streamable HTTP transport negotiates
between application/json and text/event-stream.
/mcp is permitAll() at the HTTP layer (authorization is enforced per tool),
so it is not the best probe for token validity. Use a protected actuator
endpoint instead:
curl -s -o /dev/null -w "%{http_code}\n" \
-H "Authorization: Bearer $TOKEN" http://localhost:8080/actuator/metrics
# 200 → token valid (signature, issuer, expiry and audience all check out)
# 401 → inspect the WWW-Authenticate header for the reasonTo see why a token was rejected:
curl -s -i -H "Authorization: Bearer $TOKEN" http://localhost:8080/actuator/metrics \
| grep -i www-authenticateThe curl calls above prove the server; this connects a real client. One fact
shapes the setup: the server does not answer an anonymous /mcp request with
401 (/mcp is permitAll, and @PreAuthorize denies inside each tool), and
MCP clients start their OAuth flow only on a 401. A client given just the URL
shows the server as connected, lists every tool, and gets Access Denied from
every call — no browser opens. So pass the token yourself.
Keycloak's default access-token lifetime is 300 s, which is shorter than most
trial sessions. Raise it on the realm before handing out tokens (one hour here;
$ADMIN_TOKEN is from the Quick Start and itself expires after 300 s, so
re-request it if the call returns 401). The realm imported by compose.yaml
uses the same admin login, client and test user, so the commands below work
against it too:
KC=http://localhost:8180
ADMIN_TOKEN=$(curl -s -X POST "$KC/realms/master/protocol/openid-connect/token" \
-d client_id=admin-cli -d username=admin -d password=admin \
-d grant_type=password | jq -r .access_token)
curl -s -o /dev/null -w "%{http_code}\n" -X PUT "$KC/admin/realms/solr-mcp" \
-H "Authorization: Bearer $ADMIN_TOKEN" -H "Content-Type: application/json" \
-d '{"accessTokenLifespan":3600}'
# 204Then obtain a token and hand it to the client:
TOKEN=$(curl -s -X POST "$KC/realms/solr-mcp/protocol/openid-connect/token" \
-d client_id=solr-mcp-client -d username=testuser \
-d password=testpassword -d grant_type=password | jq -r .access_token)
# Claude Code
claude mcp add --transport http solr-mcp http://localhost:8080/mcp \
--header "Authorization: Bearer $TOKEN"
claude mcp list # solr-mcp: http://localhost:8080/mcp (HTTP) - ✔ Connected
# MCP Inspector, CLI — one tool call, no browser
npx @modelcontextprotocol/inspector --cli http://localhost:8080/mcp --transport http \
--header "Authorization: Bearer $TOKEN" --method tools/call --tool-name list-collections
# MCP Inspector, web UI — the header applies to the ad-hoc server it opens with
npx @modelcontextprotocol/inspector --server-url http://localhost:8080/mcp --transport http \
--header "Authorization: Bearer $TOKEN"Note that claude mcp list reports ✔ Connected with or without the header —
the handshake is anonymous. The test that matters is a tool call: ask Claude
Code to run list-collections, and expect the collection names, not
Access Denied.
When the token expires, calls fail with 401 and Claude Code reports the
connection as failed (it does not re-authenticate a server whose
Authorization header you configured). Re-run claude mcp add with a fresh
token. The other clients take the same header — see
the table in http.md
for VS Code, Cursor, Zed and mcp-remote.
The Quick Start's solr-mcp-client is a public client driven by the password
grant — fine for curl and the MCP Inspector, wrong for an application. A
Spring AI app calling this server has no interactive user, so it should
authenticate as itself with the client credentials grant.
Spring AI client app Keycloak Solr MCP Server
│ │ │
│── client_credentials ───►│ │
│◄──── JWT (aud=.../mcp) ──│ │
│ │
│── POST /mcp + Authorization: Bearer <jwt> ──────────────►│
│ (JWKS validation) │
│◄──────────────── tools/list │ tools/call ────────────────│
curl -s -X POST "$KC/admin/realms/solr-mcp/clients" \
-H "Authorization: Bearer $ADMIN_TOKEN" -H "Content-Type: application/json" \
-d "{
\"clientId\": \"spring-ai-app\",
\"publicClient\": false,
\"serviceAccountsEnabled\": true,
\"standardFlowEnabled\": false,
\"protocolMappers\": [{
\"name\": \"mcp-audience\",
\"protocol\": \"openid-connect\",
\"protocolMapper\": \"oidc-audience-mapper\",
\"config\": {
\"included.custom.audience\": \"$MCP_AUDIENCE\",
\"access.token.claim\": \"true\",
\"id.token.claim\": \"false\"
}
}]
}"The audience mapper is not optional here either — a service-account token
without it is rejected with The aud claim is not valid, exactly like a user
token. The generated secret is at Clients → spring-ai-app →
Credentials in the console, or over the Admin API:
CLIENT_UUID=$(curl -s "$KC/admin/realms/solr-mcp/clients?clientId=spring-ai-app" \
-H "Authorization: Bearer $ADMIN_TOKEN" | jq -r '.[0].id')
CLIENT_SECRET=$(curl -s "$KC/admin/realms/solr-mcp/clients/$CLIENT_UUID/client-secret" \
-H "Authorization: Bearer $ADMIN_TOKEN" | jq -r '.value')Requesting a token by hand — the same client_credentials grant the Spring
client will use — confirms the registration before any application code is
involved, and populates the $TOKEN that step 4 checks:
TOKEN=$(curl -s -X POST "$KC/realms/solr-mcp/protocol/openid-connect/token" \
-d grant_type=client_credentials \
-d client_id=spring-ai-app \
-d "client_secret=$CLIENT_SECRET" | jq -r .access_token)
jwt_payload "$TOKEN" | jq .aud
# [ "http://localhost:8080/mcp", "account" ]If aud is only "account", the protocolMappers block above did not take —
re-check it before moving on, because the failure surfaces much later as a
401 from the transport.
Both tokens expire in 300s by default, $ADMIN_TOKEN included, and carrying
this $TOKEN as far as step 4 will usually outlive it. An expired
token is rejected with the same 401 as a misconfigured one, so read the reason
before suspecting the mapper:
curl -s -i -H "Authorization: Bearer $TOKEN" http://localhost:8080/actuator/metrics \
| grep -i www-authenticate
# ... error_description="... Jwt expired at ..." -> just request a new tokenAdd spring-ai-starter-mcp-client to the client application, then:
spring.ai.mcp.client.streamable-http.connections.solr.url=http://localhost:8080
spring.ai.mcp.client.streamable-http.connections.solr.endpoint=/mcp
spring.security.oauth2.client.registration.solr-mcp.provider=keycloak
spring.security.oauth2.client.registration.solr-mcp.client-id=spring-ai-app
spring.security.oauth2.client.registration.solr-mcp.client-secret=${KC_CLIENT_SECRET}
spring.security.oauth2.client.registration.solr-mcp.authorization-grant-type=client_credentials
spring.security.oauth2.client.provider.keycloak.issuer-uri=http://localhost:8180/realms/solr-mcpTwo things that bite:
endpointalready defaults to/mcp, sourlmust be the bare origin. Settingurl=http://localhost:8080/mcpproduces requests to/mcp/mcp.- The prefix is
spring.ai.mcp.client.streamable-http, not the olderspring.ai.mcp.client.sse. SSE is the deprecated transport; this server speaks Streamable HTTP.
There is no property for a bearer token. The supported hook is an
McpSyncHttpClientRequestCustomizer bean —
StreamableHttpHttpClientTransportAutoConfiguration takes one as an
ObjectProvider, so simply declaring the bean wires it in.
@Bean
OAuth2AuthorizedClientManager authorizedClientManager(
ClientRegistrationRepository registrations,
OAuth2AuthorizedClientService clients) {
var manager = new AuthorizedClientServiceOAuth2AuthorizedClientManager(registrations, clients);
manager.setAuthorizedClientProvider(
OAuth2AuthorizedClientProviderBuilder.builder().clientCredentials().build());
return manager;
}
@Bean
McpSyncHttpClientRequestCustomizer bearerTokenCustomizer(OAuth2AuthorizedClientManager manager) {
return (builder, method, uri, body, context) -> {
OAuth2AuthorizedClient client = manager.authorize(
OAuth2AuthorizeRequest.withClientRegistrationId("solr-mcp")
.principal("spring-ai-app")
.build());
builder.header("Authorization", "Bearer " + client.getAccessToken().getTokenValue());
};
}AuthorizedClientServiceOAuth2AuthorizedClientManager is the right manager for
a headless caller — the servlet-oriented DefaultOAuth2AuthorizedClientManager
expects an active HttpServletRequest. Calling authorize(...) per request
looks wasteful but is not: the manager caches the token and only re-fetches once
it is near expiry. Do not hand-roll a cache — Keycloak's default access-token
lifetime is 300s, and a naive cache without a refresh margin produces
intermittent 401s that are painful to diagnose.
If your Spring AI application is itself a resource server acting on behalf of an end user, propagating that user's identity (via Keycloak token exchange) is what the MCP authorization specification actually calls for, and preserves the identity chain that client credentials collapses. That is a larger setup and is not covered here.
A gated tool call is the end-to-end check — it exercises the token, the audience claim and method security together:
curl -s -X POST http://localhost:8080/mcp \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-H "Accept: application/json, text/event-stream" \
-d '{"jsonrpc":"2.0","method":"tools/call","id":1,
"params":{"name":"check-health","arguments":{"collection":"films"}}}'A correctly configured client gets the tool result. Dropping the
Authorization header returns {"content":[{"text":"Access Denied"}],"isError":true}
— that contrast is what confirms the gate is live rather than silently open.
If you see
Access Deniedwith a token, check that$TOKENis actually populated before blaming the server. An unset variable sends the literal headerBearer null, which is indistinguishable from sending nothing — andjq -r .access_tokenwrites the stringnullinto the variable when the token request itself failed.
Keycloak provides multiple ways to manage users beyond manual creation.
As described above, create users via Users → Add user in the admin console. Remember to set first and last name.
Allow users to authenticate via external identity providers. See GitHub Identity Provider Setup for a detailed example.
Supported providers include:
- GitHub
- Microsoft/Azure AD
- Twitter/X
- Any SAML 2.0 or OIDC provider
Allow users to create their own accounts:
- Navigate to Realm settings → Login tab
- Enable User registration
- Optionally enable:
- Email verification
- Terms and conditions
- reCAPTCHA
Programmatically create users:
# Get admin token
TOKEN=$(curl -s -X POST "http://localhost:8180/realms/master/protocol/openid-connect/token" \
-d "client_id=admin-cli" \
-d "username=admin" \
-d "password=admin" \
-d "grant_type=password" | jq -r '.access_token')
# Create user
curl -X POST "http://localhost:8180/admin/realms/solr-mcp/users" \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d '{
"username": "newuser",
"email": "newuser@example.com",
"firstName": "New",
"lastName": "User",
"enabled": true,
"emailVerified": true,
"credentials": [{
"type": "password",
"value": "temppassword",
"temporary": true
}]
}'
"temporary": trueis the right default when provisioning a real user — it forces a password change on first login. It also attaches anUPDATE_PASSWORDrequired action, so this account cannot obtain a token via the password grant until that is resolved through the browser login flow. Use"temporary": falsefor accounts you intend to drive from scripts or tests, and populatefirstName/lastNamefor the same reason.
Bulk import users during realm setup:
{
"realm": "solr-mcp",
"users": [
{
"username": "user1",
"email": "user1@example.com",
"firstName": "User",
"lastName": "One",
"enabled": true,
"credentials": [{"type": "password", "value": "pass123"}],
"realmRoles": ["user", "solr-query"]
},
{
"username": "user2",
"email": "user2@example.com",
"firstName": "User",
"lastName": "Two",
"enabled": true,
"credentials": [{"type": "password", "value": "pass456"}],
"realmRoles": ["admin"]
}
]
}Import via CLI:
/opt/keycloak/bin/kc.sh import --file realm-export.jsonFor databases or custom backends, implement Keycloak's User Storage SPI to authenticate against your existing user database without migration.
-
Go to GitHub → Settings → Developer settings → OAuth Apps → New OAuth App
Direct link: https://github.com/settings/applications/new
-
Fill in the form:
| Field | Value |
|---|---|
| Application name | Solr MCP Server |
| Homepage URL | http://localhost:8180 |
| Authorization callback URL | http://localhost:8180/realms/solr-mcp/broker/github/endpoint |
Note: The callback URL format is:
https://<keycloak-host>/realms/<realm-name>/broker/github/endpoint
- Click Register application
- Note the Client ID and generate a Client Secret
- Log into Keycloak Admin Console
- Select your realm (
solr-mcp) - Navigate to Identity Providers → Add provider → GitHub
- Configure:
| Field | Value |
|---|---|
| Client ID | <from GitHub> |
| Client Secret | <from GitHub> |
| Default Scopes | user:email (optional) |
- Click Save
- Open:
http://localhost:8180/realms/solr-mcp/account - Click the GitHub button
- Authorize on GitHub
- You're logged into Keycloak with your GitHub account
Add mappers to import GitHub profile data:
- Navigate to Identity Providers → GitHub → Mappers
- Click Add mapper
- Example configurations:
| Name | Mapper Type | Claim | User Attribute |
|---|---|---|---|
| GitHub Username | Attribute Importer | login |
github_username |
| GitHub Avatar | Attribute Importer | avatar_url |
avatar |
| GitHub Email | Attribute Importer | email |
email |
Automatically assign roles to GitHub users:
- Navigate to Identity Providers → GitHub → Mappers
- Click Add mapper
- Configure:
- Mapper Type:
Hardcoded Role - Role: Select a role (e.g.,
solr-query)
- Mapper Type:
The shipped configuration authorizes with @PreAuthorize("isAuthenticated()")
on each @McpTool method. To authorize on Keycloak roles instead, you need a
converter that reads them out of the JWT.
- Navigate to Realm roles → Create role
- Create roles like
admin,solr-query,solr-admin - Assign roles to users via Users → select user → Role mappings
| Location | Claim Path | Use Case |
|---|---|---|
| Realm roles | realm_access.roles |
Global roles across all clients |
| Client roles | resource_access.<client-id>.roles |
Roles specific to a client |
Keycloak nests realm roles under realm_access.roles, which
JwtGrantedAuthoritiesConverter cannot read directly — it expects a top-level
claim holding a list. Extract the nested list yourself:
import java.util.List;
import java.util.Map;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationConverter;
@Bean
@ConditionalOnProperty(name = "http.security.enabled", havingValue = "true", matchIfMissing = true)
public JwtAuthenticationConverter jwtAuthenticationConverter() {
JwtAuthenticationConverter converter = new JwtAuthenticationConverter();
converter.setJwtGrantedAuthoritiesConverter(jwt -> {
Map<String, Object> realmAccess = jwt.getClaimAsMap("realm_access");
if (realmAccess == null || realmAccess.get("roles") == null) {
return List.of();
}
@SuppressWarnings("unchecked")
List<String> roles = (List<String>) realmAccess.get("roles");
return roles.stream()
.map(role -> (GrantedAuthority) new SimpleGrantedAuthority("ROLE_" + role))
.toList();
});
return converter;
}This is a sketch, not shipped configuration. The converter above is correct for reading Keycloak's nested
realm_access.roles, but wiring it in depends on how the JWT decoder is built. The shipped chain constructs its decoder throughMcpServerOAuth2Configurer, so verify that a standaloneJwtAuthenticationConverterbean is actually picked up in your build before relying on role checks — and add a test that asserts a role-gated tool call succeeds with a role-bearing token and fails without one.
@PreAuthorize("hasRole('admin')")
public String deleteCollection(String name) { ... }
@PreAuthorize("hasRole('solr-query')")
public String executeQuery(String collection, String query) { ... }
@PreAuthorize("hasAnyRole('solr-query', 'solr-admin')")
public String getSchema(String collection) { ... }Do not copy an older filter-chain snippet over the shipped one. The current chain in
HttpSecurityConfigurationauthenticates/actuator/**(only/actuator/healthis anonymous) and setsresourcePath("/mcp")plusvalidateAudienceClaim(true). Replacing it with a version that permits all actuator paths exposesloggers,sbomandmetricsanonymously, and droppingvalidateAudienceClaimreintroduces CWE-345. Seehttp.mdfor the authoritative version.
This is the most common failure. HTTP mode logs to the console through the
http profile's appender in logback-spring.xml, so the real exception is
normally in the Gradle output just above the summary:
> Task :bootRun FAILED
Execution failed for task ':bootRun'.
> Process 'command '.../bin/java'' finished with non-zero exit value 1
If there is genuinely no application output, something has disabled logging
(for example a stray logback.xml on the classpath, which
LoggingConfigurationTest guards against); check that first.
The usual cause is that the realm does not exist yet:
Error creating bean with name 'securityFilterChain' ...
Unable to resolve the Configuration with the provided Issuer of
"http://localhost:8180/realms/solr-mcp"
Fix by confirming the realm resolves before starting the server:
curl -sf http://localhost:8180/realms/solr-mcp/.well-known/openid-configuration >/dev/null \
&& echo OK || echo "realm missing or Keycloak not ready""Invalid token" or 401 Unauthorized:
- Read the
WWW-Authenticateresponse header — it names the exact failure (The aud claim is not valid, expired, bad signature, ...) - Verify
OAUTH2_ISSUER_URImatches your Keycloak realm URL exactly - Check that the token hasn't expired (default lifetime is 300s)
- Ensure Keycloak is accessible from the MCP server
401 with "The aud claim is not valid":
- The Audience protocol mapper is missing or misconfigured. See Configuring the Audience Claim
- The mapper's Included Custom Audience must equal the value returned by
curl -s http://localhost:8080/.well-known/oauth-protected-resource | jq -r .resource
{"error":"invalid_grant","error_description":"Account is not fully set up"}:
The account is not in a state Keycloak will issue a token for. The password grant has
no browser leg, so there is nowhere to render the "complete your profile" or "update
your password" page — Keycloak rejects the whole grant instead of prompting. The same
account signs in fine through a browser flow, which is why this looks like it only
breaks curl.
Two different causes produce this identical message:
- A required user-profile field is blank — usually first name or last name,
both of which are
requiredin Keycloak's default user profile. Fill them in under Users → select user → Details. - The account has a pending required action — most often
UPDATE_PASSWORD, added automatically whenever a password is set with Temporary ON.
Check both at once:
ADMIN=$(curl -s -X POST "http://localhost:8180/realms/master/protocol/openid-connect/token" \
-d "client_id=admin-cli" -d "username=admin" -d "password=admin" \
-d "grant_type=password" | jq -r '.access_token')
curl -s -H "Authorization: Bearer $ADMIN" \
"http://localhost:8180/admin/realms/solr-mcp/users?username=testuser&exact=true" \
| jq -c '.[] | {username, firstName, lastName, emailVerified, requiredActions}'A ready-to-use account has both names populated and an empty requiredActions:
{"username":"testuser","firstName":"Test","lastName":"User","emailVerified":true,"requiredActions":[]}Required actions and what they mean:
| Required action | Usual cause | Fix |
|---|---|---|
UPDATE_PASSWORD |
Password was set with Temporary ON | Re-set the password with Temporary OFF |
VERIFY_EMAIL |
Realm has email verification enabled | Set Email verified ON for the user |
UPDATE_PROFILE |
A required profile field is blank | Fill in the missing fields |
CONFIGURE_TOTP |
OTP required by the realm or a policy | Enroll OTP via the browser flow, or drop the requirement |
Clear pending actions from the console (Users → select user → Details → Required user actions → remove → Save), or over the REST API:
USER_ID=$(curl -s -H "Authorization: Bearer $ADMIN" \
"http://localhost:8180/admin/realms/solr-mcp/users?username=testuser&exact=true" | jq -r '.[0].id')
curl -X PUT "http://localhost:8180/admin/realms/solr-mcp/users/$USER_ID" \
-H "Authorization: Bearer $ADMIN" -H "Content-Type: application/json" \
-d '{"requiredActions":[]}'Clearing an action does not change the password — the credential you already set stays valid. Do this for test accounts only; real users should resolve the action through the browser login flow.
A tool call returns "Access Denied" instead of 401:
Expected when no token is sent. /mcp is permitted at the HTTP layer and
authorization is enforced per tool by @PreAuthorize, so an anonymous tools/call
comes back as a JSON-RPC error with isError: true rather than an HTTP 401. An
anonymous tools/list succeeding is also expected.
unauthorized_client or Client not allowed for direct access grants:
- Enable Direct access grants on
solr-mcp-client. Clients created through the Admin REST API have it off by default
"Unable to resolve issuer":
- Keycloak must be running and accessible
- Check the issuer URL:
http://localhost:8180/realms/solr-mcp/.well-known/openid-configuration
CORS errors with MCP Inspector:
- Ensure Web origins are configured in your Keycloak client
- Add
http://localhost:6274to Web origins - The server's own allowlist is
MCP_CORS_ALLOWED_ORIGINS(defaults tohttp://localhost:6274,http://127.0.0.1:6274)
Token doesn't contain roles:
- Verify the user has roles assigned in Keycloak
- Check the token contents at https://jwt.io
- Ensure you're reading the correct claim path (
realm_access.roles)
Inspect a JWT token:
# Decode token (without verification). Handles unpadded base64url correctly —
# see "Configuring the Audience Claim" for why `base64 -d` alone does not.
jwt_payload() {
python3 -c "import sys,base64,json;p=sys.argv[1].split('.')[1];p+='='*(-len(p)%4);\
print(json.dumps(json.loads(base64.urlsafe_b64decode(p))))" "$1"
}
jwt_payload "$TOKEN" | jqCheck the audience claim specifically:
jwt_payload "$TOKEN" | jq .audCheck Keycloak OpenID configuration:
curl http://localhost:8180/realms/solr-mcp/.well-known/openid-configuration | jqCheck what resource URI the MCP server expects:
curl -s http://localhost:8080/.well-known/oauth-protected-resource | jqCheck whether an account can obtain a token:
curl -s -H "Authorization: Bearer $ADMIN" \
"http://localhost:8180/admin/realms/solr-mcp/users?username=testuser&exact=true" \
| jq -c '.[] | {firstName, lastName, requiredActions}'
# names populated + requiredActions [] = good to goView Keycloak logs:
docker logs -f keycloak