Repository navigation
fix(mcp): stop sending raw exception text to MCP clients - #46611
Draft
aminghadersohi wants to merge 17 commits into
Draft
aminghadersohi wants to merge 17 commits into
aminghadersohi wants to merge 17 commits into
Conversation
MCP tools put exception text into tool responses and ctx log notifications at about 35 call sites. get_chart_data and query_dataset are reachable by embedded guests and returned warehouse error text to them, while /api/v1/chart/data passes the same message through sanitize_error_message. Other catch-all handlers sent str(exc) from SQLAlchemy and drivers to every caller. Add one helper, safe_error, that maps an exception to a SupersetErrorType and a client-safe message with the HTTP rule: Superset's own message (the engine error of a failed query) for signed-in users, the generic message for embedded guests unless the error type is one Superset writes itself, and a fixed message for exceptions Superset did not raise. internal_error_response and report_tool_error build tool responses and ctx notifications on it, and log the full exception server-side. A static test scans the MCP service for exception text formatted into client output outside these helpers.
Build the tool error helpers on the query-error classification in superset/mcp_service/utils/tool_errors.py, whose public_query_error maps a failed data query to an error code and a safe message with the rule of /api/v1/chart/data. get_chart_data, query_dataset and the chart compile check pass query=True (and semantic=True for a semantic view) so their responses and ctx notifications use that rule. Superset exceptions it does not classify keep the general rule, and raw SQLAlchemy errors keep a fixed message.
The tool error helpers read is_sanitization_required through superset.utils.error_sanitization, like sanitize_error_message, so one patch covers the general and the query rule.
PENDING_HANDLERS covers get_table handlers that a separate change moves to public_query_error. The stale-entry test no longer reports those entries, so the test passes whichever change lands first.
A ValueError raised by superset.utils.date_parser, the MCP chart mapper or a chart plugin describes the caller's time range or chart config, and the caller needs it to fix the request. safe_error reports it like a Superset message (the generic text for embedded guests); a ValueError raised anywhere else still gets the fixed message.
safe_error classifies an exception raised inside a semantic provider call with public_query_error(semantic=True) whatever the caller passes, so provider text never reaches the client. Validator ValueErrors about the caller's arguments are checked first and keep their text on semantic queries too. The chart compile check, the unsaved preview path and the get_chart_preview strategies report query failures with query=True and the semantic flag of their datasource, like get_chart_data.
public_query_error returns a semantic query's validation or rejection text without sanitize_error_message. On the query path safe_error gives an embedded guest the generic message unless the error type is one Superset writes itself, as /api/v1/chart/data does for the same exceptions; signed-in users keep the text.
ValueErrors raised by chart_helpers (for example a missing Deck.gl spatial setting) and by the time_range validator describe the caller's own chart config or time range. Add both modules to HOST_VALUE_ERROR_MODULES so the compile check and get_chart_sql return that text again.
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## master #46611 +/- ##
==========================================
- Coverage 65.80% 65.79% -0.01%
==========================================
Files 3146 3146
Lines 220020 220292 +272
Branches 52111 52157 +46
==========================================
+ Hits 144781 144950 +169
- Misses 71807 71909 +102
- Partials 3432 3433 +1
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
A tool that reports an error and re-raises left the response text to the middleware, which echoed the exception text for guests and for exceptions Superset did not write. The error handler and the compatibility boundary now render client text through the shared classification, and a provider-marked ValueError from a host validator gets the generic message.
The tool runs under its own app context, so the error middleware could not see that the call resolved to an embedded guest and rendered Superset text for it. The resolved principal now survives the context pop, as the user id does, and the response redaction uses it.
✅ Deploy Preview for superset-docs-preview ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
Auth failures, ambiguous column references, an unsupported chart source family and a late plugin preview refusal raised plain ValueErrors from modules outside the host-validation list, so the response boundary replaced their Superset-written text with the generic error. They now raise MCPValidationError (MCPNoAuthSourceError, MCPAuthConfigError and AmbiguousDatasetReferenceError subclass it), which signed-in callers see and embedded guests still get sanitized.
Exercise shared and distinct host/singleton apps so credential guidance tests do not depend on collection order. Keep the signed-in guidance and embedded-guest redaction assertions. Cover both unsupported-source guards for preview and persistence updates.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
SUMMARY
MCP tools put raw exception text into tool responses and into
ctx.error/ctx.warningnotifications, which FastMCP sends to the client. That text can come from SQLAlchemy, a database driver, the warehouse or a semantic-layer provider, so it can include SQL, bound parameters, hostnames or connection details.get_chart_datais on the embedded guest allow-list (MCP_GUEST_ALLOWED_TOOLS), and it returned warehouse error text to embedded guests. For the same failure,/api/v1/chart/datapasses the message throughsanitize_error_message.This PR makes every MCP tool report errors with the rule the HTTP API already uses:
get_chart_datasaved and unsaved paths,get_chart_preview, the unsaved preview path,query_dataset, the chart compile check) is classified bypublic_query_error, the same rule as/api/v1/chart/data. Signed-in users get the engine text, which they need to correct the query. Embedded guests get the generic message unless the error type is inSAFE_ERROR_TYPES, including for semantic-view queries, which passsemantic=True.is_provider_fault) is classified withsemantic=Trueon every tool path, whatever the caller passes, so provider text never reaches the client.SupersetException,CommandException) keeps its message andSupersetErrorType. Embedded guests only see it when the type is inSAFE_ERROR_TYPES.ValueErrorraised by one of Superset's validators of the caller's own arguments (superset.utils.date_parser, the MCPtime_rangevalidator, the MCP chart mapper and query builder inchart_utils/chart_helpers, the chart plugins) keeps its text, for example "From date cannot be larger than to date" or "Table chart must have at least one column". The caller needs that text to correct the request. Embedded guests still get the generic message. The innermost traceback frame decides where the error was raised.Shared helper. The helper lives in
superset/mcp_service/utils/tool_errors.py. This module is shared with #46598, which adds it forget_table. The first 141 lines of the file are byte-identical to that PR's version, and so are itsis_provider_faultrename hunks insuperset/semantic_layers/exceptions.py. This PR only appends new functions belowpublic_query_error:safe_error,tool_error_message,log_tool_exception,internal_error_responseandreport_tool_error. Whichever PR merges second gets a git add/add conflict in that file. The conflict is limited to the appended block: keep both sides. I checked this with a local merge of both PR heads. The only conflict was the appended block oftool_errors.py, and after resolving it the scan,get_tableand helper tests pass (312 passed).get_tableitself is not changed here.Call sites changed (paths under
superset/mcp_service):get_annotation_layer_info,get_layer_annotation_info,list_annotation_layers,list_layer_annotationschart_utils,compile,plugins/geographic,preview_utils,delete_chart,generate_chart,get_chart_data,get_chart_info,get_chart_preview,get_chart_sql,list_charts,restore_chart,update_chart,update_chart_preview,validation/pipelineadd_chart_to_existing_dashboard,apply_dashboard_filters,delete_dashboard,duplicate_dashboard,generate_dashboard,get_dashboard_data,get_dashboard_datasets,get_dashboard_info,get_dashboard_layout,manage_native_filters,remove_chart_from_dashboard,restore_dashboardget_database_info,list_databasescreate_dataset,create_dataset_column,create_dataset_metric,create_virtual_dataset,delete_dataset,delete_dataset_column,delete_dataset_metric,get_dataset_info,list_datasets,query_dataset,restore_dataset,update_dataset,update_dataset_column,update_dataset_metricgenerate_explore_linkget_query_info,list_queriesget_report_info,list_reportsget_rls_filter_info,list_rls_filtersget_role_info,list_rolesget_saved_query_info,list_saved_queriesexecute_sql,open_sql_lab_with_context,save_sql_queryget_instance_infoget_tag_info,list_tagsget_task_info,list_taskscreate_theme,get_theme_info,list_themesget_user_info,list_usersmcp_core,utils/schema_utilsNot changed, by design:
get_tablehandlers are converted by fix(mcp): return safe get_table error codes #46598. They are listed inPENDING_HANDLERS, which the stale-entry check does not report, so either PR can merge first.middleware.py(the last-resort handler) andjwt_verifier.pyalready keep exception text server-side.HOST_MESSAGE_HANDLERS, each with a reason.Guard against regressions.
tests/unit_tests/mcp_service/test_tool_error_text.pyparses every module undersuperset/mcp_service. It fails when anexcepthandler, or a function that takes an exception, turns the exception into text (str(e),f"{e}","%s" % e,e.message,bounded_exception_message(e), ...) that goes to a tool response or actxnotification without going through these helpers.BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF
N/A (backend only).
Before, an embedded guest calling
get_chart_dataon a chart whose query fails receivesError retrieving chart data: Error: relation "<schema>.<table>" does not exist. After, the guest receivesError retrieving chart data: An error occurred while fetching the data., and a signed-in user still gets the engine text.TESTING INSTRUCTIONS
Regression tests that fail on master and pass on this branch:
tests/unit_tests/mcp_service/test_tool_error_text.py::test_mcp_service_sends_no_exception_text_to_clients: the static scan. On master it lists the raw-text call sites.tests/unit_tests/mcp_service/chart/tool/test_get_chart_data.py::test_chart_data_engine_error_is_redacted_for_guests: covers saved and unsaved charts, failed and invalid queries. Guest cases fail on master; signed-in cases keep the engine text and pass on both.tests/unit_tests/mcp_service/dataset/tool/test_query_dataset.py::test_query_dataset_superset_error_text_for_principal(guest cases) and::test_query_dataset_failure_text_not_sent(SQLAlchemy and otherValueErrorcases).tests/unit_tests/mcp_service/utils/test_tool_errors.py: the helper rules, includingquery=Trueagainstpublic_query_error, semantic provider faults, and validatorValueErrors versus libraryValueErrors.tests/unit_tests/mcp_service/chart/tool/test_get_chart_data.py::test_semantic_chart_validation_text_is_redacted_for_guests.tests/unit_tests/mcp_service/chart/test_preview_utils.py::test_generate_preview_hides_semantic_provider_textandtests/unit_tests/mcp_service/chart/tool/test_generate_chart.py::TestCompileChart::test_compile_chart_hides_semantic_provider_text.Tests that keep caller guidance (they pass on master, which returned
str(e), and they pin that this change keeps the text):tests/unit_tests/mcp_service/chart/tool/test_get_chart_data.py::test_chart_data_time_range_error_keeps_date_parser_guidancetests/unit_tests/mcp_service/chart/tool/test_generate_chart.py::test_generate_chart_failure_keeps_chart_config_guidancetests/unit_tests/mcp_service/chart/tool/test_generate_chart.py::TestCompileChart::test_compile_chart_time_range_error_keeps_guidancetests/unit_tests/mcp_service/chart/tool/test_generate_chart.py::TestCompileChart::test_compile_chart_chart_config_error_keeps_guidanceADDITIONAL INFORMATION