feat(core): truncate large MCP tool output#19365
Conversation
|
Hi @SandyTao520, thank you so much for your contribution to Gemini CLI! We really appreciate the time and effort you've put into this. We're making some updates to our contribution process to improve how we track and review changes. Please take a moment to review our recent discussion post: Improving Our Contribution Process & Introducing New Guidelines. Key Update: Starting January 26, 2026, the Gemini CLI project will require all pull requests to be associated with an existing issue. Any pull requests not linked to an issue by that date will be automatically closed. Thank you for your understanding and for being a part of our community! |
Summary of ChangesHello @SandyTao520, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request enhances the tool execution framework by implementing output truncation for MCP tools. Previously, only shell tools had this capability. The change ensures that large string responses from MCP servers are automatically truncated, with the full output saved to a temporary file, thereby managing the model's context window more effectively and improving system stability when dealing with verbose tool results. Highlights
Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
|
Size Change: +1.44 kB (+0.01%) Total Size: 25.8 MB
ℹ️ View Unchanged
|
There was a problem hiding this comment.
Code Review
This pull request aims to generalize tool output truncation logic for MCP tools. However, the current implementation is flawed: it only truncates string outputs, completely bypassing truncation for MCP tools which return Part[]. This critical vulnerability can lead to context window overflow, prompt injection, or bypass. Furthermore, the underlying truncation logic needs to be made Unicode-aware to correctly handle multi-byte characters, as its usage is being expanded, specifically by using grapheme-cluster aware methods for truncation as per repository guidelines.
15e34b9 to
ae755b3
Compare
gundermanc
left a comment
There was a problem hiding this comment.
Approved with maintainability suggestion.
c94fea7 to
2d191f4
Compare
Extend tool output truncation to MCP tools. When an MCP tool returns a Part[] with a single text Part exceeding the threshold, the text is truncated in-place while preserving the Part[] structure. MCP tools are identified via instanceof DiscoveredMCPTool rather than name-based checks, which fail for non-colliding tool names that lack the server__tool qualified format. Multi-part responses (e.g., mixed text + images) are left untouched.
2d191f4 to
446c1c1
Compare
Summary
Generalized tool output truncation to include MCP tools and improved truncation safety by making it Unicode-aware. This ensures that large string responses from MCP servers do not exceed the model's context window while preventing corruption of multi-byte characters.
Details
ToolExecutorto handle both raw strings andPart[]arrays containing a single text-based part (common for successful MCP tool responses).formatTruncatedToolOutputinfileUtils.tsto useIntl.Segmenterfor character slicing. This prevents splitting multi-byte characters like emojis when showing the head/tail of large outputs.__separator) and type-based (instanceof) checks for reliability across different registration contexts (e.g., within agents).Related Issues
Related to #18007
How to Validate
npm test -w @google/gemini-cli-core -- src/scheduler/tool-executor.test.tsPre-Merge Checklist