codeant-ai-for-open-source[bot] commented on code in PR #40960:
URL: https://github.com/apache/superset/pull/40960#discussion_r3493342098
##########
superset/mcp_service/dashboard/schemas.py:
##########
@@ -1528,3 +1512,217 @@ def dashboard_layout_serializer(dashboard: "Dashboard")
-> DashboardLayout:
has_layout=bool(position_json_str),
)
)
+
+
+# ---------------------------------------------------------------------------
+# manage_native_filters schemas
+# ---------------------------------------------------------------------------
+
+
+class BaseNewFilterSpec(BaseModel):
+ """Common fields shared by all new native filter specs."""
+
+ name: str = Field(..., min_length=1, description="Filter display name")
+ description: str = Field("", description="Optional filter description")
+ scope_chart_ids: List[int] | None = Field(
+ None,
+ description=(
+ "Chart IDs this filter should apply to. When omitted the filter "
+ "applies to all charts on the dashboard. All IDs must belong to "
+ "charts that are on the dashboard."
+ ),
+ )
+
+
+class FilterSelectSpec(BaseNewFilterSpec):
+ """Spec for a new dropdown (filter_select) native filter."""
+
+ filter_type: Literal["filter_select"] = Field(
+ ..., description="Discriminator - must be 'filter_select'"
+ )
+ dataset_id: int = Field(..., description="ID of the dataset to filter on")
+ column: str = Field(
+ ..., min_length=1, description="Name of the dataset column to filter
on"
+ )
+ multi_select: bool = Field(
+ True, description="Allow selecting multiple values (default True)"
+ )
+ default_to_first_item: bool = Field(
+ False, description="Default the filter to the first item in the list"
+ )
+ enable_empty_filter: bool = Field(
+ False, description="Require a value before the filter is applied"
+ )
+ sort_ascending: bool | None = Field(
+ None,
+ description=(
+ "Sort filter values ascending (True) or descending (False). "
+ "When omitted, values are not explicitly sorted."
+ ),
+ )
+ search_all_options: bool = Field(
+ False, description="Query the database on search rather than
client-side"
+ )
+
+
+class FilterTimeSpec(BaseNewFilterSpec):
+ """Spec for a new time range (filter_time) native filter."""
+
+ filter_type: Literal["filter_time"] = Field(
+ ..., description="Discriminator - must be 'filter_time'"
+ )
+ default_time_range: str | None = Field(
+ None,
+ description=(
+ "Default time range value, e.g. 'Last week', 'Last month', "
+ "'2024-01-01 : 2024-12-31'. When omitted the filter has no
default."
+ ),
+ )
+
+
+NewNativeFilterSpec = Annotated[
+ FilterSelectSpec | FilterTimeSpec,
+ Field(discriminator="filter_type"),
+]
+
+
+class NativeFilterUpdateSpec(BaseModel):
+ """Partial update for an existing native filter.
+
+ Only ``id`` is required; any other provided field is merged into the
+ existing filter configuration. Fields that only apply to one filter
+ type (e.g. ``multi_select`` for filter_select, ``default_time_range``
+ for filter_time) are rejected when used on the wrong filter type.
+ """
+
+ id: str = Field(..., min_length=1, description="ID of the filter to
update")
+ name: str | None = Field(None, min_length=1, description="New display
name")
+ description: str | None = Field(None, description="New description")
+ dataset_id: int | None = Field(
+ None, description="New target dataset ID (filter_select only)"
+ )
+ column: str | None = Field(
+ None, min_length=1, description="New target column name (filter_select
only)"
+ )
+ multi_select: bool | None = Field(
+ None, description="Allow multiple values (filter_select only)"
+ )
+ default_to_first_item: bool | None = Field(
+ None, description="Default to first item (filter_select only)"
+ )
+ enable_empty_filter: bool | None = Field(
+ None, description="Require a value (filter_select only)"
+ )
+ sort_ascending: bool | None = Field(
+ None, description="Sort values ascending/descending (filter_select
only)"
+ )
+ search_all_options: bool | None = Field(
+ None, description="Search all options in the database (filter_select
only)"
+ )
+ default_time_range: str | None = Field(
+ None, description="Default time range (filter_time only)"
+ )
+ scope_chart_ids: List[int] | None = Field(
+ None,
+ description=(
+ "Chart IDs this filter should apply to. Replaces the current "
+ "scope. All IDs must belong to charts on the dashboard."
+ ),
+ )
+
+
+class ManageNativeFiltersRequest(BaseModel):
+ """Request schema for the manage_native_filters tool."""
+
+ dashboard_id: int = Field(..., description="ID of the dashboard to modify")
+ add: List[NewNativeFilterSpec] = Field(
+ default_factory=list,
+ description=(
+ "New filters to create. Supported types: filter_select "
+ "(dropdown) and filter_time (time range). Other filter types "
+ "(numerical range, time column, time grain) are not yet "
+ "supported by this tool."
+ ),
+ )
+ update: List[NativeFilterUpdateSpec] = Field(
+ default_factory=list,
+ description="Partial updates to existing filters, addressed by filter
ID",
+ )
+ remove: List[str] = Field(
+ default_factory=list,
+ description="IDs of filters to delete from the dashboard",
+ )
+ reorder: List[str] | None = Field(
+ None,
+ description=(
+ "Complete ordered list of filter IDs defining the new filter "
+ "order. Must include every filter that remains on the dashboard "
+ "(after removals); newly added filters are appended "
+ "automatically and may be omitted."
+ ),
+ )
Review Comment:
**Suggestion:** `ManageNativeFiltersRequest` defines list-typed inputs but
does not add `mode="before"` parsers for JSON-string payloads. In this
codebase, MCP clients (notably Claude Code) often send arrays/object-lists as
double-serialized strings, and without `parse_json_or_list` /
`parse_json_or_model_list` validators these otherwise valid requests fail
schema validation before the tool runs. Add pre-validators for `add`, `update`,
`remove`, and `reorder` to normalize string inputs into real lists. [api
mismatch]
<details>
<summary><b>Severity Level:</b> Critical 🚨</summary>
```mdx
- ❌ manage_native_filters MCP tool rejects valid stringified list inputs.
- ⚠️ LLM clients cannot programmatically manage dashboard native filters.
- ⚠️ Behavior inconsistent with list_dashboards and tag MCP tools.
- ⚠️ Additional client-side workarounds needed for Claude Code bug.
```
</details>
<details>
<summary><b>Steps of Reproduction ✅ </b></summary>
```mdx
1. The `ManageNativeFiltersRequest` schema is defined at
`superset/mcp_service/dashboard/schemas.py:1634-1683`, where `add`,
`update`, `remove`,
and `reorder` are declared as `List[...]` fields (lines 1638-176) without any
`@field_validator(..., mode="before")` using `parse_json_or_list` /
`parse_json_or_model_list`, unlike
`ListDashboardsRequest.filters/select_columns` at
`superset/mcp_service/dashboard/schemas.py:22-49` and
`ListTagsRequest.filters/select_columns` at
`superset/mcp_service/tag/schemas.py:17-25`,
which explicitly normalize JSON-string inputs before validation.
2. The MCP tool entrypoint `manage_native_filters` is registered with
FastMCP in
`superset/mcp_service/dashboard/tool/manage_native_filters.py:19-21`
(signature `request:
ManageNativeFiltersRequest, ctx: Context`) and exported into the MCP app in
`superset/mcp_service/dashboard/tool/__init__.py:23,32` and
`superset/mcp_service/app.py:700`, meaning FastMCP will instantiate
`ManageNativeFiltersRequest` from the raw tool parameters.
3. The unit-test helper `_call` in
`superset/tests/unit_tests/mcp_service/dashboard/tool/test_manage_native_filters.py:188-192`
shows the real execution path:
`Client(mcp_server).call_tool("manage_native_filters",
{"request": request})`, where `request` is a plain dict. If you adapt an
existing test
(e.g. `test_add_filter_select` at lines 200-228) to send a double-serialized
payload, such
as:
- `request = {"dashboard_id": 1, "add": json.dumps([{"filter_type":
"filter_select",
"name": "Region", "dataset_id": 5, "column": "region"}])}`, i.e. `add` is
a JSON string
rather than a list,
then `Client.call_tool` will pass this through to FastMCP, which attempts
to construct
`ManageNativeFiltersRequest` with `add` as a `str`.
4. Because `ManageNativeFiltersRequest.add`/`update`/`remove`/`reorder` are
bare
`List[...]` fields with no pre-validator, Pydantic treats the incoming
string as invalid
for a `List[NewNativeFilterSpec]` / `List[...]` and raises a
`ValidationError` before
`manage_native_filters()` is entered, causing the MCP call to fail for
otherwise valid
JSON-string list inputs, unlike other MCP tools which successfully handle
the same Claude
Code double-serialization bug via `parse_json_or_list` /
`parse_json_or_model_list`
pre-validators.
```
</details>
[](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=e396301e64ce4f8683aa8c4a8352a074&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
[](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=e396301e64ce4f8683aa8c4a8352a074&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/dashboard/schemas.py
**Line:** 1638:1663
**Comment:**
*Api Mismatch: `ManageNativeFiltersRequest` defines list-typed inputs
but does not add `mode="before"` parsers for JSON-string payloads. In this
codebase, MCP clients (notably Claude Code) often send arrays/object-lists as
double-serialized strings, and without `parse_json_or_list` /
`parse_json_or_model_list` validators these otherwise valid requests fail
schema validation before the tool runs. Add pre-validators for `add`, `update`,
`remove`, and `reorder` to normalize string inputs into real lists.
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%2F40960&comment_hash=7e0af8214b86101496d343a483626b8441f55ef76c3046bfda272ab75f254cfd&reaction=like'>👍</a>
| <a
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F40960&comment_hash=7e0af8214b86101496d343a483626b8441f55ef76c3046bfda272ab75f254cfd&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]