Skip to content

feat(core): truncate large MCP tool output#19365

Merged
SandyTao520 merged 2 commits intomainfrom
st/feat/mcp-output-truncation
Mar 2, 2026
Merged

feat(core): truncate large MCP tool output#19365
SandyTao520 merged 2 commits intomainfrom
st/feat/mcp-output-truncation

Conversation

@SandyTao520
Copy link
Copy Markdown
Contributor

@SandyTao520 SandyTao520 commented Feb 18, 2026

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

  • MCP Tool Truncation: Extended the truncation logic in ToolExecutor to handle both raw strings and Part[] arrays containing a single text-based part (common for successful MCP tool responses).
  • Unicode-Safe Truncation: Updated formatTruncatedToolOutput in fileUtils.ts to use Intl.Segmenter for character slicing. This prevents splitting multi-byte characters like emojis when showing the head/tail of large outputs.
  • Robust Identification: MCP tools are identified using both name-based (__ separator) and type-based (instanceof) checks for reliability across different registration contexts (e.g., within agents).

Related Issues

Related to #18007

How to Validate

  1. Run an MCP tool that returns a single text block larger than 40,000 characters.
  2. Verify that the output is truncated and the full output is saved to a file.
  3. Test with text containing emojis near the truncation boundary to verify no characters are split.
  4. Verify tests pass: npm test -w @google/gemini-cli-core -- src/scheduler/tool-executor.test.ts

Pre-Merge Checklist

  • Updated relevant documentation and README (if needed)
  • Added/updated tests (if needed)
  • Noted breaking changes (if any)
  • Validated on required platforms/methods:
    • MacOS
      • npm run

@SandyTao520 SandyTao520 requested a review from a team as a code owner February 18, 2026 04:17
@gemini-cli
Copy link
Copy Markdown
Contributor

gemini-cli bot commented Feb 18, 2026

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!

@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @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

  • Generalized Tool Output Truncation: The mechanism for truncating large tool outputs has been extended to include MCP (Managed Cloud Platform) tools, preventing their responses from exceeding model context windows.
  • MCP Tool Identification: MCP tools are now identified by the presence of MCP_QUALIFIED_NAME_SEPARATOR (__) in their names, allowing the system to apply the truncation logic appropriately.
  • New Test Coverage: A new unit test has been added to tool-executor.test.ts to specifically verify the correct truncation behavior for large MCP tool outputs.
Changelog
  • packages/core/src/scheduler/tool-executor.test.ts
    • Added a new test case to verify the truncation of large MCP tool outputs, including mocking dependencies for saving and formatting truncated content.
  • packages/core/src/scheduler/tool-executor.ts
    • Imported MCP_QUALIFIED_NAME_SEPARATOR to facilitate identification of MCP tools.
    • Modified the handleToolOutput method to apply output truncation logic to both shell tools and MCP tools, based on their names.
Activity
  • No human activity (comments, reviews) has been recorded on this pull request yet.
Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@github-actions
Copy link
Copy Markdown

github-actions bot commented Feb 18, 2026

Size Change: +1.44 kB (+0.01%)

Total Size: 25.8 MB

Filename Size Change
./bundle/gemini.js 25.3 MB +1.44 kB (+0.01%)
ℹ️ View Unchanged
Filename Size
./bundle/node_modules/@google/gemini-cli-devtools/dist/client/main.js 221 kB
./bundle/node_modules/@google/gemini-cli-devtools/dist/src/_client-assets.js 227 kB
./bundle/node_modules/@google/gemini-cli-devtools/dist/src/index.js 11.5 kB
./bundle/node_modules/@google/gemini-cli-devtools/dist/src/types.js 132 B
./bundle/sandbox-macos-permissive-open.sb 890 B
./bundle/sandbox-macos-permissive-proxied.sb 1.31 kB
./bundle/sandbox-macos-restrictive-open.sb 3.36 kB
./bundle/sandbox-macos-restrictive-proxied.sb 3.56 kB
./bundle/sandbox-macos-strict-open.sb 4.82 kB
./bundle/sandbox-macos-strict-proxied.sb 5.02 kB

compressed-size-action

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@SandyTao520 SandyTao520 force-pushed the st/feat/mcp-output-truncation branch 3 times, most recently from 15e34b9 to ae755b3 Compare February 18, 2026 05:45
Copy link
Copy Markdown
Member

@gundermanc gundermanc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved with maintainability suggestion.

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.
@SandyTao520 SandyTao520 force-pushed the st/feat/mcp-output-truncation branch from 2d191f4 to 446c1c1 Compare March 2, 2026 17:08
@SandyTao520 SandyTao520 enabled auto-merge March 2, 2026 17:16
@SandyTao520 SandyTao520 added this pull request to the merge queue Mar 2, 2026
Merged via the queue into main with commit ce5a2d0 Mar 2, 2026
27 checks passed
@SandyTao520 SandyTao520 deleted the st/feat/mcp-output-truncation branch March 2, 2026 21:20
BryanBradfo pushed a commit to BryanBradfo/gemini-cli that referenced this pull request Mar 5, 2026
struckoff pushed a commit to struckoff/gemini-cli that referenced this pull request Mar 6, 2026
liamhelmer pushed a commit to badal-io/gemini-cli that referenced this pull request Mar 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants