aminghadersohi opened a new pull request, #42244: URL: https://github.com/apache/superset/pull/42244
## SUMMARY `execute_sql`, `query_dataset`, and `get_chart_data` raised a hard "Response too large" error whenever their result exceeded the MCP response token limit, even when the response was only a few rows over. This extends `ResponseSizeGuardMiddleware` to truncate these three tools the same way info tools (`get_chart_info`, `get_dataset_info`, etc.) are already truncated, instead of hard-failing: - Binary-search the largest row-count prefix that keeps the serialized response under the token limit, returning the truncated result instead of erroring. - Handle CSV chart-data exports, where the payload lives in the scalar `csv_data` field (`data=[]`) rather than a row list, by truncating that field directly. `excel_data` is base64-encoded binary and is left untouched, since truncating it would produce a corrupt file. - Re-check the truncated response's size before returning it (mirroring the existing info-tool path), so a single row/CSV blob that alone exceeds the limit falls back to the informative size-limit error instead of silently shipping an over-budget response. The bisection reserves headroom for the truncation-note metadata up front so this fallback only triggers when truncation genuinely can't fit. - Tailor the truncation-note wording per tool (`limit`/`row_limit` parameter vs. SQL `LIMIT` clause) instead of always suggesting a SQL `LIMIT`. ## BEFORE/AFTER **Before:** `execute_sql`/`query_dataset`/`get_chart_data` responses that exceeded the token limit always raised `ToolError: Response too large`, even if truncating a handful of rows would have brought them under the limit. **After:** these tools truncate the row set (or CSV content) and return a partial result with `_response_truncated` / `_truncation_notes` set, falling back to the hard error only when truncation genuinely cannot bring the response under the limit. ## TESTING INSTRUCTIONS - `pytest tests/unit_tests/mcp_service/` (3086 passed) - `ruff check` / `ruff format --diff` / `mypy` on the changed files ## ADDITIONAL INFORMATION - [x] Has associated tests - [ ] Confirm this PR does not include user-facing docs strings changes requiring `docs/` updates -- 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]
