codeant-ai-for-open-source[bot] commented on code in PR #40342: URL: https://github.com/apache/superset/pull/40342#discussion_r3311936078
########## superset/mcp_service/annotation_layer/tool/list_layer_annotations.py: ########## @@ -0,0 +1,149 @@ +# 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. + +"""List annotations within a layer FastMCP tool.""" + +import logging +from datetime import datetime, timezone + +from fastmcp import Context +from superset_core.mcp.decorators import tool, ToolAnnotations + +from superset.daos.base import ColumnOperator, ColumnOperatorEnum +from superset.extensions import event_logger +from superset.mcp_service.annotation_layer.schemas import ( + AnnotationFilter, + AnnotationInfo, + AnnotationLayerError, + AnnotationList, + DEFAULT_ANNOTATION_COLUMNS, + ListLayerAnnotationsRequest, + serialize_annotation, +) +from superset.mcp_service.mcp_core import ModelListCore + +logger = logging.getLogger(__name__) + +_ALL_ANNOTATION_COLUMNS = [ + "id", + "short_descr", + "long_descr", + "start_dttm", + "end_dttm", + "json_metadata", + "layer_id", +] +_SORTABLE_ANNOTATION_COLUMNS = ["id", "short_descr", "start_dttm", "end_dttm"] + + +@tool( + tags=["core"], + class_permission_name="Annotation", + annotations=ToolAnnotations( + title="List annotations in a layer", + readOnlyHint=True, + destructiveHint=False, + ), +) +async def list_layer_annotations( + request: ListLayerAnnotationsRequest, + ctx: Context, +) -> AnnotationList | AnnotationLayerError: + """List annotations within a specific annotation layer. + + The layer_id parameter is required and scopes all results to that layer. + + Sortable columns for order_column: id, short_descr, start_dttm, end_dttm + + Example: + ```json + {"layer_id": 1, "page": 1, "page_size": 25} + ``` + """ + await ctx.info( + "Listing annotations: layer_id=%s, page=%s, page_size=%s, search=%s" + % (request.layer_id, request.page, request.page_size, request.search) + ) + + try: + from superset.daos.annotation_layer import AnnotationDAO, AnnotationLayerDAO + + # Verify the layer exists before listing + layer = AnnotationLayerDAO.find_by_id(request.layer_id) + if layer is None: + await ctx.warning("Annotation layer not found: id=%s" % (request.layer_id,)) + return AnnotationLayerError.create( + error=f"Annotation layer with id '{request.layer_id}' not found", + error_type="not_found", + ) + + # Prepend the layer_id filter so results are scoped to this layer + layer_filter = ColumnOperator( + col="layer_id", opr=ColumnOperatorEnum.eq, value=request.layer_id + ) + combined_filters: list[ColumnOperator] = [layer_filter] + list(request.filters) + + def _serialize(obj: object, cols: list[str] | None) -> AnnotationInfo | None: + return serialize_annotation(obj) Review Comment: **Suggestion:** The annotation list serializer also ignores the `cols` parameter, so `select_columns` does not control which annotation fields are returned. This violates the advertised API behavior for column selection and can surface unrequested fields (as nulls) in responses. [incomplete implementation] <details> <summary><b>Severity Level:</b> Major ⚠️</summary> ```mdx - ⚠️ Layer annotation MCP tool ignores select_columns filtering. - ⚠️ Extra annotation fields returned, possibly large json_metadata. - ⚠️ Projection behavior diverges from other MCP listing tools. ``` </details> <details> <summary><b>Steps of Reproduction ✅ </b></summary> ```mdx 1. Start the MCP server via `mcp` from `superset.mcp_service.app`, which imports and registers `list_layer_annotations` (`app.py:12-17`), and is exposed in tests as the `mcp_server` fixture (`tests/unit_tests/mcp_service/annotation_layer/tool/test_annotation_layer_tools.py:77-81`). 2. Patch `AnnotationLayerDAO.find_by_id` and `AnnotationDAO.list` as in `test_list_layer_annotations_basic` (`test_annotation_layer_tools.py:259-267`) so that the layer exists (`make_layer()` at lines 43-52) and `AnnotationDAO.list` returns one annotation object with all fields set (`make_annotation()` at lines 55-69). 3. Using `fastmcp.Client` as in `test_list_layer_annotations_basic` (`test_annotation_layer_tools.py:9-13`), call the `list_layer_annotations` tool (`list_layer_annotations.py:62-65`) with a request specifying a narrow projection, for example `{"request": {"layer_id": 1, "select_columns": ["id", "short_descr"], "page": 1, "page_size": 10}}`. The request is forwarded into `ModelListCore.run_tool` at `list_layer_annotations.py:117-126`, which calculates `columns_requested=["id","short_descr"]` and `columns_to_load` accordingly (`mcp_core.py:100-112, 191-193`). 4. Parse the JSON response using `superset.utils.json` as in existing tests (`test_annotation_layer_tools.py:15-20`) and observe that each `annotations` entry contains all fields from `AnnotationInfo` (`schemas.py:170-177`), including `long_descr`, `start_dttm`, `end_dttm`, `json_metadata`, and `layer_id`, even though only `"id"` and `"short_descr"` were requested. This occurs because `_serialize` in `list_layer_annotations.py:100-101` ignores `cols` and always calls `serialize_annotation(obj)`, which unconditionally sets every field (`schemas.py:42-52`), and `AnnotationInfo` lacks a `_filter_fields_by_context` serializer like charts/datasets/dashboards, so `select_columns` does not control which fields appear in the response. ``` </details> [Fix in Cursor](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=95ccc7df5981461982f2ab0d9001e786&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset) | [Fix in VSCode Claude](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=95ccc7df5981461982f2ab0d9001e786&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 a comment left during a code review. **Path:** superset/mcp_service/annotation_layer/tool/list_layer_annotations.py **Line:** 100:101 **Comment:** *Incomplete Implementation: The annotation list serializer also ignores the `cols` parameter, so `select_columns` does not control which annotation fields are returned. This violates the advertised API behavior for column selection and can surface unrequested fields (as nulls) in responses. 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. 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> <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F40342&comment_hash=07b5422d28748f97622425cdf052b646fd16d3e576fd7218a02907c8253de925&reaction=like'>👍</a> | <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F40342&comment_hash=07b5422d28748f97622425cdf052b646fd16d3e576fd7218a02907c8253de925&reaction=dislike'>👎</a> -- 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]
