validate_chart_code
Validate user-created chart code for compliance with PyCafe standards, ensuring correct function structure, imports, and data usage. Optionally open the validated chart link in a browser for direct review.
Instructions
Validate the chart code created by the user and optionally open the PyCafe link in a browser.
Args:
chart_config: A ChartPlan object with the chart configuration
data_info: Metadata for the dataset to be used in the chart
auto_open: Whether to automatically open the PyCafe link in a browser
Returns:
ValidationResults object with status and dashboard details
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| auto_open | No | ||
| chart_config | Yes | ||
| data_info | Yes |
Input Schema (JSON Schema)
{
"$defs": {
"ChartPlan": {
"description": "Base chart plan used to generate chart code based on user visualization requirements.",
"properties": {
"chart_code": {
"description": "\n Python code that generates a generates a plotly go.Figure object. It must fulfill the following criteria:\n 1. Must be wrapped in a function that is named `chart_name`\n 2. Must accept as first argument argument `data_frame` which is a pandas DataFrame\n 3. Must return a plotly go.Figure object\n 4. All data used in the chart must be derived from the data_frame argument, all data manipulations\n must be done within the function.\n 5. DO NOT modify the background (with plot_bgcolor) or color sequences unless explicitly asked for\n 6. When creating hover templates, explicitly ensure that it works on light and dark mode\n ",
"title": "Chart Code",
"type": "string"
},
"chart_name": {
"description": "\n The name of the chart function. Should be unique, concise and in snake_case.\n ",
"pattern": "^[a-z][a-z0-9_]*$",
"title": "Chart Name",
"type": "string"
},
"chart_type": {
"description": "\n Describes the chart type that best reflects the user request.\n ",
"title": "Chart Type",
"type": "string"
},
"imports": {
"description": "\n List of import statements required to render the chart defined by the `chart_code` field. Ensure that every\n import statement is a separate list/array entry: An example of valid list of import statements would be:\n\n [\"import pandas as pd\",\n \"import plotly.express as px\"]\n ",
"items": {
"type": "string"
},
"title": "Imports",
"type": "array"
}
},
"required": [
"chart_type",
"chart_name",
"imports",
"chart_code"
],
"title": "ChartPlan",
"type": "object"
},
"DFMetaData": {
"properties": {
"column_names_types": {
"anyOf": [
{
"additionalProperties": {
"type": "string"
},
"type": "object"
},
{
"type": "null"
}
],
"default": null,
"title": "Column Names Types"
},
"file_location_type": {
"enum": [
"local",
"remote"
],
"title": "File Location Type",
"type": "string"
},
"file_name": {
"title": "File Name",
"type": "string"
},
"file_path_or_url": {
"title": "File Path Or Url",
"type": "string"
},
"read_function_string": {
"enum": [
"pd.read_csv",
"pd.read_json",
"pd.read_html",
"pd.read_parquet",
"pd.read_excel"
],
"title": "Read Function String",
"type": "string"
}
},
"required": [
"file_name",
"file_path_or_url",
"file_location_type",
"read_function_string"
],
"title": "DFMetaData",
"type": "object"
}
},
"properties": {
"auto_open": {
"default": true,
"title": "Auto Open",
"type": "boolean"
},
"chart_config": {
"$ref": "#/$defs/ChartPlan"
},
"data_info": {
"$ref": "#/$defs/DFMetaData"
}
},
"required": [
"chart_config",
"data_info"
],
"title": "validate_chart_codeArguments",
"type": "object"
}
Implementation Reference
- The primary handler function for the 'validate_chart_code' tool. It is decorated with @mcp.tool() for registration. Validates the ChartPlan configuration, generates dashboard code and PyCafe URL if data is remote, optionally opens in browser, and returns validation results.@mcp.tool() def validate_chart_code( chart_config: ChartPlan = Field(description="A ChartPlan object with the chart configuration"), data_info: DFMetaData = Field(description="Metadata for the dataset to be used in the chart"), auto_open: bool = Field(default=True, description="Whether to automatically open the PyCafe link in a browser"), ) -> ValidateResults: """Validate the chart code created by the user and optionally open the PyCafe link in a browser. Returns: ValidationResults object with status and dashboard details """ Vizro._reset() try: chart_plan_obj = ChartPlan.model_validate(chart_config) except ValidationError as e: return ValidateResults( valid=False, message=f"Validation Error: {e!s}", python_code="", pycafe_url=None, browser_opened=False, ) else: dashboard_code = chart_plan_obj.get_dashboard_template(data_info=data_info) # Generate PyCafe URL if all data is remote pycafe_url = create_pycafe_url(dashboard_code) if data_info.file_location_type == "remote" else None browser_opened = False if auto_open and pycafe_url: try: browser_opened = webbrowser.open(pycafe_url) except Exception: browser_opened = False return ValidateResults( valid=True, message="Chart only dashboard created successfully!", python_code=chart_plan_obj.get_chart_code(vizro=True), pycafe_url=pycafe_url, browser_opened=browser_opened, ) finally: Vizro._reset()
- vizro-mcp/src/vizro_mcp/server.py:324-324 (registration)The @mcp.tool() decorator registers the validate_chart_code function as an MCP tool.@mcp.tool()
- Pydantic Field definitions providing the input schema for the tool, including ChartPlan, DFMetaData, and auto_open parameters.chart_config: ChartPlan = Field(description="A ChartPlan object with the chart configuration"), data_info: DFMetaData = Field(description="Metadata for the dataset to be used in the chart"), auto_open: bool = Field(default=True, description="Whether to automatically open the PyCafe link in a browser"), ) -> ValidateResults: