aminghadersohi commented on code in PR #40351:
URL: https://github.com/apache/superset/pull/40351#discussion_r3305907520


##########
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:
   Fixed: unit tests added in 
`tests/unit_tests/mcp_service/annotation_layer/tool/test_create_layer_annotation.py`
 covering success, layer-not-found, validation error (duplicate short_descr), 
create-failed, and optional-fields-forwarded cases.



##########
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:
   Fixed: added a `field_validator` for `json_metadata` in 
`CreateLayerAnnotationRequest` (schemas.py) that calls `json.loads` and raises 
`ValueError('json_metadata must be valid JSON')` for invalid input. The 
validator runs at the Pydantic layer before the value ever reaches 
`CreateAnnotationCommand`.



-- 
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]

Reply via email to