grafana_fetch_dashboard_variables
Retrieve dashboard template variables and their current values from Grafana to enable dynamic data filtering and visualization.
Instructions
Fetches all variables and their values from a Grafana dashboard. Retrieves dashboard template variables and their current values.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| dashboard_uid | Yes | Dashboard UID |
Implementation Reference
- The actual logic that calls the Grafana API to fetch dashboard variables.
def grafana_fetch_dashboard_variables(self, dashboard_uid: str) -> dict[str, Any]: """ Fetches all variables and their values from a Grafana dashboard. Args: dashboard_uid: Dashboard UID Returns: Dict containing dashboard variables and their values """ try: url = f"{self.__host}/api/dashboards/uid/{dashboard_uid}" logger.info(f"Fetching dashboard variables for UID: {dashboard_uid}") response = requests.get(url, headers=self.headers, verify=self.__ssl_verify, timeout=20) if response.status_code == 200: dashboard_data = response.json() dashboard = dashboard_data.get("dashboard", {}) templating = dashboard.get("templating", {}) variables = templating.get("list", []) # Extract variable information variable_details = [] for var in variables: variable_details.append( { "name": var.get("name"), "type": var.get("type"), "current_value": var.get("current", {}).get("value"), "options": var.get("options", []), "query": var.get("query"), "definition": var.get("definition"), } ) return { "status": "success", "dashboard_uid": dashboard_uid, "variables": variable_details, } - The wrapper function in the MCP server that invokes the Grafana processor.
def grafana_fetch_dashboard_variables(dashboard_uid): """Fetch all variables and their values from a Grafana dashboard""" try: grafana_processor = current_app.config.get("grafana_processor") if not grafana_processor: return { "status": "error", "message": "Grafana processor not initialized. Check configuration.", } result = grafana_processor.grafana_fetch_dashboard_variables(dashboard_uid) return result except Exception as e: logger.error(f"Error fetching dashboard variables: {e!s}") return { "status": "error", "message": f"Failed to fetch dashboard variables: {e!s}", } - src/grafana_mcp_server/mcp_server.py:474-474 (registration)Registration of the tool in the server's function mapping.
"grafana_fetch_dashboard_variables": grafana_fetch_dashboard_variables,