@@ -66,7 +66,7 @@ class MCPUIGenerator:
6666 def __init__ (self ):
6767 self .logger = logging .getLogger (__name__ )
6868
69- def create_chart_ui_resource (self , chart_config : Dict [str , Any ], title : str = "Chart" ) -> Dict [str , Any ]:
69+ def create_chart_ui_resource (self , chart_config : Dict [str , Any ], title : str = "Chart" , x_axis : str = None , y_axis : str = None ) -> Dict [str , Any ]:
7070 """
7171 Create a chart UI resource from chart configuration.
7272
@@ -79,7 +79,7 @@ def create_chart_ui_resource(self, chart_config: Dict[str, Any], title: str = "C
7979 """
8080 try :
8181 # Create a simple chart HTML
82- chart_html = self ._generate_chart_html (chart_config , title )
82+ chart_html = self ._generate_chart_html (chart_config , title , x_axis , y_axis )
8383
8484 return createUIResource ({
8585 "uri" : f"ui://chart/{ datetime .now ().timestamp ()} " ,
@@ -121,7 +121,7 @@ def create_data_table_ui_resource(self, data: List[Dict[str, Any]], columns: Lis
121121 self .logger .error (f"Failed to create table UI resource: { e } " )
122122 return self ._create_error_ui_resource (f"Table generation failed: { e } " )
123123
124- def _generate_chart_html (self , chart_config : Dict [str , Any ], title : str ) -> str :
124+ def _generate_chart_html (self , chart_config : Dict [str , Any ], title : str , x_axis : str = None , y_axis : str = None ) -> str :
125125 """Generate HTML for chart visualization."""
126126 # Simple chart HTML using Chart.js or similar
127127 chart_id = f"chart_{ datetime .now ().timestamp ()} "
@@ -132,6 +132,13 @@ def _generate_chart_html(self, chart_config: Dict[str, Any], title: str) -> str:
132132 if not data_points :
133133 return f"<div><h3>{ title } </h3><p>No data available for chart</p></div>"
134134
135+ # Fallback to first two keys if x_axis/y_axis not provided
136+ if not x_axis or not y_axis :
137+ first_row = data_points [0 ] if data_points else {}
138+ keys = list (first_row .keys ())
139+ x_axis = x_axis or (keys [0 ] if len (keys ) > 0 else 'x' )
140+ y_axis = y_axis or (keys [1 ] if len (keys ) > 1 else 'y' )
141+
135142 # Simple HTML chart
136143 html = f"""
137144 <div style="padding: 20px;">
@@ -148,10 +155,10 @@ def _generate_chart_html(self, chart_config: Dict[str, Any], title: str) -> str:
148155 new Chart(ctx, {{
149156 type: 'bar',
150157 data: {{
151- labels: chartData.map(item => Object.keys( item)[0 ]),
158+ labels: chartData.map(item => item[' { x_axis } ' ]),
152159 datasets: [{{
153160 label: '{ title } ',
154- data: chartData.map(item => Object.values( item)[0 ]),
161+ data: chartData.map(item => item[' { y_axis } ' ]),
155162 backgroundColor: 'rgba(54, 162, 235, 0.2)',
156163 borderColor: 'rgba(54, 162, 235, 1)',
157164 borderWidth: 1
0 commit comments