Skip to main content
Glama

search_code_advanced

Search for code patterns in projects using optimized command-line tools with pagination, regex, fuzzy matching, and file filtering capabilities.

Instructions

Search for a code pattern in the project using an advanced, fast tool with pagination support.

This tool automatically selects the best available command-line search tool
(like ugrep, ripgrep, ag, or grep) for maximum performance.

Args:
    pattern: The search pattern. Can be literal text or regex (see regex parameter).
    case_sensitive: Whether the search should be case-sensitive.
    context_lines: Number of lines to show before and after the match.
    file_pattern: A glob pattern to filter files to search in
                 (e.g., "*.py", "*.js", "test_*.py").
                 All search tools now handle glob patterns consistently:
                 - ugrep: Uses glob patterns (*.py, *.{js,ts})
                 - ripgrep: Uses glob patterns (*.py, *.{js,ts})
                 - ag (Silver Searcher): Automatically converts globs to regex patterns
                 - grep: Basic glob pattern matching
                 All common glob patterns like "*.py", "test_*.js", "src/*.ts" are supported.
    fuzzy: If True, enables fuzzy/partial matching behavior varies by search tool:
           - ugrep: Native fuzzy search with --fuzzy flag (true edit-distance fuzzy search)
           - ripgrep, ag, grep, basic: Word boundary pattern matching (not true fuzzy search)
           IMPORTANT: Only ugrep provides true fuzzy search. Other tools use word boundary
           matching which allows partial matches at word boundaries.
           For exact literal matches, set fuzzy=False (default and recommended).
    regex: Controls regex pattern matching behavior:
           - If True, enables regex pattern matching
           - If False, forces literal string search
           - If None (default), automatically detects regex patterns and enables regex for patterns like "ERROR|WARN"
           The pattern will always be validated for safety to prevent ReDoS attacks.
    start_index: Zero-based offset into the flattened match list. Use to fetch subsequent pages.
    max_results: Maximum number of matches to return (default 10). Pass None to retrieve all matches.

Returns:
    A dictionary containing:
    - results: List of matches with file, line, and text keys.
    - pagination: Metadata with total_matches, returned, start_index, end_index, has_more,
                  and optionally max_results.
    If an error occurs, an error message is returned instead.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
patternYes
case_sensitiveNo
context_linesNo
file_patternNo
fuzzyNo
regexNo
start_indexNo
max_resultsNo

Output Schema

TableJSON Schema
NameRequiredDescriptionDefault
resultYes

