logout_user
Log out all active sessions of a specific user in a Keycloak realm by providing the user ID.
Instructions
Logout all sessions for a user.
Args:
user_id: The user's ID
realm: Target realm (uses default if not specified)
Returns:
Status messageInput Schema
| Name | Required | Description | Default |
|---|---|---|---|
| user_id | Yes | ||
| realm | No |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
| result | Yes |
Implementation Reference
- src/tools/user_tools.py:231-247 (handler)The handler function for the logout_user tool that makes a POST request to Keycloak's /users/{user_id}/logout endpoint and returns a success status message.
@mcp.tool() async def logout_user(user_id: str, realm: Optional[str] = None) -> Dict[str, str]: """ Logout all sessions for a user. Args: user_id: The user's ID realm: Target realm (uses default if not specified) Returns: Status message """ await client._make_request("POST", f"/users/{user_id}/logout", realm=realm) return { "status": "success", "message": f"User {user_id} logged out from all sessions", } - src/tools/user_tools.py:231-232 (registration)The logout_user function is registered as an MCP tool using the @mcp.tool() decorator from FastMCP.
@mcp.tool() async def logout_user(user_id: str, realm: Optional[str] = None) -> Dict[str, str]: - src/tools/keycloak_client.py:59-74 (helper)The KeycloakClient._make_request helper method used by logout_user to make the authenticated API call.
async def _make_request( self, method: str, endpoint: str, data: Optional[Dict] = None, params: Optional[Dict] = None, skip_realm: bool = False, realm: Optional[str] = None, ) -> Any: """Make authenticated request to Keycloak API""" if skip_realm: url = f"{self.server_url}/auth/admin{endpoint}" else: # Use provided realm or fall back to configured realm target_realm = realm if realm is not None else self.realm_name url = f"{self.server_url}/auth/admin/realms/{target_realm}{endpoint}"