Modern identity systems require intelligent, context-aware, and secure communication between authentication components. Model Context Protocol (MCP) enables AI agents and identity servers like Keycloak to share dynamic session context and decision boundaries. In this guide, you’ll learn how to build an MCP server for Keycloak using Quarkus, a fast Java framework for microservices, and the Goose CLI, a lightweight tool for managing MCP configurations and state.
We’ll cover:
-
What MCP is and why it matters
-
The architecture of an MCP-enabled Keycloak server
-
Bootstrapping a Quarkus MCP service
-
Integrating with Keycloak
-
Using Goose CLI to manage and test contexts
Understanding the Model Context Protocol (MCP)
Model Context Protocol (MCP) is an emerging spec designed for sharing structured interaction context across systems involving models (AI or rule-based). In the case of Keycloak, MCP enables passing dynamic identity-related information (session status, role decisions, adaptive policy hints) to LLMs or other services that evaluate or enforce access control.
Benefits of MCP with Keycloak:
-
Enables dynamic authorization based on model feedback.
-
Supports context chaining between services.
-
Makes identity context auditable and versioned.
-
Bridges the gap between AI policy agents and IAM systems.
Architecture Overview: MCP with Keycloak
Here’s a high-level view of how the system will work:
-
The Quarkus MCP server exposes MCP endpoints (e.g.,
POST /context
,GET /context/:id
) and manages contextual model state. -
Keycloak communicates with this server using a Service Provider Interface (SPI) plugin.
-
The Goose CLI interacts with the MCP server to inspect or simulate state changes.
Setting Up the Quarkus Project
Let’s create the Quarkus project that will serve MCP context data.
This sets up a Quarkus REST API project with JPA and H2 in-memory DB.
Define the MCP Context Entity
This represents a piece of contextual identity data, versioned by timestamp.
Create REST Endpoints
This API enables Keycloak and external tools to read/write model context.
Configure the MCP Quarkus Server
application.properties
Run the app:
Test it with:
Connect Keycloak to MCP (Custom SPI)
Create a Keycloak SPI plugin to push and fetch context data from the Quarkus MCP service.
Example SPI Plugin (simplified):
Package and deploy the plugin into your Keycloak providers
directory, and register it via standalone.xml
or keycloak.conf
.
Managing Contexts with Goose CLI
Install Goose CLI (or simulate using a shell script for now):
Example Commands:
Sample Output:
You can also extend Goose CLI to simulate model feedback by updating a field like status="suspended"
based on AI evaluation.
Optional: Enabling Model Feedback Loop
To support model input, add a PATCH method to your Quarkus app:
Now you can enable AI-based model agents to adjust the context dynamically, e.g., reduce privileges, flag anomalies, or escalate access.
Secure MCP Server with Keycloak Token
Use Quarkus OIDC to secure the MCP server, accepting tokens issued by your Keycloak realm.
Add extension:
Add config:
This makes sure only trusted entities (like Keycloak SPI or Goose CLI with token) can write to the MCP store.
Conclusion
Bringing together identity management, real-time context sharing, and AI feedback mechanisms is a pivotal advancement for modern digital systems. With Model Context Protocol (MCP), Keycloak becomes more than an identity provider — it becomes a dynamic, intelligent participant in broader decision-making workflows.
Using Quarkus, we built a performant and extensible MCP server with minimal setup. Its REST endpoints, backed by a persistent model, provide structured access to contextual data. Then, with Goose CLI, we injected and managed contexts directly, enabling easy testing, debugging, and simulated AI interaction.
On the Keycloak side, a custom SPI plugin let us push session and identity events into the MCP server, forming a real-time loop between IAM and model logic. The setup also accommodates token-based security, ensuring that only trusted actors contribute or consume context data.
This architecture is particularly powerful for:
-
Adaptive access control (adjust roles based on model trust)
-
Auditing user interactions across services
-
Bridging AI agents with secure systems via a protocolized interface
As context-based computing and AI decision systems become ubiquitous, MCP will emerge as a standard glue between trust, behavior, and control. And by using tools like Quarkus and Goose CLI, you can stand at the forefront of this transformation — with Keycloak acting not just as a gatekeeper, but as a contextual partner in trust orchestration.