gkneighb commented on code in PR #41472: URL: https://github.com/apache/superset/pull/41472#discussion_r3555940429
########## superset/mcp_service/chart/tool/delete_chart.py: ########## @@ -0,0 +1,161 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +""" +MCP tool: delete_chart +""" + +import logging + +from fastmcp import Context +from sqlalchemy.exc import SQLAlchemyError +from superset_core.mcp.decorators import tool, ToolAnnotations + +from superset import is_feature_enabled +from superset.commands.chart.exceptions import ( + ChartDeleteFailedReportsExistError, + ChartForbiddenError, + ChartNotFoundError, +) +from superset.commands.exceptions import CommandException +from superset.extensions import event_logger +from superset.mcp_service.chart.chart_helpers import find_chart_by_identifier +from superset.mcp_service.chart.schemas import ( + DeleteChartRequest, + DeleteChartResponse, +) +from superset.mcp_service.utils import escape_llm_context_delimiters + +logger = logging.getLogger(__name__) + + +def _rollback() -> None: + from superset import db + + try: + db.session.rollback() # pylint: disable=consider-using-transaction + except SQLAlchemyError: + logger.warning("Database rollback failed during delete_chart error handling") + + +def _routes_to_soft_delete() -> bool: + """Mirror the ``BaseDAO.delete`` routing predicate so the response can + report whether the row was trashed (restorable) or permanently removed.""" + from superset.models.helpers import SoftDeleteMixin + from superset.models.slice import Slice + + return issubclass(Slice, SoftDeleteMixin) and is_feature_enabled("SOFT_DELETE") Review Comment: Factual correction: current master's Slice/Dashboard DO inherit SoftDeleteMixin (added by #40128/#40129, merged) — see superset/models/slice.py:67 and dashboard.py:126 on master. The quoted definitions are pre-soft-delete. Empirically, the soft-delete test patches only is_feature_enabled and passes, which requires the issubclass check to be truthy. Keeping the predicate mirroring BaseDAO.delete (both conditions) so the response can never report soft_deleted=true where the DAO would hard-delete. Full detail in the PR-level reply. ########## tests/unit_tests/mcp_service/chart/tool/test_delete_chart.py: ########## @@ -0,0 +1,171 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +"""Unit tests for the delete_chart MCP tool. + +Run through the async MCP Client (not direct calls); auth is mocked via the +autouse mock_auth fixture, matching the other chart tool test files. +""" + +from unittest.mock import Mock, patch + +import pytest +from fastmcp import Client + +from superset.mcp_service.app import mcp + + [email protected] +def mcp_server() -> object: + return mcp + + [email protected](autouse=True) +def mock_auth(): + with patch("superset.mcp_service.auth.get_user_from_request") as mock_get_user: + mock_user = Mock() + mock_user.id = 1 + mock_user.username = "admin" + mock_get_user.return_value = mock_user + yield mock_get_user + + +def _mock_chart(chart_id: int = 10, slice_name: str = "Test Chart") -> Mock: + chart = Mock() + chart.id = chart_id + chart.slice_name = slice_name + return chart + + +@patch("superset.mcp_service.chart.tool.delete_chart.find_chart_by_identifier") [email protected] +async def test_delete_chart_not_found(mock_find: Mock, mcp_server: object) -> None: + mock_find.return_value = None + + async with Client(mcp_server) as client: + result = await client.call_tool( + "delete_chart", {"request": {"identifier": 999}} + ) + + content = result.structured_content + assert content["success"] is False + assert content["error_type"] == "NotFound" + assert "999" in (content["error"] or "") + + +@patch("superset.commands.chart.delete.DeleteChartCommand.run") +@patch("superset.mcp_service.chart.tool.delete_chart.find_chart_by_identifier") [email protected] +async def test_delete_chart_success( + mock_find: Mock, mock_run: Mock, mcp_server: object +) -> None: + mock_find.return_value = _mock_chart(chart_id=10, slice_name="Sales") + mock_run.return_value = None + + async with Client(mcp_server) as client: + result = await client.call_tool("delete_chart", {"request": {"identifier": 10}}) + + content = result.structured_content + assert content["success"] is True + assert content["deleted_id"] == 10 + assert content["deleted_name"] == "Sales" + assert content["permission_denied"] is False + mock_run.assert_called_once() + + +@patch("superset.mcp_service.chart.tool.delete_chart.is_feature_enabled") +@patch("superset.commands.chart.delete.DeleteChartCommand.run") +@patch("superset.mcp_service.chart.tool.delete_chart.find_chart_by_identifier") [email protected] +async def test_delete_chart_soft_delete_reports_restorable( + mock_find: Mock, mock_run: Mock, mock_flag: Mock, mcp_server: object +) -> None: + mock_find.return_value = _mock_chart(chart_id=10, slice_name="Sales") + mock_run.return_value = None + mock_flag.side_effect = lambda flag: flag == "SOFT_DELETE" + + async with Client(mcp_server) as client: + result = await client.call_tool("delete_chart", {"request": {"identifier": 10}}) + + content = result.structured_content + assert content["success"] is True + assert content["soft_deleted"] is True Review Comment: Factual correction: current master's Slice/Dashboard DO inherit SoftDeleteMixin (added by #40128/#40129, merged) — see superset/models/slice.py:67 and dashboard.py:126 on master. The quoted definitions are pre-soft-delete. Empirically, the soft-delete test patches only is_feature_enabled and passes, which requires the issubclass check to be truthy. Keeping the predicate mirroring BaseDAO.delete (both conditions) so the response can never report soft_deleted=true where the DAO would hard-delete. Full detail in the PR-level reply. ########## superset/mcp_service/chart/tool/delete_chart.py: ########## @@ -0,0 +1,140 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +""" +MCP tool: delete_chart +""" + +import logging + +from fastmcp import Context +from sqlalchemy.exc import SQLAlchemyError +from superset_core.mcp.decorators import tool, ToolAnnotations + +from superset.commands.chart.exceptions import ( + ChartDeleteFailedReportsExistError, + ChartForbiddenError, + ChartNotFoundError, +) +from superset.commands.exceptions import CommandException +from superset.extensions import event_logger +from superset.mcp_service.chart.chart_helpers import find_chart_by_identifier +from superset.mcp_service.chart.schemas import ( + DeleteChartRequest, + DeleteChartResponse, +) +from superset.mcp_service.utils import escape_llm_context_delimiters + +logger = logging.getLogger(__name__) + + +def _rollback() -> None: + from superset import db + + try: + db.session.rollback() # pylint: disable=consider-using-transaction + except SQLAlchemyError: + logger.warning("Database rollback failed during delete_chart error handling") + + +@tool( + tags=["mutate"], + class_permission_name="Chart", + annotations=ToolAnnotations( + title="Delete chart", + readOnlyHint=False, + destructiveHint=True, + ), +) +async def delete_chart( + request: DeleteChartRequest, ctx: Context +) -> DeleteChartResponse: + """Permanently delete a saved chart. + + Identify the chart by numeric ID or UUID string (NOT chart name). This is + destructive and cannot be undone. The caller must own the chart (or be an + Admin); charts with attached alerts/reports cannot be deleted until those + are removed. + + Example: + ```json + {"identifier": 123} + ``` + + Returns success with the deleted chart's id/name, or an error. When the + caller lacks permission, ``permission_denied`` is true — do not retry; ask + the user. + """ + await ctx.info("Deleting chart: identifier=%s" % (request.identifier,)) + + chart = find_chart_by_identifier(request.identifier) + if not chart: + safe_id = escape_llm_context_delimiters(str(request.identifier)[:200]) + msg = ( + f"No chart found with identifier: {safe_id}. " + "Use list_charts to get valid chart IDs." + ) + return DeleteChartResponse(success=False, error=msg, error_type="NotFound") + + chart_id = chart.id + chart_name = chart.slice_name + + try: + from superset.commands.chart.delete import DeleteChartCommand + + with event_logger.log_context(action="mcp.delete_chart"): Review Comment: Fixed in 0439d6b2b2. Failed attempts are now recorded: the try/except sits inside log_context. ########## superset/mcp_service/dashboard/tool/delete_dashboard.py: ########## @@ -0,0 +1,166 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +""" +MCP tool: delete_dashboard +""" + +import logging +from typing import Any + +from fastmcp import Context +from sqlalchemy.exc import SQLAlchemyError +from superset_core.mcp.decorators import tool, ToolAnnotations + +from superset.commands.dashboard.exceptions import ( + DashboardDeleteFailedReportsExistError, + DashboardForbiddenError, + DashboardNotFoundError, +) +from superset.commands.exceptions import CommandException +from superset.extensions import event_logger +from superset.mcp_service.dashboard.schemas import ( + DeleteDashboardRequest, + DeleteDashboardResponse, +) +from superset.mcp_service.utils import escape_llm_context_delimiters + +logger = logging.getLogger(__name__) + + +def _find_dashboard_by_identifier(identifier: int | str) -> Any | None: + """Resolve a dashboard by numeric ID, UUID string, or slug. Returns None.""" + from superset.daos.dashboard import DashboardDAO + + if isinstance(identifier, int) or ( + isinstance(identifier, str) and identifier.isdigit() + ): + return DashboardDAO.find_by_id(int(identifier)) + # Try UUID, then fall back to slug. + dashboard = DashboardDAO.find_by_id(identifier, id_column="uuid") + if dashboard: + return dashboard + try: + return DashboardDAO.get_by_id_or_slug(identifier) + except DashboardNotFoundError: + return None + + +def _rollback() -> None: + from superset import db + + try: + db.session.rollback() # pylint: disable=consider-using-transaction + except SQLAlchemyError: + logger.warning( + "Database rollback failed during delete_dashboard error handling" + ) + + +@tool( + tags=["mutate"], + class_permission_name="Dashboard", + annotations=ToolAnnotations( + title="Delete dashboard", + readOnlyHint=False, + destructiveHint=True, + ), +) +async def delete_dashboard( + request: DeleteDashboardRequest, ctx: Context +) -> DeleteDashboardResponse: + """Permanently delete a dashboard. + + Identify the dashboard by numeric ID, UUID string, or slug. This is + destructive and cannot be undone. It removes the dashboard container only — + the charts on it are NOT deleted. The caller must own the dashboard (or be + an Admin); dashboards with attached alerts/reports cannot be deleted until + those are removed. + + Example: + ```json + {"identifier": 42} + ``` + + Returns success with the deleted dashboard's id/title, or an error. When the + caller lacks permission, ``permission_denied`` is true — do not retry; ask + the user. + """ + await ctx.info("Deleting dashboard: identifier=%s" % (request.identifier,)) + + dashboard = _find_dashboard_by_identifier(request.identifier) + if not dashboard: + safe_id = escape_llm_context_delimiters(str(request.identifier)[:200]) + msg = ( + f"No dashboard found with identifier: {safe_id}. " + "Use list_dashboards to get valid dashboard IDs." + ) + return DeleteDashboardResponse(success=False, error=msg, error_type="NotFound") + + dashboard_id = dashboard.id + dashboard_name = dashboard.dashboard_title + + try: + from superset.commands.dashboard.delete import DeleteDashboardCommand + + with event_logger.log_context(action="mcp.delete_dashboard"): Review Comment: Fixed in 0439d6b2b2. Same restructure as delete_chart. ########## superset/mcp_service/chart/tool/delete_chart.py: ########## @@ -0,0 +1,140 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +""" +MCP tool: delete_chart +""" + +import logging + +from fastmcp import Context +from sqlalchemy.exc import SQLAlchemyError +from superset_core.mcp.decorators import tool, ToolAnnotations + +from superset.commands.chart.exceptions import ( + ChartDeleteFailedReportsExistError, + ChartForbiddenError, + ChartNotFoundError, +) +from superset.commands.exceptions import CommandException +from superset.extensions import event_logger +from superset.mcp_service.chart.chart_helpers import find_chart_by_identifier +from superset.mcp_service.chart.schemas import ( + DeleteChartRequest, + DeleteChartResponse, +) +from superset.mcp_service.utils import escape_llm_context_delimiters + +logger = logging.getLogger(__name__) + + +def _rollback() -> None: + from superset import db + + try: + db.session.rollback() # pylint: disable=consider-using-transaction + except SQLAlchemyError: + logger.warning("Database rollback failed during delete_chart error handling") + + +@tool( + tags=["mutate"], + class_permission_name="Chart", + annotations=ToolAnnotations( + title="Delete chart", + readOnlyHint=False, + destructiveHint=True, + ), +) +async def delete_chart( + request: DeleteChartRequest, ctx: Context +) -> DeleteChartResponse: + """Permanently delete a saved chart. + + Identify the chart by numeric ID or UUID string (NOT chart name). This is + destructive and cannot be undone. The caller must own the chart (or be an + Admin); charts with attached alerts/reports cannot be deleted until those + are removed. + + Example: + ```json + {"identifier": 123} + ``` + + Returns success with the deleted chart's id/name, or an error. When the + caller lacks permission, ``permission_denied`` is true — do not retry; ask + the user. + """ + await ctx.info("Deleting chart: identifier=%s" % (request.identifier,)) + + chart = find_chart_by_identifier(request.identifier) + if not chart: + safe_id = escape_llm_context_delimiters(str(request.identifier)[:200]) + msg = ( + f"No chart found with identifier: {safe_id}. " + "Use list_charts to get valid chart IDs." + ) + return DeleteChartResponse(success=False, error=msg, error_type="NotFound") + + chart_id = chart.id + chart_name = chart.slice_name + + try: + from superset.commands.chart.delete import DeleteChartCommand + + with event_logger.log_context(action="mcp.delete_chart"): + DeleteChartCommand([chart_id]).run() + + return DeleteChartResponse( + success=True, + deleted_id=chart_id, + deleted_name=chart_name, + message=f"Deleted chart '{chart_name}' (id={chart_id}).", + ) + except ChartForbiddenError: + await ctx.warning("Permission denied deleting chart id=%s" % (chart_id,)) + return DeleteChartResponse( + success=False, + permission_denied=True, + error=( + f"You do not have permission to delete chart '{chart_name}' " + f"(id={chart_id}). Ask the user to delete it or grant access; " + "do not retry." + ), + error_type="Forbidden", + ) + except ChartDeleteFailedReportsExistError as ex: + _rollback() + return DeleteChartResponse( + success=False, + error=( + f"Chart '{chart_name}' (id={chart_id}) cannot be deleted: {ex}. " + "Remove the associated alerts/reports first." + ), + error_type="ReportsExist", + ) + except ChartNotFoundError: + msg = f"Chart id={chart_id} no longer exists." + return DeleteChartResponse(success=False, error=msg, error_type="NotFound") + except (CommandException, SQLAlchemyError, ValueError) as ex: + _rollback() + await ctx.error("Chart delete failed: %s: %s" % (type(ex).__name__, ex)) + return DeleteChartResponse( + success=False, + error=f"Chart delete failed: {ex}", Review Comment: Fixed in 0439d6b2b2. SQLAlchemyError now returns a generic client message; full exception stays in server logs. ########## superset/mcp_service/dashboard/tool/delete_dashboard.py: ########## @@ -0,0 +1,166 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +""" +MCP tool: delete_dashboard +""" + +import logging +from typing import Any + +from fastmcp import Context +from sqlalchemy.exc import SQLAlchemyError +from superset_core.mcp.decorators import tool, ToolAnnotations + +from superset.commands.dashboard.exceptions import ( + DashboardDeleteFailedReportsExistError, + DashboardForbiddenError, + DashboardNotFoundError, +) +from superset.commands.exceptions import CommandException +from superset.extensions import event_logger +from superset.mcp_service.dashboard.schemas import ( + DeleteDashboardRequest, + DeleteDashboardResponse, +) +from superset.mcp_service.utils import escape_llm_context_delimiters + +logger = logging.getLogger(__name__) + + +def _find_dashboard_by_identifier(identifier: int | str) -> Any | None: + """Resolve a dashboard by numeric ID, UUID string, or slug. Returns None.""" + from superset.daos.dashboard import DashboardDAO + + if isinstance(identifier, int) or ( + isinstance(identifier, str) and identifier.isdigit() + ): + return DashboardDAO.find_by_id(int(identifier)) + # Try UUID, then fall back to slug. + dashboard = DashboardDAO.find_by_id(identifier, id_column="uuid") + if dashboard: + return dashboard + try: + return DashboardDAO.get_by_id_or_slug(identifier) + except DashboardNotFoundError: + return None + + +def _rollback() -> None: + from superset import db + + try: + db.session.rollback() # pylint: disable=consider-using-transaction + except SQLAlchemyError: + logger.warning( + "Database rollback failed during delete_dashboard error handling" + ) + + +@tool( + tags=["mutate"], + class_permission_name="Dashboard", + annotations=ToolAnnotations( + title="Delete dashboard", + readOnlyHint=False, + destructiveHint=True, + ), +) +async def delete_dashboard( + request: DeleteDashboardRequest, ctx: Context +) -> DeleteDashboardResponse: + """Permanently delete a dashboard. + + Identify the dashboard by numeric ID, UUID string, or slug. This is + destructive and cannot be undone. It removes the dashboard container only — + the charts on it are NOT deleted. The caller must own the dashboard (or be + an Admin); dashboards with attached alerts/reports cannot be deleted until + those are removed. + + Example: + ```json + {"identifier": 42} + ``` + + Returns success with the deleted dashboard's id/title, or an error. When the + caller lacks permission, ``permission_denied`` is true — do not retry; ask + the user. + """ + await ctx.info("Deleting dashboard: identifier=%s" % (request.identifier,)) + + dashboard = _find_dashboard_by_identifier(request.identifier) + if not dashboard: + safe_id = escape_llm_context_delimiters(str(request.identifier)[:200]) + msg = ( + f"No dashboard found with identifier: {safe_id}. " + "Use list_dashboards to get valid dashboard IDs." + ) + return DeleteDashboardResponse(success=False, error=msg, error_type="NotFound") + + dashboard_id = dashboard.id + dashboard_name = dashboard.dashboard_title + + try: + from superset.commands.dashboard.delete import DeleteDashboardCommand + + with event_logger.log_context(action="mcp.delete_dashboard"): + DeleteDashboardCommand([dashboard_id]).run() + + return DeleteDashboardResponse( + success=True, + deleted_id=dashboard_id, + deleted_name=dashboard_name, + message=( + f"Deleted dashboard '{dashboard_name}' (id={dashboard_id}). " + "Its charts were not deleted." + ), + ) + except DashboardForbiddenError: + await ctx.warning( + "Permission denied deleting dashboard id=%s" % (dashboard_id,) + ) + return DeleteDashboardResponse( + success=False, + permission_denied=True, + error=( + f"You do not have permission to delete dashboard " + f"'{dashboard_name}' (id={dashboard_id}). Ask the user to delete " + "it or grant access; do not retry." + ), + error_type="Forbidden", + ) + except DashboardDeleteFailedReportsExistError as ex: + _rollback() + return DeleteDashboardResponse( + success=False, + error=( + f"Dashboard '{dashboard_name}' (id={dashboard_id}) cannot be " + f"deleted: {ex}. Remove the associated alerts/reports first." + ), + error_type="ReportsExist", + ) + except DashboardNotFoundError: + msg = f"Dashboard id={dashboard_id} no longer exists." + return DeleteDashboardResponse(success=False, error=msg, error_type="NotFound") + except (CommandException, SQLAlchemyError, ValueError) as ex: + _rollback() + await ctx.error("Dashboard delete failed: %s: %s" % (type(ex).__name__, ex)) + return DeleteDashboardResponse( + success=False, + error=f"Dashboard delete failed: {ex}", Review Comment: Fixed in 0439d6b2b2. Same sanitization as delete_chart. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
