Skip to main content
Glama
idoyudha

mcp-keycloak

by idoyudha

assign_client_role_to_user

Assign client-specific roles to users in Keycloak for precise access control. Specify user, client, and roles to manage permissions.

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
NameRequiredDescriptionDefault
user_idYes
client_idYes
role_namesYes
realmNo

Implementation Reference

  • The handler function for the 'assign_client_role_to_user' tool, decorated with @mcp.tool() which serves as the registration. It fetches client role details and assigns them to the specified user via Keycloak's admin API.
    @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}",
        }
  • The @mcp.tool() decorator registers the function as an MCP tool.
    @mcp.tool()
  • Function signature with type hints and docstring defining the input schema and output type.
    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)

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/idoyudha/mcp-keycloak'

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