Implementation Reference

  • The MCP tool handler and registration for 'search_code_advanced'. Decorated with @mcp.tool(), includes input schema via parameters and comprehensive docstring describing args and returns. Delegates execution to SearchService.search_code.
    @mcp.tool()
    @handle_mcp_tool_errors(return_type='dict')
    def search_code_advanced(
        pattern: str,
        ctx: Context,
        case_sensitive: bool = True,
        context_lines: int = 0,
        file_pattern: str = None,
        fuzzy: bool = False,
        regex: bool = None,
        start_index: int = 0,
            max_results: Optional[int] = 10
    ) -> Dict[str, Any]:
        """
        Search for a code pattern in the project using an advanced, fast tool with pagination support.
    
        This tool automatically selects the best available command-line search tool
        (like ugrep, ripgrep, ag, or grep) for maximum performance.
    
        Args:
            pattern: The search pattern. Can be literal text or regex (see regex parameter).
            case_sensitive: Whether the search should be case-sensitive.
            context_lines: Number of lines to show before and after the match.
            file_pattern: A glob pattern to filter files to search in
                         (e.g., "*.py", "*.js", "test_*.py").
                         All search tools now handle glob patterns consistently:
                         - ugrep: Uses glob patterns (*.py, *.{js,ts})
                         - ripgrep: Uses glob patterns (*.py, *.{js,ts})
                         - ag (Silver Searcher): Automatically converts globs to regex patterns
                         - grep: Basic glob pattern matching
                         All common glob patterns like "*.py", "test_*.js", "src/*.ts" are supported.
            fuzzy: If True, enables fuzzy/partial matching behavior varies by search tool:
                   - ugrep: Native fuzzy search with --fuzzy flag (true edit-distance fuzzy search)
                   - ripgrep, ag, grep, basic: Word boundary pattern matching (not true fuzzy search)
                   IMPORTANT: Only ugrep provides true fuzzy search. Other tools use word boundary
                   matching which allows partial matches at word boundaries.
                   For exact literal matches, set fuzzy=False (default and recommended).
            regex: Controls regex pattern matching behavior:
                   - If True, enables regex pattern matching
                   - If False, forces literal string search
                   - If None (default), automatically detects regex patterns and enables regex for patterns like "ERROR|WARN"
                   The pattern will always be validated for safety to prevent ReDoS attacks.
            start_index: Zero-based offset into the flattened match list. Use to fetch subsequent pages.
            max_results: Maximum number of matches to return (default 10). Pass None to retrieve all matches.
    
        Returns:
            A dictionary containing:
            - results: List of matches with file, line, and text keys.
            - pagination: Metadata with total_matches, returned, start_index, end_index, has_more,
                          and optionally max_results.
            If an error occurs, an error message is returned instead.
    
        """
        return SearchService(ctx).search_code(
            pattern=pattern,
            case_sensitive=case_sensitive,
            context_lines=context_lines,
            file_pattern=file_pattern,
            fuzzy=fuzzy,
            regex=regex,
            start_index=start_index,
            max_results=max_results
        )
  • Core implementation logic of the search functionality in SearchService.search_code method. Handles validation, strategy selection, execution via search strategy, result filtering, pagination, and response formatting.
    def search_code(  # pylint: disable=too-many-arguments, too-many-locals
        self,
        pattern: str,
        case_sensitive: bool = True,
        context_lines: int = 0,
        file_pattern: Optional[str] = None,
        fuzzy: bool = False,
        regex: Optional[bool] = None,
        start_index: int = 0,
        max_results: Optional[int] = 10
    ) -> Dict[str, Any]:
        """Search for code patterns in the project."""
        self._require_project_setup()
    
        if regex is None:
            regex = is_safe_regex_pattern(pattern)
    
        error = ValidationHelper.validate_search_pattern(pattern, regex)
        if error:
            raise ValueError(error)
    
        if file_pattern:
            error = ValidationHelper.validate_glob_pattern(file_pattern)
            if error:
                raise ValueError(f"Invalid file pattern: {error}")
    
        pagination_error = ValidationHelper.validate_pagination(start_index, max_results)
        if pagination_error:
            raise ValueError(pagination_error)
    
        if not self.settings:
            raise ValueError("Settings not available")
    
        strategy = self.settings.get_preferred_search_tool()
        if not strategy:
            raise ValueError("No search strategies available")
    
        self._configure_strategy(strategy)
    
        try:
            results = strategy.search(
                pattern=pattern,
                base_path=self.base_path,
                case_sensitive=case_sensitive,
                context_lines=context_lines,
                file_pattern=file_pattern,
                fuzzy=fuzzy,
                regex=regex
            )
            filtered = self._filter_results(results)
            formatted_results, pagination = self._paginate_results(
                filtered,
                start_index=start_index,
                max_results=max_results
            )
            return ResponseFormatter.search_results_response(
                formatted_results,
                pagination
            )
        except Exception as exc:
            raise ValueError(f"Search failed using '{strategy.name}': {exc}") from exc
  • The @mcp.tool() decorator registers the search_code_advanced tool with the MCP server.
    @mcp.tool()
Behavior5/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations provided, the description carries the full burden of behavioral disclosure and does so comprehensively. It describes performance characteristics ('fast tool with pagination support'), tool selection logic, safety features (ReDoS attack prevention), return format, error handling, and detailed behavior variations across different underlying search tools. This provides rich behavioral context beyond basic functionality.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness4/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is well-structured with clear sections (purpose, tool selection, parameter details, return format) and uses bullet points for complex information. While comprehensive, some sections (like the detailed tool-specific behavior for file_pattern and fuzzy) could be more concise. Every sentence earns its place by providing necessary information, but there's minor redundancy in explaining tool variations.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness5/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the tool's complexity (8 parameters, no annotations, but with output schema), the description is remarkably complete. It covers purpose, usage context, behavioral details, parameter semantics, return format, and error handling. The output schema existence means the description doesn't need to detail return structure, but it still provides a helpful summary. This provides all necessary context for an AI agent to use the tool effectively.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters5/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

With 0% schema description coverage for 8 parameters, the description fully compensates by providing extensive semantic information for every parameter. Each parameter gets detailed explanations: pattern types (literal vs regex), case_sensitive behavior, context_lines usage, file_pattern glob syntax with tool-specific implementations, fuzzy matching variations across tools, regex behavior with automatic detection, and pagination parameters (start_index, max_results). This adds significant value beyond the bare schema.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the tool's purpose: 'Search for a code pattern in the project using an advanced, fast tool with pagination support.' It specifies the verb ('search'), resource ('code pattern in the project'), and key characteristics ('advanced, fast, pagination support'). This distinguishes it from simpler search tools and its sibling 'find_files' which likely searches for files rather than code patterns.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines4/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides clear context for when to use this tool: for advanced code pattern searching with performance optimization and pagination. It mentions automatic selection of command-line tools (ugrep, ripgrep, ag, grep) for maximum performance, which implies this is preferred over basic search tools. However, it doesn't explicitly state when NOT to use it or name specific alternatives among sibling tools.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/johnhuang316/code-index-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server