fix: return truncated content when token limit exceeded in MCP search_code#604
Merged
brendan-kellam merged 1 commit intosourcebot-dev:mainfrom Nov 10, 2025
Merged
Conversation
Contributor
|
Important Review skippedAuto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the You can disable this status message by setting the ✨ Finishing touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
c5b8fda to
e9903f4
Compare
Contributor
|
LGTM, this approach makes sense. Thanks for the contribution 👍 Left one comment, once resolved happy to merge |
…_code When search results exceed maxTokens limit, now returns partial truncated content instead of discarding the file completely. Changes: - Calculate remaining token budget before breaking - Truncate file content to fit within remaining tokens (if > 100 tokens left) - Append truncation marker to indicate content was cut off - Still add truncation message at end of all results Benefits: - Users get partial data instead of nothing - Better debugging and analysis experience - More useful for AI-powered code analysis tasks - Consistent with expected behavior when limits are reached Example: If file would use 10K tokens but only 2K remain, return first ~8K chars of content + truncation marker instead of dropping it. Signed-off-by: Wayne Sun <gsun@redhat.com>
d99c047 to
a1da34a
Compare
brendan-kellam
approved these changes
Nov 7, 2025
brendan-kellam
approved these changes
Nov 10, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes token truncation behavior in the MCP
search_codetool to return partial content instead of discarding files completely when token limits are exceeded.Problem
Currently, when a search result file would exceed the
maxTokenslimit, the entire file is discarded with only a truncation message returned. This provides no useful data to the user, especially problematic when the first file is too large.Example failure scenario:
maxTokens=6000Solution
Modified the truncation logic in
packages/mcp/src/index.tsto:...[content truncated due to token limit]Changes
File:
packages/mcp/src/index.ts(lines 125-142)Before:
After:
Benefits
✅ Users receive partial data instead of nothing
✅ Better debugging and analysis experience
✅ More useful for AI-powered code analysis workflows
✅ Consistent with expected truncation behavior
✅ Maintains backward compatibility (still includes truncation message)
Example Impact
Scenario: Search returns 50 files, each ~2K tokens, with
maxTokens=10000Before: Returns first 5 complete files (10K tokens), discards file #6 completely
After: Returns first 5 complete files + truncated version of file #6
Testing
Related Issues
This fix specifically addresses issues observed in AI agent workflows where large codebases trigger token limits on early results, providing zero useful data to analysis tasks.
Commit: c5b8fda
Branch:
fix/mcp-search-truncation