Copilot commented on code in PR #40357: URL: https://github.com/apache/superset/pull/40357#discussion_r3285764950
########## superset/mcp_service/plugin/tool/create_plugin.py: ########## @@ -0,0 +1,108 @@ +# 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 fastmcp import Context +from superset_core.mcp.decorators import tool, ToolAnnotations + +from superset.extensions import event_logger +from superset.mcp_service.plugin.schemas import ( + CreatePluginRequest, + CreatePluginResponse, +) + +logger = logging.getLogger(__name__) + + +@tool( + tags=["mutate"], + class_permission_name="DynamicPlugin", + method_permission_name="write", + annotations=ToolAnnotations( + title="Register a dynamic plugin", + readOnlyHint=False, + destructiveHint=False, + ), +) +async def create_plugin( + request: CreatePluginRequest, ctx: Context +) -> CreatePluginResponse: + """Register a new dynamic (custom) plugin in Superset. + + Requires the DYNAMIC_PLUGINS feature flag to be enabled and admin write + access to DynamicPlugin. The ``key`` must match the package name from the + plugin's package.json and be unique across all registered plugins. Review Comment: The tool enforces RBAC via `class_permission_name="DynamicPlugin"` + `method_permission_name="write"`, which is not necessarily synonymous with “admin-only” (custom roles could be granted this permission). Consider rewording the docstring to state the exact required permission rather than implying a specific role. ########## superset/mcp_service/plugin/schemas.py: ########## @@ -0,0 +1,69 @@ +# 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. + +"""Pydantic schemas for plugin-related MCP tool requests and responses.""" + +from pydantic import BaseModel, Field, field_validator + + +class CreatePluginRequest(BaseModel): + """Request schema for create_plugin.""" + + name: str = Field( + ..., + min_length=1, + description="Human-friendly name for the plugin.", + ) + key: str = Field( + ..., + min_length=1, + description=( + "Unique plugin key. Should match the package name from the plugin's " + "package.json (e.g. '@my-org/my-chart-plugin')." + ), + ) + bundle_url: str = Field( + ..., + min_length=1, + description=( + "Full URL pointing to the built plugin bundle " + "(e.g. a CDN-hosted JavaScript file)." + ), + ) Review Comment: `DynamicPlugin` columns are constrained in the DB migration (name/key length 50, bundle_url length 1000), but the request schema only enforces `min_length`. This can lead to runtime DB errors (e.g., DataError) instead of a clean validation error. Add `max_length` constraints (and/or explicit validation) aligned with the underlying table to fail fast with a structured message. ########## superset/mcp_service/plugin/tool/create_plugin.py: ########## @@ -0,0 +1,108 @@ +# 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 fastmcp import Context +from superset_core.mcp.decorators import tool, ToolAnnotations + +from superset.extensions import event_logger +from superset.mcp_service.plugin.schemas import ( + CreatePluginRequest, + CreatePluginResponse, +) + +logger = logging.getLogger(__name__) + + +@tool( + tags=["mutate"], + class_permission_name="DynamicPlugin", + method_permission_name="write", + annotations=ToolAnnotations( + title="Register a dynamic plugin", + readOnlyHint=False, + destructiveHint=False, + ), +) +async def create_plugin( + request: CreatePluginRequest, ctx: Context +) -> CreatePluginResponse: + """Register a new dynamic (custom) plugin in Superset. + + Requires the DYNAMIC_PLUGINS feature flag to be enabled and admin write + access to DynamicPlugin. The ``key`` must match the package name from the + plugin's package.json and be unique across all registered plugins. + + After registration, Superset will load the plugin bundle from ``bundle_url`` + on the next page load. + """ + await ctx.info( + "Registering dynamic plugin: name=%r, key=%r" % (request.name, request.key) + ) + + try: + from sqlalchemy.exc import IntegrityError + + from superset import is_feature_enabled + from superset.extensions import db + from superset.models.dynamic_plugins import DynamicPlugin + + if not is_feature_enabled("DYNAMIC_PLUGINS"): + await ctx.warning("DYNAMIC_PLUGINS feature flag is not enabled") + return CreatePluginResponse( + error=( + "The DYNAMIC_PLUGINS feature flag is not enabled on this instance." + ) + ) + + with event_logger.log_context(action="mcp.create_plugin.create"): + plugin = DynamicPlugin( + name=request.name, + key=request.key, + bundle_url=request.bundle_url, + ) + db.session.add(plugin) + db.session.commit() + + await ctx.info( + "Dynamic plugin registered: id=%s, key=%r" % (plugin.id, plugin.key) + ) + + return CreatePluginResponse( + id=plugin.id, + name=plugin.name, + key=plugin.key, + bundle_url=plugin.bundle_url, + ) + + except IntegrityError as exc: + db.session.rollback() + msg = str(exc.orig) if exc.orig else str(exc) + await ctx.warning("Plugin creation failed (duplicate field): %s" % (msg,)) + return CreatePluginResponse( + error=( + "A plugin with the same name, key, or bundle_url already exists. " + "Each field must be unique." + ) + ) + except Exception as exc: + db.session.rollback() + await ctx.error( Review Comment: The exception handlers assume `db` (and even `IntegrityError`) are always defined, but both are imported inside the `try`. If an exception occurs before those imports complete (e.g., import failure), evaluating `except IntegrityError` and/or calling `db.session.rollback()` will raise and mask the original error. Import `IntegrityError`/`db` outside the `try`, and guard rollback (similar to other MCP tools) so error handling can’t crash. ########## superset/mcp_service/plugin/tool/create_plugin.py: ########## @@ -0,0 +1,108 @@ +# 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 fastmcp import Context +from superset_core.mcp.decorators import tool, ToolAnnotations + +from superset.extensions import event_logger +from superset.mcp_service.plugin.schemas import ( + CreatePluginRequest, + CreatePluginResponse, +) + +logger = logging.getLogger(__name__) + + +@tool( + tags=["mutate"], + class_permission_name="DynamicPlugin", + method_permission_name="write", + annotations=ToolAnnotations( + title="Register a dynamic plugin", + readOnlyHint=False, + destructiveHint=False, + ), +) +async def create_plugin( + request: CreatePluginRequest, ctx: Context +) -> CreatePluginResponse: + """Register a new dynamic (custom) plugin in Superset. Review Comment: This PR introduces a new MCP mutation tool and request/response schemas but doesn’t add unit tests. There are existing unit tests for other MCP tools (e.g., create_virtual_dataset) that cover schema validation, success, feature-flag behavior, and permission/IntegrityError paths—this tool should have similar coverage to prevent regressions. ########## superset/mcp_service/app.py: ########## @@ -130,6 +130,9 @@ def get_default_instructions( - generate_dashboard: Create a dashboard from chart IDs (requires write access) - add_chart_to_existing_dashboard: Add a chart to an existing dashboard (requires write access) +Plugin Management: +- create_plugin: Register a new dynamic plugin by name, key, and bundle URL (requires admin write access and DYNAMIC_PLUGINS feature flag) Review Comment: This instruction text says the tool “requires admin write access”, but the implementation is permission-based (`DynamicPlugin`/`write`) and may be granted to non-admin roles depending on RBAC configuration. Consider wording this as “requires DynamicPlugin write permission” (and keep the DYNAMIC_PLUGINS flag requirement). -- 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]
