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


##########
superset/mcp_service/pydantic_schemas/chart_schemas.py:
##########
@@ -0,0 +1,139 @@
+# 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 chart-related responses
+"""
+from datetime import datetime
+from typing import Any, Dict, List, Optional, Union, Literal
+from pydantic import BaseModel, Field, ConfigDict
+from .dashboard_schemas import UserInfo, TagInfo, PaginationInfo
+
+class ChartListItem(BaseModel):
+    """Chart item for list responses"""
+    id: int = Field(..., description="Chart ID")
+    slice_name: str = Field(..., description="Chart name")
+    viz_type: Optional[str] = Field(None, description="Visualization type")
+    datasource_name: Optional[str] = Field(None, description="Datasource name")
+    datasource_type: Optional[str] = Field(None, description="Datasource type")
+    url: Optional[str] = Field(None, description="Chart URL")
+    description: Optional[str] = Field(None, description="Chart description")
+    cache_timeout: Optional[int] = Field(None, description="Cache timeout")
+    form_data: Optional[Dict[str, Any]] = Field(None, description="Chart form 
data")
+    query_context: Optional[Any] = Field(None, description="Query context")
+    changed_by: Optional[str] = Field(None, description="Last modifier 
(username)")
+    changed_by_name: Optional[str] = Field(None, description="Last modifier 
(display name)")
+    changed_on: Optional[Union[str, datetime]] = Field(None, description="Last 
modification timestamp")
+    changed_on_humanized: Optional[str] = Field(None, description="Humanized 
modification time")
+    created_by: Optional[str] = Field(None, description="Chart creator 
(username)")
+    created_on: Optional[Union[str, datetime]] = Field(None, 
description="Creation timestamp")
+    created_on_humanized: Optional[str] = Field(None, description="Humanized 
creation time")
+    tags: List[TagInfo] = Field(default_factory=list, description="Chart tags")
+    owners: List[UserInfo] = Field(default_factory=list, description="Chart 
owners")
+    model_config = ConfigDict(from_attributes=True, 
ser_json_timedelta="iso8601")
+
+class ChartListResponse(BaseModel):
+    """Response for chart list operations"""
+    charts: List[ChartListItem] = Field(..., description="List of charts")
+    count: int = Field(..., description="Number of charts in current page")
+    total_count: int = Field(..., description="Total number of charts")
+    page: int = Field(..., description="Current page number")
+    page_size: int = Field(..., description="Page size")
+    total_pages: int = Field(..., description="Total number of pages")
+    has_previous: bool = Field(..., description="Whether there is a previous 
page")
+    has_next: bool = Field(..., description="Whether there is a next page")
+    columns_requested: List[str] = Field(..., description="Columns that were 
requested")
+    columns_loaded: List[str] = Field(..., description="Columns that were 
actually loaded")
+    filters_applied: Dict[str, Any] = Field(..., description="Filters that 
were applied")
+    pagination: PaginationInfo = Field(..., description="Pagination 
information")
+    timestamp: datetime = Field(..., description="Response timestamp")
+    model_config = ConfigDict(ser_json_timedelta="iso8601")
+
+class ChartSimpleFilters(BaseModel):
+    slice_name: Optional[str] = Field(None, description="Filter by chart name 
(partial match)")
+    viz_type: Optional[str] = Field(None, description="Filter by visualization 
type")
+    datasource_name: Optional[str] = Field(None, description="Filter by 
datasource name")
+    changed_by: Optional[str] = Field(None, description="Filter by last 
modifier (username)")
+    created_by: Optional[str] = Field(None, description="Filter by creator 
(username)")
+    owner: Optional[str] = Field(None, description="Filter by owner 
(username)")
+    tags: Optional[str] = Field(None, description="Filter by tags 
(comma-separated)")
+
+class ChartAvailableFiltersResponse(BaseModel):
+    filters: Dict[str, Any] = Field(..., description="Available filters and 
their metadata")
+    operators: List[str] = Field(..., description="Supported filter operators")
+    columns: List[str] = Field(..., description="Available columns for 
filtering")
+
+class ChartInfoResponse(BaseModel):
+    chart: ChartListItem = Field(..., description="Detailed chart info")
+    model_config = ConfigDict(from_attributes=True, 
ser_json_timedelta="iso8601")
+
+class ChartErrorResponse(BaseModel):
+    error: str = Field(..., description="Error message")
+    error_type: str = Field(..., description="Type of error")
+    timestamp: Optional[Union[str, datetime]] = Field(None, description="Error 
timestamp")
+    model_config = ConfigDict(ser_json_timedelta="iso8601")
+
+def serialize_chart_object(chart) -> Optional[ChartListItem]:
+    if not chart:
+        return None
+    return ChartListItem(
+        id=getattr(chart, 'id', None),
+        slice_name=getattr(chart, 'slice_name', None),
+        viz_type=getattr(chart, 'viz_type', None),
+        datasource_name=getattr(chart, 'datasource_name', None),
+        datasource_type=getattr(chart, 'datasource_type', None),
+        url=getattr(chart, 'url', None),
+        description=getattr(chart, 'description', None),
+        cache_timeout=getattr(chart, 'cache_timeout', None),
+        form_data=getattr(chart, 'form_data', None),
+        query_context=getattr(chart, 'query_context', None),
+        changed_by=getattr(chart, 'changed_by_name', None) or 
(str(chart.changed_by) if getattr(chart, 'changed_by', None) else None),
+        changed_by_name=getattr(chart, 'changed_by_name', None) or 
(str(chart.changed_by) if getattr(chart, 'changed_by', None) else None),
+        changed_on=getattr(chart, 'changed_on', None),
+        changed_on_humanized=getattr(chart, 'changed_on_humanized', None),
+        created_by=getattr(chart, 'created_by_name', None) or 
(str(chart.created_by) if getattr(chart, 'created_by', None) else None),
+        created_on=getattr(chart, 'created_on', None),
+        created_on_humanized=getattr(chart, 'created_on_humanized', None),
+        tags=[TagInfo.model_validate(tag, from_attributes=True) for tag in 
getattr(chart, 'tags', [])] if getattr(chart, 'tags', None) else [],
+        owners=[UserInfo.model_validate(owner, from_attributes=True) for owner 
in getattr(chart, 'owners', [])] if getattr(chart, 'owners', None) else [],
+    ) 
+
+class CreateSimpleChartRequest(BaseModel):
+    """
+    Request schema for creating a simple chart via MCP.
+    """
+    slice_name: str = Field(..., description="Chart name")
+    viz_type: str = Field(..., description="Visualization type (e.g., bar, 
line, table, pie)")
+    datasource_id: int = Field(..., description="ID of the datasource 
(dataset) to use")
+    datasource_type: Literal["table"] = Field("table", description="Datasource 
type (usually 'table')")
+    metrics: List[str] = Field(..., description="List of metric names to 
display")
+    dimensions: List[str] = Field(..., description="List of dimension (column) 
names to group by")
+    filters: Optional[List[Dict[str, Any]]] = Field(None, description="List of 
filter objects (column, operator, value)")

Review Comment:
   seems given the pydantic schemas kept as static as possible using concrete 
types instead of dicts and string where possible, that it does figure things 
out well but i did notice a couple of instances where it added extra attributes 
that didnt exist, received an error and then corrected it. 
   I gave it "list all dashboards with z at the end the name " and it ran this:
   
   ```    
   {
     "filters": [
       {
         "col": "dashboard_title",
         "opr": "ilike",
         "value": "%z"
       }
     ],
     "select_columns": [
       "id",
       "dashboard_title"
     ],
     "order_column": "dashboard_title",
     "order_direction": "asc",
     "page": 1,
     "page_size": 100
   }
   ```
   
   "list all datasets related to population"
   
   ```
   {
     "filters": [
       {
         "col": "table_name",
         "opr": "ilike",
         "value": "%population%"
       }
     ],
     "select_columns": [
       "id",
       "table_name"
     ],
     "order_column": "table_name",
     "order_direction": "asc",
     "page": 1,
     "page_size": 100
   }
   ```
   <img width="594" height="459" alt="Screenshot 2025-07-16 at 11 05 41 AM" 
src="https://github.com/user-attachments/assets/a2568d05-6ec2-44bb-93ec-f57fe2febc4d";
 />
   <img width="603" height="784" alt="Screenshot 2025-07-16 at 11 06 08 AM" 
src="https://github.com/user-attachments/assets/2a827d1c-2560-4d27-9708-225b76a51301";
 />
   
   
   
   
   
   



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