Copilot commented on code in PR #40351: URL: https://github.com/apache/superset/pull/40351#discussion_r3285753898
########## superset/mcp_service/annotation_layer/tool/create_layer_annotation.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. + +import logging +from typing import Any + +from fastmcp import Context +from superset_core.mcp.decorators import tool, ToolAnnotations + +from superset.extensions import event_logger +from superset.mcp_service.annotation_layer.schemas import ( + CreateLayerAnnotationRequest, + CreateLayerAnnotationResponse, +) + +logger = logging.getLogger(__name__) + + +@tool( + tags=["mutate"], + class_permission_name="Annotation", + method_permission_name="write", + annotations=ToolAnnotations( + title="Add annotation to an annotation layer", + readOnlyHint=False, + destructiveHint=False, + ), +) +async def create_layer_annotation( + request: CreateLayerAnnotationRequest, ctx: Context +) -> CreateLayerAnnotationResponse: Review Comment: This PR adds a new MCP mutation tool but doesn’t include corresponding unit tests. Other MCP tools have dedicated tests under `tests/unit_tests/mcp_service/**/tool/` (e.g. `tests/unit_tests/mcp_service/dataset/tool/test_dataset_tools.py`), and adding similar coverage here would help prevent regressions in error handling (layer not found, validation errors, create failures) and ensure the request/response schemas serialize as expected. ########## superset/mcp_service/annotation_layer/tool/create_layer_annotation.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. + +import logging +from typing import Any + +from fastmcp import Context +from superset_core.mcp.decorators import tool, ToolAnnotations + +from superset.extensions import event_logger +from superset.mcp_service.annotation_layer.schemas import ( + CreateLayerAnnotationRequest, + CreateLayerAnnotationResponse, +) + +logger = logging.getLogger(__name__) + + +@tool( + tags=["mutate"], + class_permission_name="Annotation", + method_permission_name="write", + annotations=ToolAnnotations( + title="Add annotation to an annotation layer", + readOnlyHint=False, + destructiveHint=False, + ), +) +async def create_layer_annotation( + request: CreateLayerAnnotationRequest, ctx: Context +) -> CreateLayerAnnotationResponse: + """Add a new annotation to an existing annotation layer. + + Use this tool when a user wants to mark a specific time range on charts + with a label or note — for example, to flag a deployment, an outage, or + a campaign period. + + Workflow: + 1. Identify the target annotation layer (layer_id). Use list tools if needed. + 2. Call this tool with the layer_id, a short description, and the time range. + 3. The annotation will appear on charts that reference that layer. + """ + await ctx.info( + "Creating annotation: layer_id=%s, short_descr=%r" + % (request.layer_id, request.short_descr) + ) + + try: + from superset.commands.annotation_layer.annotation.create import ( + CreateAnnotationCommand, + ) + from superset.commands.annotation_layer.annotation.exceptions import ( + AnnotationCreateFailedError, + AnnotationInvalidError, + ) + from superset.commands.annotation_layer.exceptions import ( + AnnotationLayerNotFoundError, + ) + + properties: dict[str, Any] = { + "layer": request.layer_id, + "short_descr": request.short_descr, + "start_dttm": request.start_dttm, + "end_dttm": request.end_dttm, + } + if request.long_descr is not None: + properties["long_descr"] = request.long_descr + if request.json_metadata is not None: + properties["json_metadata"] = request.json_metadata + Review Comment: `json_metadata` is passed straight through to `CreateAnnotationCommand` without validating that it is valid JSON. The existing Annotation REST API enforces JSON validity for this field (`superset/annotation_layers/annotations/schemas.py` uses `validate_json`), so MCP clients can currently create annotations with invalid `json_metadata` that may break consumers expecting parseable JSON. -- 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]
