fix(core): add truncation support for MCP tool output#2446
Conversation
📋 Review SummaryThis PR successfully centralizes tool output truncation logic into a reusable 🔍 General Feedback
🎯 Specific Feedback🔴 Critical
🟡 High
🟢 Medium
🔵 Low
✅ Highlights
|
Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com> - Add truncateToolOutput helper in truncation.ts to centralize threshold reading, file saving, and telemetry logging - Refactor shell.ts to use the new helper, removing duplicate code - Add truncation support for MCP tool output while preserving non-text content (images, audio, resources) - Refactor getDisplayFromParts to work on transformed Part[] instead of raw MCP response This reduces code duplication and ensures consistent truncation behavior across shell and MCP tools.
8349901 to
6134757
Compare
Code Coverage Summary
CLI Package - Full Text ReportCore Package - Full Text ReportFor detailed HTML reports, please see the 'coverage-reports-22.x-ubuntu-latest' artifact from the main CI run. |
TLDR
Adds truncation support for MCP tool output to prevent large responses from overwhelming the context window. Also centralizes truncation logic into a reusable helper for consistency across tools.
Dive Deeper
MCP tools could return arbitrarily large text responses that would consume excessive context tokens. This PR fixes that by:
Adding truncation for MCP tool output - Large text responses are now truncated with the full content saved to a temp file, matching the behavior of shell tool output.
Preserving non-text content - Images, audio, and other binary data are NOT truncated since they don't contribute to token bloat in the same way.
Centralizing truncation logic - Introduces
truncateToolOutputhelper intruncation.tsthat handles threshold reading, file saving, and telemetry logging, reducing code duplication.Simplifying display logic - Refactors
getDisplayFromPartsto work on transformedPart[]instead of raw MCP response.Reviewer Test Plan
cd packages/core && npx vitest run src/tools/mcp-tool.test.tscd packages/core && npx vitest run src/tools/shell.test.tstruncateToolOutputThresholdandtruncateToolOutputLinesin settingsTesting Matrix
Linked issues / bugs
Related to #2439
🤖 Generated with Qwen Code