Successfully implemented the Parametric Collapse Pattern for the Keycloak MCP Server, consolidating 37+ individual tool methods across 7 tool classes into a single unified KeycloakTool class.
November 20, 2025
Location: src/main/java/dev/shaaf/keycloak/mcp/server/KeycloakTool.java
Key Features:
@Tool annotated method: executeKeycloakOperation()Statistics:
Content:
Sections:
Content:
Sections:
Changes:
Location: src/main/java/dev/shaaf/keycloak/mcp/server/examples/KeycloakToolExample.java
Content:
GET_USERS, GET_USER_BY_USERNAME, GET_USER_BY_ID
CREATE_USER, DELETE_USER, UPDATE_USER
GET_USER_GROUPS, ADD_USER_TO_GROUP, REMOVE_USER_FROM_GROUP
GET_USER_ROLES, ADD_ROLE_TO_USER, REMOVE_ROLE_FROM_USER
RESET_PASSWORD, SEND_VERIFICATION_EMAIL, COUNT_USERS
GET_REALMS, GET_REALM, CREATE_REALM
GET_CLIENTS, GET_CLIENT, CREATE_CLIENT, DELETE_CLIENT
GENERATE_CLIENT_SECRET
GET_CLIENT_ROLES, CREATE_CLIENT_ROLE, DELETE_CLIENT_ROLE
GET_REALM_ROLES, GET_REALM_ROLE
GET_GROUPS, GET_GROUP_MEMBERS
CREATE_GROUP, UPDATE_GROUP, DELETE_GROUP, CREATE_SUBGROUP
GET_IDENTITY_PROVIDERS, GET_IDENTITY_PROVIDER
GET_IDENTITY_PROVIDER_MAPPERS
GET_AUTHENTICATION_FLOWS, GET_AUTHENTICATION_FLOW
CREATE_AUTHENTICATION_FLOW, DELETE_AUTHENTICATION_FLOW
GET_FLOW_EXECUTIONS, UPDATE_FLOW_EXECUTION
@Tool(description = "Execute Keycloak operations...")
public String executeKeycloakOperation(
@ToolArg(description = "Operation type") KeycloakOperation operation,
@ToolArg(description = "JSON parameters") String params) {
try {
JsonNode paramsNode = mapper.readTree(params);
switch (operation) {
case CREATE_USER:
return userService.addUser(
paramsNode.get("realm").asText(),
paramsNode.get("username").asText(),
// ... more params
);
// ... more cases
}
} catch (Exception e) {
Log.error("Failed to execute operation: " + operation, e);
throw new ToolCallException("Failed: " + e.getMessage());
}
}
src/main/java/dev/shaaf/keycloak/mcp/server/KeycloakTool.java (450 lines)src/main/java/dev/shaaf/keycloak/mcp/server/examples/KeycloakToolExample.java (200 lines)parametric-collapse.md (350 lines)migration-guide.md (450 lines)implementation-summary.md (this file)README.md - Added sections about Parametric CollapseAll service layer files remain unchanged:
UserService.javaRealmService.javaClientService.javaRoleService.javaGroupService.javaIdentityProviderService.javaAuthenticationService.javaThe following files can be optionally removed (not needed anymore):
user/UserTool.javarealm/RealmTool.javaclient/ClientTool.javarole/RoleTool.javagroup/GroupTool.javaidp/IdentityProviderTool.javaauthentication/AuthenticationTool.javaRecommendation: Keep them for backward compatibility during a transition period, then deprecate and remove in a future major version.
SUCCESS - Project compiles cleanly
[INFO] BUILD SUCCESS
[INFO] Compiling 22 source files
keycloakTool.executeKeycloakOperation(
KeycloakOperation.GET_USERS,
"{\"realm\": \"quarkus\"}"
)
keycloakTool.executeKeycloakOperation(
KeycloakOperation.CREATE_USER,
"{" +
" \"realm\": \"quarkus\"," +
" \"username\": \"jdoe\"," +
" \"firstName\": \"John\"," +
" \"lastName\": \"Doe\"," +
" \"email\": \"john@example.com\"," +
" \"password\": \"secure123\"" +
"}"
)
keycloakTool.executeKeycloakOperation(
KeycloakOperation.ADD_ROLE_TO_USER,
"{" +
" \"realm\": \"quarkus\"," +
" \"userId\": \"user-id-123\"," +
" \"roleName\": \"admin\"" +
"}"
)
To test the implementation:
# Compile
mvn clean compile
# Build
mvn package
# Run
java -jar target/keycloak-mcp-server-0.2.0-runner.jar
Test with an MCP client (Goose, Claude Desktop, etc.) by:
Adding a new operation is straightforward:
// 1. Add to enum
public enum KeycloakOperation {
// ... existing operations
MY_NEW_OPERATION
}
// 2. Add to switch statement
case MY_NEW_OPERATION:
return myService.myNewMethod(
paramsNode.get("param1").asText()
);
The Parametric Collapse implementation successfully:
This implementation serves as a reference for applying the Parametric Collapse pattern to other MCP servers and API designs facing tool proliferation challenges.
Status: COMPLETE AND READY FOR USE Build: PASSING Documentation: COMPREHENSIVE Examples: PROVIDED