|
| 1 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +# or more contributor license agreements. See the NOTICE file |
| 3 | +# distributed with this work for additional information |
| 4 | +# regarding copyright ownership. The ASF licenses this file |
| 5 | +# to you under the Apache License, Version 2.0 (the |
| 6 | +# "License"); you may not use this file except in compliance |
| 7 | +# with the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | + |
| 18 | +""" |
| 19 | +MCP tool: get_chart_type_schema |
| 20 | +""" |
| 21 | + |
| 22 | +from __future__ import annotations |
| 23 | + |
| 24 | +import logging |
| 25 | +from typing import Any, Dict |
| 26 | + |
| 27 | +from pydantic import TypeAdapter |
| 28 | +from superset_core.mcp.decorators import tool, ToolAnnotations |
| 29 | + |
| 30 | +from superset.mcp_service.chart.schemas import ( |
| 31 | + BigNumberChartConfig, |
| 32 | + HandlebarsChartConfig, |
| 33 | + MixedTimeseriesChartConfig, |
| 34 | + PieChartConfig, |
| 35 | + PivotTableChartConfig, |
| 36 | + TableChartConfig, |
| 37 | + XYChartConfig, |
| 38 | +) |
| 39 | + |
| 40 | +logger = logging.getLogger(__name__) |
| 41 | + |
| 42 | +# Module-level TypeAdapters — one per chart type, compiled once. |
| 43 | +_CHART_TYPE_ADAPTERS: Dict[str, TypeAdapter[Any]] = { |
| 44 | + "xy": TypeAdapter(XYChartConfig), |
| 45 | + "table": TypeAdapter(TableChartConfig), |
| 46 | + "pie": TypeAdapter(PieChartConfig), |
| 47 | + "pivot_table": TypeAdapter(PivotTableChartConfig), |
| 48 | + "mixed_timeseries": TypeAdapter(MixedTimeseriesChartConfig), |
| 49 | + "handlebars": TypeAdapter(HandlebarsChartConfig), |
| 50 | + "big_number": TypeAdapter(BigNumberChartConfig), |
| 51 | +} |
| 52 | + |
| 53 | +VALID_CHART_TYPES = sorted(_CHART_TYPE_ADAPTERS.keys()) |
| 54 | + |
| 55 | +# Per-type examples — lightweight inline examples for each chart type. |
| 56 | +_CHART_EXAMPLES: Dict[str, list[Dict[str, Any]]] = { |
| 57 | + "xy": [ |
| 58 | + { |
| 59 | + "chart_type": "xy", |
| 60 | + "kind": "line", |
| 61 | + "x": {"name": "order_date"}, |
| 62 | + "y": [{"name": "revenue", "aggregate": "SUM"}], |
| 63 | + "time_grain": "P1D", |
| 64 | + }, |
| 65 | + { |
| 66 | + "chart_type": "xy", |
| 67 | + "kind": "bar", |
| 68 | + "x": {"name": "category"}, |
| 69 | + "y": [{"name": "sales", "aggregate": "SUM"}], |
| 70 | + }, |
| 71 | + ], |
| 72 | + "table": [ |
| 73 | + { |
| 74 | + "chart_type": "table", |
| 75 | + "columns": [ |
| 76 | + {"name": "customer_name"}, |
| 77 | + {"name": "revenue", "aggregate": "SUM"}, |
| 78 | + ], |
| 79 | + }, |
| 80 | + ], |
| 81 | + "pie": [ |
| 82 | + { |
| 83 | + "chart_type": "pie", |
| 84 | + "dimension": {"name": "region"}, |
| 85 | + "metric": {"name": "revenue", "aggregate": "SUM"}, |
| 86 | + }, |
| 87 | + ], |
| 88 | + "pivot_table": [ |
| 89 | + { |
| 90 | + "chart_type": "pivot_table", |
| 91 | + "rows": [{"name": "region"}], |
| 92 | + "metrics": [{"name": "revenue", "aggregate": "SUM"}], |
| 93 | + "columns": [{"name": "quarter"}], |
| 94 | + }, |
| 95 | + ], |
| 96 | + "mixed_timeseries": [ |
| 97 | + { |
| 98 | + "chart_type": "mixed_timeseries", |
| 99 | + "x": {"name": "order_date"}, |
| 100 | + "y": [{"name": "revenue", "aggregate": "SUM"}], |
| 101 | + "y_secondary": [{"name": "orders", "aggregate": "COUNT"}], |
| 102 | + "time_grain": "P1M", |
| 103 | + }, |
| 104 | + ], |
| 105 | + "handlebars": [ |
| 106 | + { |
| 107 | + "chart_type": "handlebars", |
| 108 | + "query_mode": "raw", |
| 109 | + "columns": [{"name": "customer_name"}, {"name": "email"}], |
| 110 | + "handlebars_template": "{{#each data}}<p>{{customer_name}}</p>{{/each}}", |
| 111 | + }, |
| 112 | + ], |
| 113 | + "big_number": [ |
| 114 | + { |
| 115 | + "chart_type": "big_number", |
| 116 | + "metric": {"name": "revenue", "aggregate": "SUM"}, |
| 117 | + }, |
| 118 | + ], |
| 119 | +} |
| 120 | + |
| 121 | + |
| 122 | +def _get_chart_type_schema_impl( |
| 123 | + chart_type: str, |
| 124 | + include_examples: bool = True, |
| 125 | +) -> Dict[str, Any]: |
| 126 | + """Pure logic for chart type schema lookup — no auth, no decorators.""" |
| 127 | + adapter = _CHART_TYPE_ADAPTERS.get(chart_type) |
| 128 | + if adapter is None: |
| 129 | + return { |
| 130 | + "error": f"Unknown chart_type: {chart_type!r}", |
| 131 | + "valid_chart_types": VALID_CHART_TYPES, |
| 132 | + "hint": ( |
| 133 | + "Use one of the valid chart_type values listed above. " |
| 134 | + "Call this tool again with a valid chart_type to see " |
| 135 | + "its schema and examples." |
| 136 | + ), |
| 137 | + } |
| 138 | + |
| 139 | + schema = adapter.json_schema() |
| 140 | + result: Dict[str, Any] = { |
| 141 | + "chart_type": chart_type, |
| 142 | + "schema": schema, |
| 143 | + } |
| 144 | + |
| 145 | + if include_examples: |
| 146 | + result["examples"] = _CHART_EXAMPLES.get(chart_type, []) |
| 147 | + |
| 148 | + return result |
| 149 | + |
| 150 | + |
| 151 | +@tool( |
| 152 | + tags=["discovery"], |
| 153 | + annotations=ToolAnnotations( |
| 154 | + title="Get chart type schema", |
| 155 | + readOnlyHint=True, |
| 156 | + destructiveHint=False, |
| 157 | + ), |
| 158 | +) |
| 159 | +def get_chart_type_schema( |
| 160 | + chart_type: str, |
| 161 | + include_examples: bool = True, |
| 162 | +) -> Dict[str, Any]: |
| 163 | + """Get the full JSON Schema and examples for a specific chart type. |
| 164 | +
|
| 165 | + Use this tool to discover the exact fields, types, and constraints |
| 166 | + for a chart configuration before calling generate_chart or update_chart. |
| 167 | +
|
| 168 | + Valid chart_type values: xy, table, pie, pivot_table, |
| 169 | + mixed_timeseries, handlebars, big_number. |
| 170 | +
|
| 171 | + Returns the JSON Schema for the requested chart type, optionally |
| 172 | + with working examples. |
| 173 | + """ |
| 174 | + return _get_chart_type_schema_impl(chart_type, include_examples) |
0 commit comments