""" MCP tool for getting Loki label values. This tool allows authorized AI agents and developers to fetch all values for a specific label name in Loki. Returns JSON in Loki API format: {"status": "success", "data": ["value1", "value2", ...]}. """ import json from typing import Any from pydantic import ValidationError as PydanticValidationError from src.models.loki_queries import LokiLabelValuesRequest from src.services.auth import get_valid_server_auth from src.services.loki_resolve import get_loki_client_for_cluster from src.tools.query_range import mcp from src.utils.errors import ( AuthorizationError, AuthServiceError, LokiAPIError, LokiTimeoutError, NetworkError, ValidationError, ) from src.utils.tool_response import format_tool_error, tool_text_response @mcp.tool() async def get_loki_label_values( label_name: str, cluster_id: str, env: str, deploy_type: str, start: str | None = None, end: str | None = None, query: str | None = None, ) -> list[dict[str, Any]]: """ Get all values for a specific label name in Loki. Returns JSON in the same format as Loki /loki/api/v1/label//values: {"status": "success", "data": ["value1", "value2", ...]}. Args: label_name: Name of the label to get values for (e.g., 'app', 'namespace', 'level') cluster_id: Cluster identifier. env: Environment name (e.g. prod, staging, dev). deploy_type: Deploy type (dedicated, premium, resource_pool, starter, essential). start: Optional start timestamp in RFC3339 format for time range end: Optional end timestamp in RFC3339 format for time range query: Optional LogQL query to filter which values are returned Returns: List containing a single dict with 'type': 'text' and 'text': Loki-format JSON Raises: Various errors for authorization, validation, network, and timeout issues Examples: # Get all values for the 'app' label label_name='app' # Get values for 'level' label in a time range label_name='level' start='2024-01-01T00:00:00Z' end='2024-01-01T23:59:59Z' # Get values for 'namespace' label filtered by a query label_name='namespace' query='{app="mysql"}' """ try: await get_valid_server_auth() try: request = LokiLabelValuesRequest( label_name=label_name, cluster_id=cluster_id.strip(), env=env.strip(), deploy_type=deploy_type.strip(), start=start, end=end, query=query, ) except (ValueError, PydanticValidationError) as e: raise ValidationError(f"Invalid Loki label values request parameters: {str(e)}") from e client, region = await get_loki_client_for_cluster( cluster_id=request.cluster_id, env=request.env, deploy_type=request.deploy_type, timeout=15.0, ) values = await client.get_label_values( label_name=request.label_name, start=request.start, end=request.end, query=request.query, ) response = {"status": "success", "data": values} return tool_text_response(json.dumps(response, ensure_ascii=False)) except ( AuthServiceError, AuthorizationError, ValidationError, NetworkError, LokiTimeoutError, LokiAPIError, ) as e: return tool_text_response(format_tool_error(e)) except Exception as e: return tool_text_response(format_tool_error(e))