assign_client_role_to_user
Assign specific client roles to a user in Keycloak by providing user ID, client ID, and role names. Optionally specify a target realm for role assignment.
Instructions
Assign client roles to a user.
Args:
user_id: User ID
client_id: Client database ID
role_names: List of role names to assign
realm: Target realm (uses default if not specified)
Returns:
Status message
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| client_id | Yes | ||
| realm | No | ||
| role_names | Yes | ||
| user_id | Yes |
Implementation Reference
- src/tools/role_tools.py:290-323 (handler)The async handler function decorated with @mcp.tool() that implements the logic to assign client roles to a user by fetching role details and posting to Keycloak's user role mappings endpoint.@mcp.tool() async def assign_client_role_to_user( user_id: str, client_id: str, role_names: List[str], realm: Optional[str] = None ) -> Dict[str, str]: """ Assign client roles to a user. Args: user_id: User ID client_id: Client database ID role_names: List of role names to assign realm: Target realm (uses default if not specified) Returns: Status message """ # Get role representations roles = [] for role_name in role_names: role = await client._make_request( "GET", f"/clients/{client_id}/roles/{role_name}", realm=realm ) roles.append(role) await client._make_request( "POST", f"/users/{user_id}/role-mappings/clients/{client_id}", data=roles, realm=realm, ) return { "status": "assigned", "message": f"Client roles {role_names} assigned to user {user_id}", }