alexandrusoare commented on code in PR #40328: URL: https://github.com/apache/superset/pull/40328#discussion_r3304552048
########## superset/mcp_service/dashboard/tool/get_dashboard_layout.py: ########## @@ -0,0 +1,122 @@ +# 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. + +""" +Get dashboard layout FastMCP tool + +Companion to get_dashboard_info: returns the parsed dashboard layout +(tabs and chart positions) extracted from position_json. Use this +when get_dashboard_info's omitted_fields hint indicates position_json +was stripped and structured layout data is needed for analysis. +""" + +import logging +from datetime import datetime, timezone + +from fastmcp import Context +from sqlalchemy.orm import subqueryload +from superset_core.mcp.decorators import tool, ToolAnnotations + +from superset.extensions import event_logger +from superset.mcp_service.dashboard.schemas import ( + dashboard_layout_serializer, + DashboardError, + DashboardLayout, + GetDashboardLayoutRequest, +) +from superset.mcp_service.mcp_core import ModelGetInfoCore + +logger = logging.getLogger(__name__) + + +@tool( + tags=["discovery"], + class_permission_name="Dashboard", + annotations=ToolAnnotations( + title="Get dashboard layout", + readOnlyHint=True, + destructiveHint=False, + ), +) +async def get_dashboard_layout( + request: GetDashboardLayoutRequest, ctx: Context +) -> DashboardLayout | DashboardError: + """ + Get parsed dashboard layout by ID, UUID, or slug. + + Returns the tabs and chart positions extracted from the dashboard's + position_json. get_dashboard_info omits position_json to keep responses + small; call this tool when you need the structured layout (e.g. to + explain which charts live under which tab, or to locate a chart by + its parent tab). + + Example usage: + ```json + { + "identifier": 123 + } + ``` + """ + await ctx.info("Retrieving dashboard layout: identifier=%s" % (request.identifier,)) + + try: + from superset.daos.dashboard import DashboardDAO + from superset.models.dashboard import Dashboard + + eager_options = [subqueryload(Dashboard.slices)] + + with event_logger.log_context(action="mcp.get_dashboard_layout.lookup"): + core = ModelGetInfoCore( + dao_class=DashboardDAO, + output_schema=DashboardLayout, + error_schema=DashboardError, + serializer=dashboard_layout_serializer, + supports_slug=True, + logger=logger, + query_options=eager_options, + ) + result = core.run_tool(request.identifier) + + if isinstance(result, DashboardLayout): + await ctx.info( + "Dashboard layout retrieved: id=%s, tab_count=%s, chart_count=%s, " + "has_layout=%s" + % ( + result.id, + len(result.tabs), + len(result.charts), + result.has_layout, + ) + ) + else: + await ctx.warning( + "Dashboard layout retrieval failed: error_type=%s, error=%s" + % (result.error_type, result.error) + ) + + return result + + except Exception as e: Review Comment: Can we narrow down the type of errors here? ########## superset/mcp_service/dashboard/tool/get_dashboard_layout.py: ########## @@ -0,0 +1,122 @@ +# 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. + +""" +Get dashboard layout FastMCP tool + +Companion to get_dashboard_info: returns the parsed dashboard layout +(tabs and chart positions) extracted from position_json. Use this +when get_dashboard_info's omitted_fields hint indicates position_json +was stripped and structured layout data is needed for analysis. +""" + +import logging +from datetime import datetime, timezone + +from fastmcp import Context +from sqlalchemy.orm import subqueryload +from superset_core.mcp.decorators import tool, ToolAnnotations + +from superset.extensions import event_logger +from superset.mcp_service.dashboard.schemas import ( + dashboard_layout_serializer, + DashboardError, + DashboardLayout, + GetDashboardLayoutRequest, +) +from superset.mcp_service.mcp_core import ModelGetInfoCore + +logger = logging.getLogger(__name__) + + +@tool( + tags=["discovery"], + class_permission_name="Dashboard", + annotations=ToolAnnotations( + title="Get dashboard layout", + readOnlyHint=True, + destructiveHint=False, + ), +) +async def get_dashboard_layout( + request: GetDashboardLayoutRequest, ctx: Context +) -> DashboardLayout | DashboardError: + """ + Get parsed dashboard layout by ID, UUID, or slug. + + Returns the tabs and chart positions extracted from the dashboard's + position_json. get_dashboard_info omits position_json to keep responses + small; call this tool when you need the structured layout (e.g. to + explain which charts live under which tab, or to locate a chart by + its parent tab). + + Example usage: + ```json + { + "identifier": 123 + } + ``` + """ + await ctx.info("Retrieving dashboard layout: identifier=%s" % (request.identifier,)) + + try: + from superset.daos.dashboard import DashboardDAO + from superset.models.dashboard import Dashboard + + eager_options = [subqueryload(Dashboard.slices)] Review Comment: What do you think of this? -- 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]
