Skip to content

MCP tool results bypass output truncation logic #2439

@tanzhenxin

Description

@tanzhenxin

What happened?

MCP tool results are not subject to any output truncation before being sent to the LLM context. Built-in tools like shell, grep, and ripGrep all apply truncation via truncateAndSaveToFile() (default: 25,000 chars / 1,000 lines), but MCP tool execution paths (executeWithDirectClient and executeWithCallableTool in mcp-tool.ts) return raw results directly without any size limiting.

This means an MCP server that returns a very large response (e.g., a database query tool returning thousands of rows, or a browser automation tool returning full page HTML) will flood the LLM context window, potentially degrading response quality, increasing token costs, and risking context overflow.

What did you expect to happen?

MCP tool results should be truncated using the same (or similar) logic as built-in tools, respecting truncateToolOutputThreshold and truncateToolOutputLines config values. When truncated, the full output should be saved to a temp file with a pointer, just like the shell tool does.

Client information

Client Information

N/A — this is a code-level bug found via source review, not runtime behavior.

Anything else we need to know?

The fix likely involves:

  1. Extracting text content from the MCP result parts after transformMcpContentToParts()
  2. Applying truncateAndSaveToFile() (or a similar check) to any text parts that exceed the configured thresholds
  3. Emitting logToolOutputTruncated telemetry, consistent with the shell tool

Metadata

Metadata

Assignees

No one assigned

    Labels

    type/bugSomething isn't working as expected

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions