assign_realm_role_to_user
Assign specific realm roles to a user in Keycloak by providing user ID, role names, and optional realm. Simplifies role management for identity and access control.
Instructions
Assign realm roles to a user.
Args:
user_id: User 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 |
|---|---|---|---|
| realm | No | ||
| role_names | Yes | ||
| user_id | Yes |
Implementation Reference
- src/tools/role_tools.py:208-235 (handler)The main handler function for the 'assign_realm_role_to_user' tool. It is decorated with @mcp.tool() for registration and implements the logic by fetching role representations and assigning them to the user via Keycloak API.@mcp.tool() async def assign_realm_role_to_user( user_id: str, role_names: List[str], realm: Optional[str] = None ) -> Dict[str, str]: """ Assign realm roles to a user. Args: user_id: User 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"/roles/{role_name}", realm=realm) roles.append(role) await client._make_request( "POST", f"/users/{user_id}/role-mappings/realm", data=roles, realm=realm ) return { "status": "assigned", "message": f"Roles {role_names} assigned to user {user_id}", }