get_user
Get a Keycloak user by ID, with optional realm specification. Returns the user object.
Instructions
Get a specific user by ID.
Args:
user_id: The user's ID
realm: Target realm (uses default if not specified)
Returns:
User objectInput 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:51-63 (handler)The handler function for the 'get_user' tool. It is decorated with @mcp.tool() and makes a GET request to the Keycloak Admin API endpoint /users/{user_id} to fetch a specific user by ID.
@mcp.tool() async def get_user(user_id: str, realm: Optional[str] = None) -> Dict[str, Any]: """ Get a specific user by ID. Args: user_id: The user's ID realm: Target realm (uses default if not specified) Returns: User object """ return await client._make_request("GET", f"/users/{user_id}", realm=realm) - src/tools/user_tools.py:51-52 (registration)The tool is registered via the @mcp.tool() decorator from FastMCP. The MCP server instance is defined in src/common/server.py.
@mcp.tool() async def get_user(user_id: str, realm: Optional[str] = None) -> Dict[str, Any]: - src/tools/user_tools.py:51-62 (schema)The input schema is defined through the function signature: user_id (required str) and realm (optional str). The output schema is Dict[str, Any]. The docstring describes the parameters and return type.
@mcp.tool() async def get_user(user_id: str, realm: Optional[str] = None) -> Dict[str, Any]: """ Get a specific user by ID. Args: user_id: The user's ID realm: Target realm (uses default if not specified) Returns: User object """ - src/tools/keycloak_client.py:59-107 (helper)The helper utility that executes the actual HTTP request. The get_user handler calls client._make_request("GET", f"/users/{user_id}", realm=realm), which constructs the URL and performs the authenticated request to the Keycloak Admin API.
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}" try: client = await self._ensure_client() headers = await self._get_headers() response = await client.request( method=method, url=url, headers=headers, json=data, params=params, ) # If token expired, refresh and retry if response.status_code == 401: await self._get_token() headers = await self._get_headers() response = await client.request( method=method, url=url, headers=headers, json=data, params=params, ) response.raise_for_status() if response.content: return response.json() return None except httpx.RequestError as e: raise Exception(f"Keycloak API request failed: {str(e)}")