Bugfix: Add max_line_length to search_code_advanced to prevent token flooding #22
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.
Introduces a new
max_line_length
parameter to thesearch_code_advanced
tool to address an issue where search results containing very long lines (e.g., from minified JavaScript files) could lead to excessive token usage and unexpected behavior when consumed by Large Language Models (LLMs).Problem:
When using the
search_code_advanced
tool withcontext_lines
, if a match is found in a file with extremely long lines (such as a.min.js
file), the entire line is returned as context. This can result in a massive, unexpected output, leading to:Solution:
This PR introduces a
max_line_length
parameter to thesearch_code_advanced
tool, which defaults to200
characters. This parameter truncates any line in the search results that exceeds the specified length, appending... (truncated)
to indicate that the line has been shortened.Key Changes:
src/code_index_mcp/server.py
: Thesearch_code_advanced
tool now accepts amax_line_length
parameter with a default value of200
.src/code_index_mcp/services/search_service.py
: Thesearch_code
method now accepts and passes themax_line_length
parameter to the underlying search strategies.src/code_index_mcp/search/base.py
: Theparse_search_output
function now includes logic to truncate lines based onmax_line_length
. TheSearchStrategy
abstract base class has been updated to include this parameter in thesearch
method.src/code_index_mcp/search/*.py
: All concreteSearchStrategy
implementations (ugrep
,ripgrep
,ag
,grep
, andbasic
) have been updated to accept and utilize themax_line_length
parameter.