You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@superset.apache.org by ms...@apache.org on 2026/04/15 17:09:21 UTC
(superset) branch msyavuz/feat/mcp-preview-chart-update created (now 33b3818fc27)
This is an automated email from the ASF dual-hosted git repository.
msyavuz pushed a change to branch msyavuz/feat/mcp-preview-chart-update
in repository https://gitbox.apache.org/repos/asf/superset.git
at 33b3818fc27 feat: add a preview flow to mcp chart updates
This branch includes the following new commits:
new 33b3818fc27 feat: add a preview flow to mcp chart updates
The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
(superset) 01/01: feat: add a preview flow to mcp chart updates
Posted by ms...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
msyavuz pushed a commit to branch msyavuz/feat/mcp-preview-chart-update
in repository https://gitbox.apache.org/repos/asf/superset.git
commit 33b3818fc2710c92d474c30b43ee21a735ec71eb
Author: Mehmet Salih Yavuz <sa...@proton.me>
AuthorDate: Wed Apr 15 20:08:57 2026 +0300
feat: add a preview flow to mcp chart updates
---
superset/mcp_service/chart/schemas.py | 8 +
superset/mcp_service/chart/tool/update_chart.py | 260 +++++++++++++++------
.../mcp_service/chart/tool/test_update_chart.py | 204 ++++++++++++++++
3 files changed, 402 insertions(+), 70 deletions(-)
diff --git a/superset/mcp_service/chart/schemas.py b/superset/mcp_service/chart/schemas.py
index aefce7ed4f5..f0384b7274f 100644
--- a/superset/mcp_service/chart/schemas.py
+++ b/superset/mcp_service/chart/schemas.py
@@ -1386,6 +1386,14 @@ class UpdateChartRequest(QueryCacheControl):
chart_name: str | None = Field(
None, description="Auto-generates if omitted", max_length=255
)
+ save_chart: bool = Field(
+ default=False,
+ description=(
+ "When False (default), generates a preview explore URL that lets the "
+ "user review changes and click Save to overwrite the original chart. "
+ "Set to True to persist the update immediately."
+ ),
+ )
generate_preview: bool = True
preview_formats: List[Literal["url", "ascii", "vega_lite", "table"]] = Field(
default_factory=lambda: ["url"],
diff --git a/superset/mcp_service/chart/tool/update_chart.py b/superset/mcp_service/chart/tool/update_chart.py
index e5be2d03324..b10da28375d 100644
--- a/superset/mcp_service/chart/tool/update_chart.py
+++ b/superset/mcp_service/chart/tool/update_chart.py
@@ -22,6 +22,7 @@ MCP tool: update_chart
import logging
import time
from typing import Any
+from urllib.parse import parse_qs, urlparse
from fastmcp import Context
from sqlalchemy.exc import SQLAlchemyError
@@ -65,6 +66,34 @@ def _find_chart(identifier: int | str) -> Any | None:
return ChartDAO.find_by_id(identifier, id_column="uuid")
+def _validation_error_response(
+ message: str, details: str
+) -> GenerateChartResponse:
+ return GenerateChartResponse.model_validate(
+ {
+ "chart": None,
+ "error": {
+ "error_type": "ValidationError",
+ "message": message,
+ "details": details,
+ },
+ "success": False,
+ "schema_version": "2.0",
+ "api_version": "v1",
+ }
+ )
+
+
+def _missing_config_or_name_error() -> GenerateChartResponse:
+ return _validation_error_response(
+ message="Either 'config' or 'chart_name' must be provided.",
+ details=(
+ "Either 'config' or 'chart_name' must be provided. "
+ "Use config for visualization changes, chart_name for renaming."
+ ),
+ )
+
+
def _build_update_payload(
request: UpdateChartRequest,
chart: Any,
@@ -94,26 +123,84 @@ def _build_update_payload(
# Name-only update: keep existing visualization, just rename
if not request.chart_name:
- return GenerateChartResponse.model_validate(
- {
- "chart": None,
- "error": {
- "error_type": "ValidationError",
- "message": ("Either 'config' or 'chart_name' must be provided."),
- "details": (
- "Either 'config' or 'chart_name' must be provided. "
- "Use config for visualization changes, chart_name "
- "for renaming."
- ),
- },
- "success": False,
- "schema_version": "2.0",
- "api_version": "v1",
- }
- )
+ return _missing_config_or_name_error()
return {"slice_name": request.chart_name}
+def _build_preview_form_data(
+ request: UpdateChartRequest,
+ chart: Any,
+) -> dict[str, Any] | GenerateChartResponse:
+ """Merge the existing chart's form_data with the requested changes.
+
+ Used by the preview-first flow so the user can review edits in Explore
+ before clicking Save. Returns the merged form_data dict on success, or a
+ GenerateChartResponse error when neither config nor chart_name is given.
+ """
+ existing_form_data: dict[str, Any] = {}
+ if getattr(chart, "params", None):
+ try:
+ existing_form_data = json.loads(chart.params) or {}
+ except (ValueError, TypeError):
+ logger.warning(
+ "Failed to parse existing chart.params for chart %s", chart.id
+ )
+ existing_form_data = {}
+
+ if request.config is not None:
+ config = parse_chart_config(request.config)
+ dataset_id = chart.datasource_id if chart.datasource_id else None
+ new_form_data = map_config_to_form_data(config, dataset_id=dataset_id)
+ new_form_data.pop("_mcp_warnings", None)
+ merged = {**existing_form_data, **new_form_data}
+ else:
+ if not request.chart_name:
+ return _missing_config_or_name_error()
+ merged = dict(existing_form_data)
+
+ if request.chart_name:
+ merged["slice_name"] = request.chart_name
+ elif chart.slice_name:
+ merged["slice_name"] = chart.slice_name
+
+ merged["slice_id"] = chart.id
+ if chart.datasource_id:
+ merged["datasource"] = f"{chart.datasource_id}__table"
+
+ return merged
+
+
+def _create_preview_url(
+ chart: Any, form_data: dict[str, Any]
+) -> tuple[str, str | None]:
+ """Cache form_data and return (explore_url, form_data_key).
+
+ The URL includes both ``slice_id`` and ``form_data_key`` so that when the
+ user clicks Save in Explore, the edits overwrite the original chart.
+ """
+ from superset.commands.explore.form_data.parameters import CommandParameters
+ from superset.mcp_service.commands.create_form_data import MCPCreateFormDataCommand
+ from superset.utils.core import DatasourceType
+
+ base_url = get_superset_base_url()
+
+ if not chart.datasource_id:
+ return f"{base_url}/explore/?slice_id={chart.id}", None
+
+ cmd_params = CommandParameters(
+ datasource_type=DatasourceType.TABLE,
+ datasource_id=chart.datasource_id,
+ chart_id=chart.id,
+ tab_id=None,
+ form_data=json.dumps(form_data),
+ )
+ form_data_key = MCPCreateFormDataCommand(cmd_params).run()
+ explore_url = (
+ f"{base_url}/explore/?form_data_key={form_data_key}&slice_id={chart.id}"
+ )
+ return explore_url, form_data_key
+
+
@tool(
tags=["mutate"],
class_permission_name="Chart",
@@ -128,13 +215,16 @@ async def update_chart( # noqa: C901
) -> GenerateChartResponse:
"""Update existing chart with new configuration.
- IMPORTANT:
- - Chart must already be saved (from generate_chart with save_chart=True)
- - LLM clients MUST display updated chart URL to users
- - Use numeric ID or UUID string to identify the chart (NOT chart name)
- - MUST include chart_type in config (either 'xy' or 'table')
+ IMPORTANT BEHAVIOR:
+ - Charts are NOT overwritten by default (save_chart=False) — a preview
+ explore URL is returned so the user can review changes and click Save
+ to overwrite the original chart (if they have permission).
+ - Set save_chart=True to persist the update immediately.
+ - LLM clients MUST display the returned explore URL to users.
+ - Use numeric ID or UUID string to identify the chart (NOT chart name).
+ - MUST include chart_type in config (either 'xy' or 'table').
- Example usage:
+ Example usage (preview, default):
```json
{
"identifier": 123,
@@ -147,17 +237,12 @@ async def update_chart( # noqa: C901
}
```
- Or with UUID:
+ Example usage (persist immediately):
```json
{
- "identifier": "a1b2c3d4-5678-90ab-cdef-1234567890ab",
- "config": {
- "chart_type": "table",
- "columns": [
- {"name": "product_name"},
- {"name": "revenue", "aggregate": "SUM"}
- ]
- }
+ "identifier": 123,
+ "save_chart": true,
+ "config": {"chart_type": "table", "columns": [{"name": "region"}]}
}
```
@@ -218,25 +303,42 @@ async def update_chart( # noqa: C901
}
)
- # Build update payload (config update or name-only rename)
- from superset.commands.chart.update import UpdateChartCommand
+ updated_chart: Any = None
+ explore_url: str
+ form_data_key: str | None = None
+ saved = False
+
+ if request.save_chart:
+ from superset.commands.chart.update import UpdateChartCommand
+
+ payload_or_error = _build_update_payload(request, chart)
+ if isinstance(payload_or_error, GenerateChartResponse):
+ return payload_or_error
- payload_or_error = _build_update_payload(request, chart)
- if isinstance(payload_or_error, GenerateChartResponse):
- return payload_or_error
+ with event_logger.log_context(action="mcp.update_chart.db_write"):
+ command = UpdateChartCommand(chart.id, payload_or_error)
+ updated_chart = command.run()
+ saved = True
+ explore_url = (
+ f"{get_superset_base_url()}/explore/?slice_id={updated_chart.id}"
+ )
+ else:
+ preview_or_error = _build_preview_form_data(request, chart)
+ if isinstance(preview_or_error, GenerateChartResponse):
+ return preview_or_error
- with event_logger.log_context(action="mcp.update_chart.db_write"):
- command = UpdateChartCommand(chart.id, payload_or_error)
- updated_chart = command.run()
+ with event_logger.log_context(action="mcp.update_chart.preview_link"):
+ explore_url, form_data_key = _create_preview_url(
+ chart, preview_or_error
+ )
# Parse config for analysis (may be None for name-only updates)
config = parse_chart_config(request.config) if request.config else None
- # Generate semantic analysis
- capabilities = analyze_chart_capabilities(updated_chart, config)
- semantics = analyze_chart_semantics(updated_chart, config)
+ chart_for_analysis = updated_chart if saved else chart
+ capabilities = analyze_chart_capabilities(chart_for_analysis, config)
+ semantics = analyze_chart_semantics(chart_for_analysis, config)
- # Create performance metadata
execution_time = int((time.time() - start_time) * 1000)
performance = PerformanceMetadata(
query_duration_ms=execution_time,
@@ -244,21 +346,29 @@ async def update_chart( # noqa: C901
optimization_suggestions=[],
)
- # Create accessibility metadata
chart_name = (
updated_chart.slice_name
- if updated_chart and hasattr(updated_chart, "slice_name")
- else (generate_chart_name(config) if config else "Updated chart")
+ if saved and updated_chart and hasattr(updated_chart, "slice_name")
+ else (
+ request.chart_name
+ or (chart.slice_name if hasattr(chart, "slice_name") else None)
+ or (generate_chart_name(config) if config else "Updated chart")
+ )
)
accessibility = AccessibilityMetadata(
- color_blind_safe=True, # Would need actual analysis
- alt_text=f"Updated chart showing {chart_name}",
+ color_blind_safe=True,
+ alt_text=(
+ f"Updated chart showing {chart_name}"
+ if saved
+ else f"Updated chart preview showing {chart_name}"
+ ),
high_contrast_available=False,
)
- # Generate previews if requested
- previews = {}
- if request.generate_preview:
+ # Generate previews for saved charts only. Unsaved previews rely on
+ # the explore URL for interactive viewing.
+ previews: dict[str, Any] = {}
+ if saved and updated_chart and request.generate_preview:
try:
with event_logger.log_context(action="mcp.update_chart.preview"):
from superset.mcp_service.chart.tool.get_chart_preview import (
@@ -279,35 +389,45 @@ async def update_chart( # noqa: C901
previews[format_type] = preview_result.content
except Exception as e:
- # Log warning but don't fail the entire request
logger.warning("Preview generation failed: %s", e)
- # Return enhanced data
+ # Fallback: extract form_data_key from explore_url if not set
+ if form_data_key is None and explore_url and "form_data_key=" in explore_url:
+ parsed = urlparse(explore_url)
+ values = parse_qs(parsed.query).get("form_data_key")
+ if values:
+ form_data_key = values[0]
+
+ chart_id = updated_chart.id if saved and updated_chart else chart.id
+ chart_uuid = (
+ str(updated_chart.uuid)
+ if saved and updated_chart and updated_chart.uuid
+ else (str(chart.uuid) if chart.uuid else None)
+ )
+ viz_type = (
+ updated_chart.viz_type if saved and updated_chart else chart.viz_type
+ )
+
result = {
"chart": {
- "id": updated_chart.id,
- "slice_name": updated_chart.slice_name,
- "viz_type": updated_chart.viz_type,
- "url": (
- f"{get_superset_base_url()}/explore/?slice_id={updated_chart.id}"
- ),
- "uuid": str(updated_chart.uuid) if updated_chart.uuid else None,
- "updated": True,
+ "id": chart_id,
+ "slice_name": chart_name,
+ "viz_type": viz_type,
+ "url": explore_url,
+ "uuid": chart_uuid,
+ "form_data_key": form_data_key,
+ "is_unsaved_state": not saved,
},
"error": None,
- # Enhanced fields for better LLM integration
"previews": previews,
"capabilities": capabilities.model_dump() if capabilities else None,
"semantics": semantics.model_dump() if semantics else None,
- "explore_url": (
- f"{get_superset_base_url()}/explore/?slice_id={updated_chart.id}"
- ),
+ "explore_url": explore_url,
+ "form_data_key": form_data_key,
"api_endpoints": {
- "data": (
- f"{get_superset_base_url()}/api/v1/chart/{updated_chart.id}/data/"
- ),
+ "data": f"{get_superset_base_url()}/api/v1/chart/{chart_id}/data/",
"export": (
- f"{get_superset_base_url()}/api/v1/chart/{updated_chart.id}/export/"
+ f"{get_superset_base_url()}/api/v1/chart/{chart_id}/export/"
),
},
"performance": performance.model_dump() if performance else None,
diff --git a/tests/unit_tests/mcp_service/chart/tool/test_update_chart.py b/tests/unit_tests/mcp_service/chart/tool/test_update_chart.py
index ca562aa9d3d..3d27ae19be4 100644
--- a/tests/unit_tests/mcp_service/chart/tool/test_update_chart.py
+++ b/tests/unit_tests/mcp_service/chart/tool/test_update_chart.py
@@ -37,6 +37,7 @@ from superset.mcp_service.chart.schemas import (
XYChartConfig,
)
from superset.mcp_service.chart.tool.update_chart import (
+ _build_preview_form_data,
_build_update_payload,
_find_chart,
)
@@ -677,6 +678,7 @@ class TestUpdateChartNameOnly:
request = {
"identifier": 1,
"chart_name": "Renamed Chart",
+ "save_chart": True,
"generate_preview": False,
}
@@ -685,6 +687,9 @@ class TestUpdateChartNameOnly:
assert result.structured_content["success"] is True
assert result.structured_content["chart"]["slice_name"] == "Renamed Chart"
+ assert (
+ result.structured_content["chart"]["is_unsaved_state"] is False
+ )
# Verify UpdateChartCommand was called with name-only payload
mock_update_cmd_cls.assert_called_once_with(
@@ -729,3 +734,202 @@ class TestUpdateChartNameOnly:
assert error["error_type"] == "ValidationError"
assert "config" in error["message"].lower()
assert "chart_name" in error["message"].lower()
+
+
+class TestUpdateChartPreviewFirst:
+ """Integration-style tests for the preview-first default flow."""
+
+ @patch(
+ "superset.mcp_service.chart.tool.update_chart._create_preview_url",
+ new_callable=Mock,
+ )
+ @patch(
+ "superset.commands.chart.update.UpdateChartCommand",
+ new_callable=Mock,
+ )
+ @patch(
+ "superset.mcp_service.auth.check_chart_data_access",
+ new_callable=Mock,
+ )
+ @patch("superset.daos.chart.ChartDAO.find_by_id", new_callable=Mock)
+ @patch("superset.db.session")
+ @pytest.mark.asyncio
+ async def test_default_generates_preview_without_saving(
+ self,
+ mock_db_session,
+ mock_find_by_id,
+ mock_check_access,
+ mock_update_cmd_cls,
+ mock_create_preview,
+ mcp_server,
+ ):
+ """Default update flow returns a preview URL and does NOT save."""
+ mock_chart = Mock()
+ mock_chart.id = 1
+ mock_chart.datasource_id = 10
+ mock_chart.slice_name = "Existing Chart"
+ mock_chart.viz_type = "table"
+ mock_chart.uuid = "abc-123"
+ mock_chart.params = '{"viz_type": "table", "datasource": "10__table"}'
+ mock_find_by_id.return_value = mock_chart
+
+ mock_check_access.return_value = DatasetValidationResult(
+ is_valid=True,
+ dataset_id=10,
+ dataset_name="my_dataset",
+ warnings=[],
+ )
+
+ preview_url = (
+ "http://localhost:8088/explore/?form_data_key=preview_key&slice_id=1"
+ )
+ mock_create_preview.return_value = (preview_url, "preview_key")
+
+ request = {
+ "identifier": 1,
+ "config": {
+ "chart_type": "table",
+ "columns": [{"name": "col1"}],
+ },
+ }
+
+ async with Client(mcp) as client:
+ result = await client.call_tool("update_chart", {"request": request})
+
+ assert result.structured_content["success"] is True
+ assert (
+ result.structured_content["chart"]["is_unsaved_state"] is True
+ )
+ assert result.structured_content["chart"]["id"] == 1
+ assert (
+ result.structured_content["chart"]["form_data_key"] == "preview_key"
+ )
+ assert result.structured_content["explore_url"] == preview_url
+ assert result.structured_content["form_data_key"] == "preview_key"
+
+ # Ensure the chart was NOT persisted
+ mock_update_cmd_cls.assert_not_called()
+ mock_create_preview.assert_called_once()
+
+ @patch(
+ "superset.mcp_service.chart.tool.update_chart._create_preview_url",
+ new_callable=Mock,
+ )
+ @patch(
+ "superset.mcp_service.auth.check_chart_data_access",
+ new_callable=Mock,
+ )
+ @patch("superset.daos.chart.ChartDAO.find_by_id", new_callable=Mock)
+ @patch("superset.db.session")
+ @pytest.mark.asyncio
+ async def test_preview_missing_config_and_name_returns_error(
+ self,
+ mock_db_session,
+ mock_find_by_id,
+ mock_check_access,
+ mock_create_preview,
+ mcp_server,
+ ):
+ """Preview flow also errors when neither config nor chart_name given."""
+ mock_chart = Mock()
+ mock_chart.id = 1
+ mock_chart.datasource_id = 10
+ mock_chart.params = "{}"
+ mock_find_by_id.return_value = mock_chart
+
+ mock_check_access.return_value = DatasetValidationResult(
+ is_valid=True,
+ dataset_id=10,
+ dataset_name="my_dataset",
+ warnings=[],
+ )
+
+ async with Client(mcp) as client:
+ result = await client.call_tool(
+ "update_chart", {"request": {"identifier": 1}}
+ )
+
+ assert result.structured_content["success"] is False
+ error = result.structured_content["error"]
+ assert error["error_type"] == "ValidationError"
+ mock_create_preview.assert_not_called()
+
+
+class TestBuildPreviewFormData:
+ """Unit tests for _build_preview_form_data helper."""
+
+ def test_merges_existing_params_with_new_config(self):
+ """New config values override existing form_data keys."""
+ config = TableChartConfig(
+ chart_type="table",
+ columns=[ColumnRef(name="region")],
+ )
+ request = UpdateChartRequest(identifier=1, config=config)
+ chart = Mock()
+ chart.id = 42
+ chart.datasource_id = 7
+ chart.slice_name = "Existing"
+ chart.params = '{"viz_type": "line", "custom_flag": true}'
+
+ result = _build_preview_form_data(request, chart)
+
+ assert isinstance(result, dict)
+ # Existing keys not touched by the new config are preserved
+ assert result["custom_flag"] is True
+ # New config overrides existing keys
+ assert result["viz_type"] == "table"
+ # slice_id and datasource are always stamped onto the preview
+ assert result["slice_id"] == 42
+ assert result["datasource"] == "7__table"
+ assert result["slice_name"] == "Existing"
+
+ def test_name_only_preview_keeps_existing_form_data(self):
+ """Name-only preview preserves existing form_data and renames."""
+ request = UpdateChartRequest(identifier=1, chart_name="Brand New Name")
+ chart = Mock()
+ chart.id = 5
+ chart.datasource_id = 3
+ chart.slice_name = "Old"
+ chart.params = '{"viz_type": "big_number", "metric": "count"}'
+
+ result = _build_preview_form_data(request, chart)
+
+ assert isinstance(result, dict)
+ assert result["viz_type"] == "big_number"
+ assert result["metric"] == "count"
+ assert result["slice_name"] == "Brand New Name"
+ assert result["slice_id"] == 5
+
+ def test_missing_config_and_name_returns_validation_error(self):
+ """Matches the _build_update_payload validation behavior."""
+ request = UpdateChartRequest(identifier=1)
+ chart = Mock()
+ chart.id = 1
+ chart.datasource_id = 10
+ chart.params = "{}"
+
+ result = _build_preview_form_data(request, chart)
+
+ assert isinstance(result, GenerateChartResponse)
+ assert result.success is False
+ assert result.error is not None
+ assert result.error.error_type == "ValidationError"
+
+ def test_handles_invalid_existing_params(self):
+ """Gracefully recovers when chart.params is not valid JSON."""
+ config = TableChartConfig(
+ chart_type="table",
+ columns=[ColumnRef(name="col1")],
+ )
+ request = UpdateChartRequest(identifier=1, config=config)
+ chart = Mock()
+ chart.id = 9
+ chart.datasource_id = 4
+ chart.slice_name = "Broken"
+ chart.params = "not-json"
+
+ result = _build_preview_form_data(request, chart)
+
+ assert isinstance(result, dict)
+ assert result["slice_id"] == 9
+ assert result["slice_name"] == "Broken"