MCP: XYChartConfig has no sort option — bar charts created via MCP are always ordered by category name
- Dominant language
- Python
- Stars
- 74.8k
- Forks
- 18.3k
- Avg merge
- 2d 5h
- Merged PRs (30d)
- 685
Description
### Problem
`XYChartConfig` in `superset/mcp_service/chart/schemas.py` (line 1662 on master @ `7b351d5`) exposes no way to order categories. Its full field set is:
```
chart_type, x, y, kind, time_grain, orientation, stacked, group_by,
x_axis, y_axis, legend, x_axis_time_format, show_value, currency_format,
color_scheme, filters, row_limit, series_limit
```
There is no sort field, and `grep -ci sort` over the class body returns `0`.
Other chart configs in the same file do have one:
- `TableChartConfig` — `sort_by: List[str | SortByConfig]` (line 1572)
- `PieChartConfig` — `sort_by_metric: bool = True` (line 1047)
A `SortByConfig` model already exists (line 997) with `column` and `ascending` fields, and its docstring describes exactly the relevant use case:
```
Bare strings default to descending, which matches the
sort-by-metric "top N" pattern most commonly used for tables.
```
### Effect
Every `xy` chart created through MCP is emitted with:
```json
"x_axis_sort_series_type": "name",
"x_axis_sort_series_ascending": true
```
so categorical bar charts are always ordered by category name. For a ranking — the most common reason to reach for a bar chart — this is the wrong default and there is no way to override it through the tool. The equivalent is a one-click "Sort by metric" toggle in the Explore UI, so charts built via MCP start out worse than charts built by hand and need manual fixing afterwards.
Concrete case: a bar chart of orders per Brazilian state (27 categories, top value ~3x the second and ~900x the smallest) renders alphabetically AC, AL, AM, AP, BA… The concentration that motivates the chart is invisible until a human re-sorts it.
Sorting is also not reachable by other means, since `update_chart` accepts only the simplified `config` (raw `form_data` is not honoured), and unknown nested keys such as `x_axis: {"sort_by": ...}` are silently dropped rather than rejected (filed separately).
### Proposed change
Add a sort field to `XYChartConfig`, reusing the existing `SortByConfig`:
```python
sort_by: SortByConfig | None = Field(
None,
description="Order categories by a metric label or column. "
"Defaults to category name when unset.",
)
```
mapping to the native `x_axis_sort` / `x_axis_sort_asc` (or `x_axis_sort_series_type` / `x_axis_sort_series_ascending` for the series case) form_data keys.
Two things worth deciding in review:
1. **Whether to change the default.** Leaving it at category-name ordering is the compatible choice; defaulting bar charts to metric-descending matches what people usually want but would alter existing charts on regeneration. I'd lean toward keeping the current default and making sort opt-in.
2. **Scope.** `kind` covers `line`, `bar`, `area`, `scatter`. Sorting is meaningful for categorical `bar`/`area` but not for a temporal x-axis, so it may warrant a validator rejecting `sort_by` when `time_grain` is set.
Happy to implement whichever shape reviewers prefer.
### Environment
- Apache Superset 6.1.0 (docker), pydantic 2.11.7, fastmcp 3.4.5
- Field lists confirmed against `master` @ `7b351d5`
Contributor guide
Assessment
This issue has not been assessed yet.