feat(mcp): add get_chart_sql tool and expose chart filters in get_chart_info#38700
feat(mcp): add get_chart_sql tool and expose chart filters in get_chart_info#38700aminghadersohi wants to merge 8 commits intoapache:masterfrom
Conversation
Sequence DiagramThis PR adds a new get chart sql tool that returns rendered chart SQL without running the query, supporting both saved charts and unsaved cached chart state. It also enhances get chart info to include a structured filters field extracted from chart form data. sequenceDiagram
participant Client
participant MCP Service
participant Chart Store
participant Cache
participant Query Builder
Client->>MCP Service: Request get chart sql
alt Unsaved state with form data key
MCP Service->>Cache: Load cached chart form data
Cache-->>MCP Service: Unsaved chart form data
else Saved chart identifier
MCP Service->>Chart Store: Load chart and validate dataset access
Chart Store-->>MCP Service: Saved chart configuration
end
MCP Service->>Query Builder: Build query context and render SQL
Query Builder-->>MCP Service: Rendered SQL result
MCP Service-->>Client: Return chart SQL response
Client->>MCP Service: Request get chart info
MCP Service->>MCP Service: Extract filters from form data
MCP Service-->>Client: Return chart info with filters
Generated by CodeAnt AI |
Codecov Report❌ Patch coverage is ❌ Your project check has failed because the head coverage (99.85%) is below the target coverage (100.00%). You can increase the head coverage or adjust the target coverage. Additional details and impacted files@@ Coverage Diff @@
## master #38700 +/- ##
==========================================
- Coverage 65.54% 64.40% -1.14%
==========================================
Files 1820 2534 +714
Lines 72868 130449 +57581
Branches 23339 30199 +6860
==========================================
+ Hits 47758 84019 +36261
- Misses 25110 44966 +19856
- Partials 0 1464 +1464
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Code Review Agent Run #57d92b
Actionable Suggestions - 1
-
superset/mcp_service/chart/tool/get_chart_sql.py - 1
- Use of assert statement detected · Line 306-307
Additional Suggestions - 1
-
superset/mcp_service/app.py - 1
-
Missing Unit Tests for New Tool · Line 401-401The new get_chart_sql tool has been added without unit tests, which violates the documented development standards in CLAUDE.md. All new tools must have corresponding unit tests to ensure reliability and prevent regressions.
-
Review Details
-
Files reviewed - 5 · Commit Range:
d01bc8a..d01bc8a- superset/mcp_service/app.py
- superset/mcp_service/chart/schemas.py
- superset/mcp_service/chart/tool/__init__.py
- superset/mcp_service/chart/tool/get_chart_info.py
- superset/mcp_service/chart/tool/get_chart_sql.py
-
Files skipped - 0
-
Tools
- Whispers (Secret Scanner) - ✔︎ Successful
- Detect-secrets (Secret Scanner) - ✔︎ Successful
- MyPy (Static Code Analysis) - ✔︎ Successful
- Astral Ruff (Static Code Analysis) - ✔︎ Successful
Bito Usage Guide
Commands
Type the following command in the pull request comment and save the comment.
-
/review- Manually triggers a full AI review. -
/pause- Pauses automatic reviews on this pull request. -
/resume- Resumes automatic reviews. -
/resolve- Marks all Bito-posted review comments as resolved. -
/abort- Cancels all in-progress reviews.
Refer to the documentation for additional commands.
Configuration
This repository uses Superset You can customize the agent settings here or contact your Bito workspace admin at evan@preset.io.
Documentation & Help
Code Review Agent Run #587934Actionable Suggestions - 0Review Details
Bito Usage GuideCommands Type the following command in the pull request comment and save the comment.
Refer to the documentation for additional commands. Configuration This repository uses Documentation & Help |
There was a problem hiding this comment.
Code Review Agent Run #9638dc
Actionable Suggestions - 1
-
superset/mcp_service/chart/tool/get_chart_sql.py - 1
- Invalid default for datasource ID · Line 119-121
Review Details
-
Files reviewed - 3 · Commit Range:
ef874ae..8774641- superset/mcp_service/chart/schemas.py
- superset/mcp_service/chart/tool/get_chart_sql.py
- tests/unit_tests/mcp_service/chart/tool/test_get_chart_sql.py
-
Files skipped - 0
-
Tools
- Whispers (Secret Scanner) - ✔︎ Successful
- Detect-secrets (Secret Scanner) - ✔︎ Successful
- MyPy (Static Code Analysis) - ✔︎ Successful
- Astral Ruff (Static Code Analysis) - ✔︎ Successful
Bito Usage Guide
Commands
Type the following command in the pull request comment and save the comment.
-
/review- Manually triggers a full AI review. -
/pause- Pauses automatic reviews on this pull request. -
/resume- Resumes automatic reviews. -
/resolve- Marks all Bito-posted review comments as resolved. -
/abort- Cancels all in-progress reviews.
Refer to the documentation for additional commands.
Configuration
This repository uses Superset You can customize the agent settings here or contact your Bito workspace admin at evan@preset.io.
Documentation & Help
8774641 to
ec92d9c
Compare
There was a problem hiding this comment.
Code Review Agent Run #43b18d
Actionable Suggestions - 1
-
superset/mcp_service/chart/schemas.py - 1
- Unhandled ValidationError in filter extraction · Line 308-308
Additional Suggestions - 1
-
superset/mcp_service/chart/schemas.py - 1
-
Incomplete field description · Line 115-121The description for the filters field claims to include 'all filters' but omits granularity_sqla, which is extracted and included in ChartFiltersInfo.
Code suggestion
@@ -118,4 +118,4 @@ - "extracted from form_data. Includes adhoc filters, time range, " - "extra filters, and custom WHERE/HAVING clauses." + "extracted from form_data. Includes adhoc filters, time range, " + "extra filters, and custom WHERE/HAVING clauses, granularity_sqla."
-
Filtered by Review Rules
Bito filtered these suggestions based on rules created automatically for your feedback. Manage rules.
-
superset/mcp_service/chart/tool/get_chart_sql.py - 1
- Missing Access Validation · Line 390-428
Review Details
-
Files reviewed - 6 · Commit Range:
6a74b07..6fad704- superset/mcp_service/app.py
- superset/mcp_service/chart/schemas.py
- superset/mcp_service/chart/tool/__init__.py
- superset/mcp_service/chart/tool/get_chart_info.py
- superset/mcp_service/chart/tool/get_chart_sql.py
- tests/unit_tests/mcp_service/chart/tool/test_get_chart_sql.py
-
Files skipped - 0
-
Tools
- Whispers (Secret Scanner) - ✔︎ Successful
- Detect-secrets (Secret Scanner) - ✔︎ Successful
- MyPy (Static Code Analysis) - ✔︎ Successful
- Astral Ruff (Static Code Analysis) - ✔︎ Successful
Bito Usage Guide
Commands
Type the following command in the pull request comment and save the comment.
-
/review- Manually triggers a full AI review. -
/pause- Pauses automatic reviews on this pull request. -
/resume- Resumes automatic reviews. -
/resolve- Marks all Bito-posted review comments as resolved. -
/abort- Cancels all in-progress reviews.
Refer to the documentation for additional commands.
Configuration
This repository uses Superset You can customize the agent settings here or contact your Bito workspace admin at evan@preset.io.
Documentation & Help
…rt_info Add a dedicated get_chart_sql MCP tool that returns the rendered SQL for a chart without executing it, similar to the "View Query" feature in the UI. This enables agents to inspect, debug, and audit chart SQL. Also expose existing chart filters (adhoc_filters, time_range, extra_filters, WHERE/HAVING clauses) as a structured field on the get_chart_info response, improving filter discoverability for MCP consumers.
- Validate extra_filters items are dicts before building ChartFiltersInfo - Parse combined datasource field (e.g. "123__table") in form_data - Guard JSON parsing of chart.params with try/except - Apply walrus operator patterns for auto-walrus pre-commit hook
…t_chart_sql tests - Fix mypy typeddict-item errors in get_chart_sql by resolving datasource_id/datasource_type to proper types before passing to QueryContextFactory.create() - Add isinstance(form_data, dict) guard in extract_filters_from_form_data to prevent 'Mock object is not iterable' errors when form_data is not a dict (e.g., from Mock objects in test environments) - Add comprehensive unit tests for the get_chart_sql tool covering: - GetChartSqlRequest schema validation - _extract_sql_from_result helper (success, empty, error cases) - _find_chart_by_identifier helper (numeric, string, UUID, not found) - _resolve_effective_form_data helper (saved, cached, fallback) - MCP Client integration tests (not found, success, fallback, access denied)
Use importlib.import_module to get the actual module object instead of the function exported from __init__.py, fixing patch.object calls that were resolving to the function instead of the module.
- Raise ValueError instead of defaulting to 0 for invalid datasource ID - Wrap AdhocFilter construction in try/except to skip malformed entries - Include row_limit from form_data in query context for accurate SQL - Catch ValueError in callers of _sql_from_form_data
…ter schemas - Add ValidationError to except clause for AdhocFilter construction so Pydantic validation errors from malformed filter fields are caught - Add item-level validation for extra_filters to reject entries without a valid string "col" key - Catch ValueError/TypeError in outer get_chart_sql handler so malformed chart.params or datasource parsing errors do not escape as unhandled exceptions
6fad704 to
a9411af
Compare
- Extract _resolve_metrics_and_groupby helper to reduce complexity of _build_query_context_from_form_data (fixes ruff C901) - Add fallback metric/dimension field aliases (metric, entity, series, columns) so that chart types which store measures/dimensions under non-standard keys produce correct SQL in the fallback flow - Collect SQL from ALL query results in _extract_sql_from_result instead of only the first, so composite charts (e.g. mixed timeseries) do not silently lose part of their SQL output
There was a problem hiding this comment.
Code Review Agent Run #641514
Actionable Suggestions - 1
-
superset/mcp_service/chart/tool/get_chart_sql.py - 1
- Incomplete function body · Line 335-361
Review Details
-
Files reviewed - 6 · Commit Range:
704854f..ceb613e- superset/mcp_service/app.py
- superset/mcp_service/chart/schemas.py
- superset/mcp_service/chart/tool/__init__.py
- superset/mcp_service/chart/tool/get_chart_info.py
- superset/mcp_service/chart/tool/get_chart_sql.py
- tests/unit_tests/mcp_service/chart/tool/test_get_chart_sql.py
-
Files skipped - 0
-
Tools
- Whispers (Secret Scanner) - ✔︎ Successful
- Detect-secrets (Secret Scanner) - ✔︎ Successful
- MyPy (Static Code Analysis) - ✔︎ Successful
- Astral Ruff (Static Code Analysis) - ✔︎ Successful
Bito Usage Guide
Commands
Type the following command in the pull request comment and save the comment.
-
/review- Manually triggers a full AI review. -
/pause- Pauses automatic reviews on this pull request. -
/resume- Resumes automatic reviews. -
/resolve- Marks all Bito-posted review comments as resolved. -
/abort- Cancels all in-progress reviews.
Refer to the documentation for additional commands.
Configuration
This repository uses Superset You can customize the agent settings here or contact your Bito workspace admin at evan@preset.io.
Documentation & Help
| async def get_chart_sql( | ||
| request: GetChartSqlRequest, ctx: Context | ||
| ) -> ChartSql | ChartError: | ||
| """Get the rendered SQL query for a chart without executing it. | ||
|
|
||
| Returns the SQL that a chart would execute, similar to the "View Query" | ||
| feature in the Superset UI. Useful for understanding, debugging, or | ||
| auditing the SQL generated by a chart's configuration. | ||
|
|
||
| Supports: | ||
| - Numeric ID or UUID lookup | ||
| - form_data_key: get SQL for unsaved chart state from Explore view | ||
|
|
||
| Example usage: | ||
| ```json | ||
| { | ||
| "identifier": 123 | ||
| } | ||
| ``` | ||
|
|
||
| Returns the SQL query string and metadata about the chart. | ||
| """ | ||
| await ctx.info( | ||
| "Starting chart SQL retrieval: identifier=%s, form_data_key=%s" | ||
| % (request.identifier, request.form_data_key) | ||
| ) | ||
|
|
There was a problem hiding this comment.
The get_chart_sql function body is incomplete - it only logs and lacks the core logic for processing the request, chart lookup, and SQL generation. This will cause a runtime error due to no return value. Add the missing implementation using the helper functions defined above.
Code Review Run #641514
Should Bito avoid suggestions like this for future reviews? (Manage Rules)
- Yes, avoid them
Code reviewFound 1 issue:
superset/superset/mcp_service/chart/tool/get_chart_sql.py Lines 41 to 43 in ceb613e superset/superset/mcp_service/chart/tool/get_chart_sql.py Lines 334 to 336 in ceb613e 🤖 Generated with Claude Code - If this code review was useful, please react with 👍. Otherwise, react with 👎. |
User description
SUMMARY
Two improvements to the MCP service based on user feedback around chart SQL retrieval and filter discoverability:
New
get_chart_sqltool — Returns the rendered SQL for a chart without executing it, similar to the "View Query" feature in the Superset UI. Useful for agents to inspect, debug, and audit chart-generated SQL. Supports saved charts (by ID/UUID) and unsaved chart state (viaform_data_key).Expose chart filters in
get_chart_info— Adds a structuredfiltersfield to theChartInforesponse that surfacesadhoc_filters,time_range,granularity_sqla,extra_filters, and customWHERE/HAVINGclauses. This allows MCP consumers to discover what filters are currently applied to a chart and construct filter payloads.Design decisions:
get_chart_sqltool rather than enrichingget_chart_infowith optional properties — the tool name is the documentation, keepsget_chart_infofast (SQL generation involves query builder + Jinja templating), and maps to distinct intents (get_chart_info= config,get_chart_sql= SQL,get_chart_data= data)get_chart_info(not a separate tool) since they're part of chart configurationBEFORE/AFTER SCREENSHOTS OR ANIMATED GIF
N/A — backend MCP tools only, no UI changes.
TESTING INSTRUCTIONS
get_chart_sqlwith a chart ID → should return the rendered SQLget_chart_infoon a chart with filters → the response should include afiltersfield with structured filter infoget_chart_sqlwith aform_data_keyfor unsaved chart state → should return SQL for the cached form_dataget_chart_sqlwith an invalid ID → should return a proper errorADDITIONAL INFORMATION
CodeAnt-AI Description
Add chart SQL lookup and expose applied filters in chart details
What Changed
Impact
✅ Easier chart debugging✅ Clearer filter inspection✅ Faster SQL review for saved and unsaved charts💡 Usage Guide
Checking Your Pull Request
Every time you make a pull request, our system automatically looks through it. We check for security issues, mistakes in how you're setting up your infrastructure, and common code problems. We do this to make sure your changes are solid and won't cause any trouble later.
Talking to CodeAnt AI
Got a question or need a hand with something in your pull request? You can easily get in touch with CodeAnt AI right here. Just type the following in a comment on your pull request, and replace "Your question here" with whatever you want to ask:
This lets you have a chat with CodeAnt AI about your pull request, making it easier to understand and improve your code.
Example
Preserve Org Learnings with CodeAnt
You can record team preferences so CodeAnt AI applies them in future reviews. Reply directly to the specific CodeAnt AI suggestion (in the same thread) and replace "Your feedback here" with your input:
This helps CodeAnt AI learn and adapt to your team's coding style and standards.
Example
Retrigger review
Ask CodeAnt AI to review the PR again, by typing:
Check Your Repository Health
To analyze the health of your code repository, visit our dashboard at https://app.codeant.ai. This tool helps you identify potential issues and areas for improvement in your codebase, ensuring your repository maintains high standards of code health.
MCP Tool Testing Results
Tested against running Superset MCP service (localhost:5008) on 2026-03-24:
health_check{}{"status":"healthy","service":"Preset MCP Service"}get_instance_info{}list_dashboards{"page":1,"page_size":3}list_charts{"page":1,"page_size":3}list_datasets{"page":1,"page_size":3}get_chart_info{"identifier":4}execute_sql{"database_id":2,"sql":"SELECT 1 as test_col"}generate_explore_link{"dataset_id":19,"config":{...}}All tools executed successfully with proper auth context (Admin user).