You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@superset.apache.org by "aminghadersohi (via GitHub)" <gi...@apache.org> on 2026/04/04 13:25:47 UTC

[PR] fix(mcp): add dynamic response truncation for info tools [superset]

aminghadersohi opened a new pull request, #39107:
URL: https://github.com/apache/superset/pull/39107

   ## Summary
   
   When MCP info tools (`get_chart_info`, `get_dataset_info`, `get_dashboard_info`, `get_instance_info`) return responses exceeding the configured token limit, the `ResponseSizeGuardMiddleware` now dynamically truncates large fields instead of blocking the response entirely with an error.
   
   ### Problem
   
   Datasets with many columns/metrics, dashboards with many charts, or charts with large `form_data` / long descriptions can produce responses that exceed the 25K token limit. Previously, these responses were completely blocked with an error message suggesting the user retry with `select_columns` — but info tools don't support pagination, so there was no good fallback for the LLM client.
   
   ### Solution
   
   Added progressive truncation that preserves partial data while fitting within the token budget:
   
   1. **Phase 1**: Truncate long string fields (description, css, sql) to 500 chars
   2. **Phase 2**: Truncate large list fields (columns, metrics, charts) to 30 items
   3. **Phase 3**: Aggressively reduce lists to 10 items and summarize large dicts
   4. **Phase 4**: Replace all collections with summary markers
   
   Non-info tools (list tools, `execute_sql`) continue to be blocked with actionable error messages as before.
   
   Truncated responses include `_response_truncated: true` and `_truncation_notes` metadata so the LLM client knows the response was shortened.
   
   ### Changes
   
   - `superset/mcp_service/utils/token_utils.py`: Added `truncate_oversized_response()` and helper functions
   - `superset/mcp_service/middleware.py`: Added `_try_truncate_info_response()` method to `ResponseSizeGuardMiddleware`
   - New monitoring event: `mcp_response_truncated` logged when truncation succeeds
   
   ### Testing
   
   - Added comprehensive unit tests for all truncation helpers
   - Added middleware integration tests for info tool truncation behavior
   - Verified non-info tools still blocked as before
   - All 64 unit tests pass


-- 
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: notifications-unsubscribe@superset.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org


Re: [PR] fix(mcp): add dynamic response truncation for oversized info tool responses [superset]

Posted by "aminghadersohi (via GitHub)" <gi...@apache.org>.
aminghadersohi merged PR #39107:
URL: https://github.com/apache/superset/pull/39107


-- 
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: notifications-unsubscribe@superset.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org


Re: [PR] fix(mcp): add dynamic response truncation for info tools [superset]

Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #39107:
URL: https://github.com/apache/superset/pull/39107#discussion_r3035584326


