codeant-ai-for-open-source[bot] commented on code in PR #42244:
URL: https://github.com/apache/superset/pull/42244#discussion_r3616434149
##########
superset/mcp_service/middleware.py:
##########
@@ -1331,6 +1333,141 @@ def _try_truncate_info_response(
return truncated
+ def _try_truncate_data_query_response(
+ self,
+ tool_name: str,
+ response: Any,
+ estimated_tokens: int,
+ ) -> Any | None:
+ """Attempt to truncate a data-query tool response by dropping tail
rows.
+
+ Returns the truncated response if successful, None otherwise.
+ """
+ extracted = self._extract_payload_from_tool_result(response)
+ truncation_target = extracted if extracted is not None else response
+
+ try:
+ truncated, was_truncated, notes = truncate_query_result(
+ truncation_target, self.token_limit, tool_name=tool_name
+ )
+ except Exception as trunc_error: # noqa: BLE001
+ logger.warning(
+ "Query result truncation failed for %s due to %s: %s",
+ tool_name,
+ type(trunc_error).__name__,
+ trunc_error,
+ )
+ return None
+
+ if not was_truncated:
+ return None
+
+ # Mirror the info-tool path: if truncation couldn't bring the
+ # response back under the limit (e.g. a single row/scalar field
+ # alone exceeds it), fall back to the hard size-limit error instead
+ # of shipping an over-budget response.
+ truncated_tokens = estimate_response_tokens(truncated)
+ if truncated_tokens > self.token_limit:
+ return None
+
+ logger.warning(
+ "Query result for %s truncated from ~%d to ~%d tokens (limit: %d).
%s",
+ tool_name,
+ estimated_tokens,
+ truncated_tokens,
+ self.token_limit,
+ "; ".join(notes),
+ )
+
+ try:
+ user_id = get_user_id()
+ event_logger.log(
+ user_id=user_id,
+ action="mcp_response_truncated",
+ curated_payload={
+ "tool": tool_name,
+ "original_tokens": estimated_tokens,
+ "truncated_tokens": truncated_tokens,
+ "token_limit": self.token_limit,
+ "truncation_notes": notes,
+ },
+ )
+ except Exception as log_error: # noqa: BLE001
+ logger.warning("Failed to log truncation event: %s", log_error)
+
+ if extracted is not None and isinstance(truncated, dict):
+ return self._rewrap_as_tool_result(truncated, response)
+
Review Comment:
**Suggestion:** The size re-check is done on the unwrapped payload, but the
returned object may be a rewrapped `ToolResult` that is larger. For wrapped
tool responses this can still return an over-limit response even after
truncation. Recompute token size on the final object that will actually be
returned (or include wrapper overhead before deciding truncation succeeded).
[logic error]
<details>
<summary><b>Severity Level:</b> Major ⚠️</summary>
```mdx
- ⚠️ Data-query tools may emit responses exceeding token_limit.
- ⚠️ Upstream MCP client could reject oversize tool responses.
- ⚠️ Logs misreport truncated size, complicating debugging.
```
</details>
<details>
<summary><b>Steps of Reproduction ✅ </b></summary>
```mdx
1. Start the Superset MCP service so `ResponseSizeGuardMiddleware` in
`superset/mcp_service/middleware.py` is active for tool calls.
2. Execute a data-query MCP tool such as `execute_sql`, `query_dataset`, or
`get_chart_data` so `on_call_tool()` at lines 1471–1523 receives a large
`response` whose
`estimate_response_tokens(response)` exceeds `self.token_limit`.
3. `on_call_tool()` calls `_handle_oversized_response()` at lines 1403–1470,
which detects
`tool_name` in `DATA_QUERY_TOOLS` and calls
`_try_truncate_data_query_response()` at lines
1336–1401.
4. `_try_truncate_data_query_response()` unwraps any `ToolResult` via
`_extract_payload_from_tool_result()` (earlier in `middleware.py`) into
`extracted`,
passes only this payload (`truncation_target`) to `truncate_query_result()`,
and receives
a truncated payload dict in `truncated`.
5. At line 1369, it computes `truncated_tokens =
estimate_response_tokens(truncated)`
based on the payload alone; if `truncated_tokens <= self.token_limit` it
proceeds to
rewrap at line 1400 via `_rewrap_as_tool_result(truncated, response)` and
returns the
wrapped object, which can have a total token count above `self.token_limit`
because the
wrapper overhead was not included in the size re-check.
```
</details>
[](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=e5fa49d7f8754fba8abd1ee539353db1&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
[](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=e5fa49d7f8754fba8abd1ee539353db1&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
*(Use Cmd/Ctrl + Click for best experience)*
<details>
<summary><b>Prompt for AI Agent 🤖 </b></summary>
```mdx
This is a comment left during a code review.
**Path:** superset/mcp_service/middleware.py
**Line:** 1369:1400
**Comment:**
*Logic Error: The size re-check is done on the unwrapped payload, but
the returned object may be a rewrapped `ToolResult` that is larger. For wrapped
tool responses this can still return an over-limit response even after
truncation. Recompute token size on the final object that will actually be
returned (or include wrapper overhead before deciding truncation succeeded).
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.
Once fix is implemented, also check other comments on the same PR, and ask
user if the user wants to fix the rest of the comments as well. if said yes,
then fetch all the comments validate the correctness and implement a minimal fix
```
</details>
<a
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F42244&comment_hash=8ae6c24ba2a0e84d42fc1396027450ec34d96cba545e0f4b0c6d7900ebc4dad0&reaction=like'>👍</a>
| <a
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F42244&comment_hash=8ae6c24ba2a0e84d42fc1396027450ec34d96cba545e0f4b0c6d7900ebc4dad0&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: [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]