fix: return truncated content when token limit exceeded in MCP search_code #604
+19
−0
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