##########
superset/mcp_service/utils/token_utils.py:
##########
@@ -382,6 +382,164 @@ def _get_tool_specific_suggestions(
     return suggestions
 
 
+# Tools eligible for dynamic response truncation instead of hard blocking.
+# These tools return single objects (not paginated lists) where truncation
+# is preferable to returning an error.
+INFO_TOOLS = frozenset(
+    {
+        "get_chart_info",
+        "get_dataset_info",
+        "get_dashboard_info",
+        "get_instance_info",
+    }
+)
+
+# Maximum character length for string fields before truncation
+_MAX_STRING_CHARS = 500
+# Maximum items to keep in list fields before truncation
+_MAX_LIST_ITEMS = 30
+# Maximum keys to keep when summarizing large dict fields
+_MAX_DICT_KEYS = 20
+
+
+def _truncate_strings(
+    data: Dict[str, Any], notes: List[str], max_chars: int = _MAX_STRING_CHARS
+) -> bool:
+    """Truncate string fields exceeding max_chars. Returns True if any truncated."""
+    changed = False
+    for key, value in data.items():
+        if isinstance(value, str) and len(value) > max_chars:
+            original_len = len(value)
+            data[key] = value[:max_chars] + f"... [truncated from {original_len} chars]"
+            notes.append(f"Field '{key}' truncated from {original_len} chars")
+            changed = True
+    return changed
+
+
+def _truncate_lists(data: Dict[str, Any], notes: List[str], max_items: int) -> bool:
+    """Truncate list fields exceeding max_items. Returns True if any truncated."""
+    changed = False
+    for key, value in data.items():
+        if isinstance(value, list) and len(value) > max_items:
+            original_len = len(value)
+            data[key] = value[:max_items]
+            data[key].append(
+                {
+                    "_truncated": True,
+                    "_message": (
+                        f"Showing {max_items} of {original_len} items. "
+                        "Use select_columns or filters to narrow results."

Review Comment:
   The truncation marker appended by `_truncate_lists()` says “Use select_columns or filters to narrow results.” However, the MCP *info* tools that call this truncation path (e.g. `get_chart_info`, `get_dataset_info`, `get_dashboard_info`, `get_instance_info`) don’t accept `select_columns` or `filters` parameters in their request schemas, so this guidance is not actionable and may confuse the LLM client. Consider switching this message to something that applies to info tools (e.g., instruct the client to fetch details for specific items via other tools, or simply note that the list was truncated to fit the token limit).
   ```suggestion
                           "Results were truncated to fit the response size limit."
   ```



-- 
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: notifications-unsubscribe@superset.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org


Re: [PR] fix(mcp): add dynamic response truncation for info tools [superset]

Posted by "bito-code-review[bot] (via GitHub)" <gi...@apache.org>.
bito-code-review[bot] commented on PR #39107:
URL: https://github.com/apache/superset/pull/39107#issuecomment-4187115445

   AI Code Review is in progress (usually takes 3 to 15 minutes unless it's a very large PR).


-- 
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: notifications-unsubscribe@superset.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org


Re: [PR] fix(mcp): add dynamic response truncation for info tools [superset]

Posted by "codecov[bot] (via GitHub)" <gi...@apache.org>.
codecov[bot] commented on PR #39107:
URL: https://github.com/apache/superset/pull/39107#issuecomment-4187119048

   ## [Codecov](https://app.codecov.io/gh/apache/superset/pull/39107?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) Report
   :x: Patch coverage is `0%` with `96 lines` in your changes missing coverage. Please review.
   :white_check_mark: Project coverage is 54.54%. Comparing base ([`d796543`](https://app.codecov.io/gh/apache/superset/commit/d796543f5a0c5f6306f3140d48647640b7a28a14?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)) to head ([`24db1ad`](https://app.codecov.io/gh/apache/superset/commit/24db1ad25c901a8f75b99523d681c2d2e97f6d54?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)).
   
   | [Files with missing lines](https://app.codecov.io/gh/apache/superset/pull/39107?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | Patch % | Lines |
   |---|---|---|
   | [superset/mcp\_service/utils/token\_utils.py](https://app.codecov.io/gh/apache/superset/pull/39107?src=pr&el=tree&filepath=superset%2Fmcp_service%2Futils%2Ftoken_utils.py&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-c3VwZXJzZXQvbWNwX3NlcnZpY2UvdXRpbHMvdG9rZW5fdXRpbHMucHk=) | 0.00% | [69 Missing :warning: ](https://app.codecov.io/gh/apache/superset/pull/39107?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) |
   | [superset/mcp\_service/middleware.py](https://app.codecov.io/gh/apache/superset/pull/39107?src=pr&el=tree&filepath=superset%2Fmcp_service%2Fmiddleware.py&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-c3VwZXJzZXQvbWNwX3NlcnZpY2UvbWlkZGxld2FyZS5weQ==) | 0.00% | [27 Missing :warning: ](https://app.codecov.io/gh/apache/superset/pull/39107?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) |
   > :exclamation:  There is a different number of reports uploaded between BASE (d796543) and HEAD (24db1ad). Click for more details.
   > 
   > <details><summary>HEAD has 108 uploads less than BASE</summary>
   >
   >| Flag | BASE (d796543) | HEAD (24db1ad) |
   >|------|------|------|
   >|python|100|1|
   >|presto|10|1|
   ></details>
   
   <details><summary>Additional details and impacted files</summary>
   
   
   
   ```diff
   @@            Coverage Diff             @@
   ##           master   #39107      +/-   ##
   ==========================================
   - Coverage   64.52%   54.54%   -9.98%     
   ==========================================
     Files        2536     2536              
     Lines      131208   131304      +96     
     Branches    30457    30476      +19     
   ==========================================
   - Hits        84661    71623   -13038     
   - Misses      45084    59342   +14258     
   + Partials     1463      339    -1124     
   ```
   
   | [Flag](https://app.codecov.io/gh/apache/superset/pull/39107/flags?src=pr&el=flags&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | Coverage Δ | |
   |---|---|---|
   | [hive](https://app.codecov.io/gh/apache/superset/pull/39107/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `?` | |
   | [mysql](https://app.codecov.io/gh/apache/superset/pull/39107/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `?` | |
   | [postgres](https://app.codecov.io/gh/apache/superset/pull/39107/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `?` | |
   | [presto](https://app.codecov.io/gh/apache/superset/pull/39107/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `39.99% <0.00%> (-0.07%)` | :arrow_down: |
   | [python](https://app.codecov.io/gh/apache/superset/pull/39107/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `39.99% <0.00%> (-22.56%)` | :arrow_down: |
   | [sqlite](https://app.codecov.io/gh/apache/superset/pull/39107/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `?` | |
   | [unit](https://app.codecov.io/gh/apache/superset/pull/39107/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `?` | |
   
   Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#carryforward-flags-in-the-pull-request-comment) to find out more.
   </details>
   
   [:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/apache/superset/pull/39107?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache).   
   :loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache).
   <details><summary> :rocket: New features to boost your workflow: </summary>
   
   - :snowflake: [Test Analytics](https://docs.codecov.com/docs/test-analytics): Detect flaky tests, report on failures, and find test suite problems.
   - :package: [JS Bundle Analysis](https://docs.codecov.com/docs/javascript-bundle-analysis): Save yourself from yourself by tracking and limiting bundle sizes in JS merges.
   </details>


-- 
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: notifications-unsubscribe@superset.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org


Re: [PR] fix(mcp): add dynamic response truncation for info tools [superset]

Posted by "codeant-ai-for-open-source[bot] (via GitHub)" <gi...@apache.org>.
codeant-ai-for-open-source[bot] commented on code in PR #39107:
URL: https://github.com/apache/superset/pull/39107#discussion_r3035586539


##########
superset/mcp_service/utils/token_utils.py:
##########
@@ -382,6 +382,164 @@ def _get_tool_specific_suggestions(
     return suggestions
 
 
+# Tools eligible for dynamic response truncation instead of hard blocking.
+# These tools return single objects (not paginated lists) where truncation
+# is preferable to returning an error.
+INFO_TOOLS = frozenset(
+    {
+        "get_chart_info",
+        "get_dataset_info",
+        "get_dashboard_info",
+        "get_instance_info",
+    }
+)
+
+# Maximum character length for string fields before truncation
+_MAX_STRING_CHARS = 500
+# Maximum items to keep in list fields before truncation
+_MAX_LIST_ITEMS = 30
+# Maximum keys to keep when summarizing large dict fields
+_MAX_DICT_KEYS = 20
+
+
+def _truncate_strings(
+    data: Dict[str, Any], notes: List[str], max_chars: int = _MAX_STRING_CHARS
+) -> bool:
+    """Truncate string fields exceeding max_chars. Returns True if any truncated."""
+    changed = False
+    for key, value in data.items():
+        if isinstance(value, str) and len(value) > max_chars:
+            original_len = len(value)
+            data[key] = value[:max_chars] + f"... [truncated from {original_len} chars]"
+            notes.append(f"Field '{key}' truncated from {original_len} chars")
+            changed = True
+    return changed
+
+
+def _truncate_lists(data: Dict[str, Any], notes: List[str], max_items: int) -> bool:
+    """Truncate list fields exceeding max_items. Returns True if any truncated."""
+    changed = False
+    for key, value in data.items():
+        if isinstance(value, list) and len(value) > max_items:
+            original_len = len(value)
+            data[key] = value[:max_items]
+            data[key].append(
+                {
+                    "_truncated": True,
+                    "_message": (
+                        f"Showing {max_items} of {original_len} items. "
+                        "Use select_columns or filters to narrow results."
+                    ),

Review Comment:
   **Suggestion:** Appending a dict marker into every truncated list breaks list type contracts (for example, `get_instance_info` has multiple `List[str]` fields). After truncation those lists become mixed `str|dict`, which can break clients that parse or iterate them as strings only. Keep list element types unchanged and communicate truncation through notes/metadata instead. [logic error]
   
   <details>
   <summary><b>Severity Level:</b> Major ⚠️</summary>
   
   ```mdx
   - ❌ get_dataset_info truncated 'columns' list includes non-column marker object.
   - ⚠️ get_dashboard_info truncated 'charts' list includes non-chart marker object.
   ```
   </details>
   
   ```suggestion
   
   ```
   <details>
   <summary><b>Steps of Reproduction ✅ </b></summary>
   
   ```mdx
   1. Start the MCP service so that `ResponseSizeGuardMiddleware` is active in the middleware
   chain (defined in `superset/mcp_service/middleware.py:69-120` and used for all tool
   calls).
   
   2. Call the MCP tool `get_dataset_info` (async function in
   `superset/mcp_service/dataset/tool/get_dataset_info.py:4-90`) for a dataset that has a
   very large number of columns/metrics so that the response size exceeds the configured
   token limit (25k by default).
   
   3. The tool returns a `DatasetInfo` Pydantic model
   (`superset/mcp_service/dataset/schemas.py:10-63`), whose `columns` field is
   `List[TableColumnInfo]` and `metrics` is `List[SqlMetricInfo]`;
   `ResponseSizeGuardMiddleware.on_call_tool` then computes `estimated_tokens` and, seeing it
   above the limit for tool `get_dataset_info` (included in `INFO_TOOLS` at
   `superset/mcp_service/utils/token_utils.py:49-55`), calls `_try_truncate_info_response`
   which in turn calls `truncate_oversized_response` (`middleware.py:955` and
   `token_utils.py:150-201`).
   
   4. Inside `truncate_oversized_response`, the `DatasetInfo` model is converted to a dict
   via `model_dump()`, and `_truncate_lists`
   (`superset/mcp_service/utils/token_utils.py:80-100`) processes top-level list fields like
   `"columns"` and `"metrics"`. For each list longer than `_MAX_LIST_ITEMS` (30), the
   existing code at `425-435` sets `data[key] = value[:max_items]` and then appends a marker
   dict with `_truncated` and `_message` keys. The resulting JSON for `columns`/`metrics` is
   therefore a list of column/metric objects plus a trailing marker object. While this does
   not break any code within this repository (no in-repo callers assume a pure element type),
   it can violate the advertised MCP schema contract (`columns: List[TableColumnInfo]`,
   `metrics: List[SqlMetricInfo]`) for external clients and force them to handle a sentinel
   pseudo-element with a different shape than real entries.
   ```
   </details>
   <details>
   <summary><b>Prompt for AI Agent 🤖 </b></summary>
   
   ```mdx
   This is a comment left during a code review.
   
   **Path:** superset/mcp_service/utils/token_utils.py
   **Line:** 426:432
   **Comment:**
   	*Logic Error: Appending a dict marker into every truncated list breaks list type contracts (for example, `get_instance_info` has multiple `List[str]` fields). After truncation those lists become mixed `str|dict`, which can break clients that parse or iterate them as strings only. Keep list element types unchanged and communicate truncation through notes/metadata instead.
   
   Validate the correctness of the flagged issue. If correct, How can I resolve this? If you propose a fix, implement it and please make it concise.
   ```
   </details>
   <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F39107&comment_hash=6a25cd73d68f7887c9fecb82a8ea7a74beb353d6a5ff9a6ec461ce78c3262724&reaction=like'>👍</a> | <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F39107&comment_hash=6a25cd73d68f7887c9fecb82a8ea7a74beb353d6a5ff9a6ec461ce78c3262724&reaction=dislike'>👎</a>



-- 
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: notifications-unsubscribe@superset.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org


Re: [PR] fix(mcp): add dynamic response truncation for oversized info tool responses [superset]

Posted by "bito-code-review[bot] (via GitHub)" <gi...@apache.org>.
bito-code-review[bot] commented on PR #39107:
URL: https://github.com/apache/superset/pull/39107#issuecomment-4187235866

   AI Code Review is in progress (usually takes 3 to 15 minutes unless it's a very large PR).
   
   <details>
   <summary>
   Bito Usage Guide
   </summary>
   
   **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 <a href="https://docs.bito.ai/bito-dev-agents/ai-code-review-agent/available-commands" target="_blank" rel="noopener noreferrer">documentation</a> for additional commands.
         
   **Configuration**
   
   This repository uses `Superset` You can customize the agent settings <a href="https://alpha.bito.ai/home/ai-agents/code-review-agent" target="_blank" rel="noopener noreferrer">here</a> or contact your Bito workspace admin at evan@preset.io.
         
   **Documentation & Help**
   - <a href="https://docs.bito.ai/bito-dev-agents/ai-code-review-agent/getting-started/install-run-using-bito-cloud/create-or-customize-an-agent-instance" target="_blank" rel="noopener noreferrer">Customize agent settings</a>
   - <a href="https://docs.bito.ai/bito-dev-agents/ai-code-review-agent/implementing-custom-code-review-rules" target="_blank" rel="noopener noreferrer">Review rules</a>
   - <a href="https://docs.bito.ai/bito-dev-agents/ai-code-review-agent" target="_blank" rel="noopener noreferrer">General documentation</a>
   - <a href="https://docs.bito.ai/bito-dev-agents/ai-code-review-agent/faqs" target="_blank" rel="noopener noreferrer">FAQ</a>
   
   </details>
   


-- 
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: notifications-unsubscribe@superset.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org