Skip to main content
Glama
HaithamOumerzoug

Keycloak MCP Server

assign-client-role-to-user

Assign a specific client role to a user in a Keycloak realm using the MCP server, ensuring proper access control based on defined roles and permissions.

Instructions

Assign a client role to a user

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
clientUniqueIdYes
realmYes
roleNameYes
userIdYes

Implementation Reference

  • Core handler function that executes the tool logic: parses args, lists client roles, finds the role, assigns it to the user via Keycloak admin client, and returns success message.
    public async assignClientRoleToUser(args: unknown): Promise<string> { const { realm, userId, clientUniqueId, roleName } = AssignClientRoleSchema.parse(args); const roles: RoleRepresentation[] = await this.kcAdminClient.clients.listRoles({ id: clientUniqueId, realm, }); const role: RoleRepresentation | undefined = roles.find( (r) => r.name === roleName ); if (!role || !role.id || !role.name) { throw new Error(`Role '${roleName}' not found or has no ID.`); } await this.kcAdminClient.users.addClientRoleMappings({ realm, id: userId, clientUniqueId, roles: [{ id: role.id, name: role.name }], }); return `Assigned role '${roleName}' to user ${userId} in client ${clientUniqueId}`; }
  • MCP server tool handler switch case that receives tool call and delegates to the KeycloakService method.
    case "assign-client-role-to-user": return { content: [ { type: "text", text: await keycloakService.assignClientRoleToUser(args), }, ], };
  • Zod schema used for input validation within the handler function.
    export const AssignClientRoleSchema = z.object({ realm: z.string(), userId: z.string(), clientUniqueId: z.string(), roleName: z.string(), });
  • JSON schema definition for the tool input, used in MCP tool registration.
    "assign-client-role-to-user": { type: "object", properties: { realm: { type: "string" }, userId: { type: "string" }, clientUniqueId: { type: "string" }, roleName: { type: "string" }, }, required: ["realm", "userId", "clientUniqueId", "roleName"], },
  • src/server.ts:56-60 (registration)
    Tool registration in the MCP server's listTools response.
    { name: "assign-client-role-to-user", description: "Assign a client role to a user", inputSchema: InputSchema["assign-client-role-to-user"], },

Other Tools

Related Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/HaithamOumerzoug/keycloak-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server