codeant-ai-for-open-source[bot] commented on code in PR #40399: URL: https://github.com/apache/superset/pull/40399#discussion_r3422476760
########## superset/mcp_service/dashboard/tool/update_dashboard.py: ########## @@ -0,0 +1,257 @@ +# 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. + +""" +Update dashboard FastMCP tool + +This module contains the FastMCP tool for updating an existing dashboard's +layout, theme, and styling. Companion to ``generate_dashboard`` for +incremental edits without re-creating the 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 DashboardNotFoundError +from superset.exceptions import SupersetSecurityException +from superset.extensions import db, event_logger +from superset.mcp_service.dashboard.schemas import ( + dashboard_serializer, + DashboardError, + UpdateDashboardRequest, + UpdateDashboardResponse, +) +from superset.mcp_service.utils.url_utils import get_superset_base_url +from superset.utils import json + +logger = logging.getLogger(__name__) + + +def _build_dashboard_url(dashboard: Any) -> str: + """Build the user-facing dashboard URL, preferring slug over id.""" + return ( + f"{get_superset_base_url()}/superset/dashboard/" + f"{dashboard.slug or dashboard.id}/" + ) + + +def _find_and_authorize_dashboard( + identifier: int | str, +) -> tuple[Any, UpdateDashboardResponse | DashboardError | None]: + """Return (dashboard, None) on success or (None, error_response) on failure. + + Mirrors the helper in ``add_chart_to_existing_dashboard``: combines + the not-found and forbidden cases so the main tool body has a single + pre-condition branch. Returns ``DashboardError`` on not-found and + ``UpdateDashboardResponse`` (with ``permission_denied=True``) on + ownership failure — the two shapes carry different information for + the caller. + """ + # avoids ImportError before Flask app initialisation: + # `Exception: App not initialized yet. Please call init_app first` + # raised from superset.utils.encrypt when DashboardDAO is imported + # (via Slice's encrypted Column types). `security_manager` is a + # LocalProxy that needs the same app context to resolve at call + # time, so it is co-located with the DAO it accompanies. + from superset import security_manager + from superset.daos.dashboard import DashboardDAO + + try: + dashboard = DashboardDAO.get_by_id_or_slug(identifier) + except (DashboardNotFoundError, SQLAlchemyError): + return None, DashboardError( Review Comment: **🟠 Architect Review — HIGH** _find_and_authorize_dashboard only handles DashboardNotFoundError and SQLAlchemyError from DashboardDAO.get_by_id_or_slug, but not DashboardAccessDeniedError; when base access is denied, the tool raises an unhandled exception instead of returning a structured permission_denied/DashboardError response as its contract implies. **Suggestion:** Extend the try/except around DashboardDAO.get_by_id_or_slug to also catch DashboardAccessDeniedError and map it to a structured response (e.g., an UpdateDashboardResponse with permission_denied=True and an appropriate error message), mirroring how ownership failures are handled. [](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=0dcf0320d4f34074a995b49d9ebfcdce&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset) [](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=0dcf0320d4f34074a995b49d9ebfcdce&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset) *(Use Cmd/Ctrl + Click for best experience)* <details> <summary><b>Prompt for AI Agent 🤖 </b></summary> ```mdx This is an **Architect / Logical Review** comment left during a code review. These reviews are first-class, important findings — not optional suggestions. Do NOT dismiss this as a 'big architectural change' just because the title says architect review; most of these can be resolved with a small, localized fix once the intent is understood. **Path:** superset/mcp_service/dashboard/tool/update_dashboard.py **Line:** 78:80 **Comment:** *HIGH: _find_and_authorize_dashboard only handles DashboardNotFoundError and SQLAlchemyError from DashboardDAO.get_by_id_or_slug, but not DashboardAccessDeniedError; when base access is denied, the tool raises an unhandled exception instead of returning a structured permission_denied/DashboardError response as its contract implies. Validate the correctness of the flagged issue. If correct, How can I resolve this? If you propose a fix, implement it and please make it concise. If a suggested approach is provided above, use it as the authoritative instruction. If no explicit code suggestion is given, you MUST still draft and apply your own minimal, localized fix — do not punt back with 'no suggestion provided, review manually'. Keep the change as small as possible: add a guard clause, gate on a loading state, reorder an await, wrap in a conditional, etc. Do not refactor surrounding code or expand scope beyond the finding. Once fix is implemented, also check other comments on the same PR, and ask user if the user wants to fix the rest of the comments as well. if said yes, then fetch all the comments validate the correctness and implement a minimal fix ``` </details> -- 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]
