Skip to main content
Glama

search_in_large_file

Search for patterns in large files using regex or case-sensitive matching, displaying results with surrounding context lines for better analysis.

Instructions

Search for a pattern in a large file with context lines. Supports regex and case-sensitive search.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
filePathYesAbsolute path to the file
patternYesSearch pattern (supports regex if regex=true)
caseSensitiveNoCase sensitive search (default: false)
regexNoUse regex pattern (default: false)
maxResultsNoMaximum number of results (default: 100)
contextBeforeNoNumber of context lines before match (default: 2)
contextAfterNoNumber of context lines after match (default: 2)
startLineNoStart searching from line number (optional)
endLineNoEnd searching at line number (optional)

Implementation Reference

  • Core handler implementation for searching large files. Streams lines using readline, supports regex/literal patterns, case sensitivity, context lines, line range limits, and max results. Returns structured SearchResult array.
    static async search(
      filePath: string,
      pattern: string,
      options: SearchOptions = {}
    ): Promise<SearchResult[]> {
      await this.verifyFile(filePath);
    
      const results: SearchResult[] = [];
      const maxResults = options.maxResults || 100;
      const contextBefore = options.contextBefore || 2;
      const contextAfter = options.contextAfter || 2;
    
      const regex = options.regex
        ? new RegExp(pattern, options.caseSensitive ? 'g' : 'gi')
        : new RegExp(
            pattern.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'),
            options.caseSensitive ? 'g' : 'gi'
          );
    
      return new Promise((resolve, reject) => {
        let lineNumber = 0;
        const lineBuffer: string[] = [];
        const stream = fs.createReadStream(filePath);
        const rl = readline.createInterface({
          input: stream,
          crlfDelay: Infinity,
        });
    
        rl.on('line', (line) => {
          lineNumber++;
          lineBuffer.push(line);
    
          // Keep buffer for context
          if (lineBuffer.length > contextBefore + contextAfter + 1) {
            lineBuffer.shift();
          }
    
          // Check if within search range
          if (options.startLine && lineNumber < options.startLine) return;
          if (options.endLine && lineNumber > options.endLine) {
            rl.close();
            return;
          }
    
          // Search for pattern
          const matches = Array.from(line.matchAll(regex));
          if (matches.length > 0) {
            const matchPositions = matches.map(m => ({
              start: m.index!,
              end: m.index! + m[0].length,
            }));
    
            const bufferIndex = lineBuffer.length - 1;
            const before = lineBuffer.slice(
              Math.max(0, bufferIndex - contextBefore),
              bufferIndex
            );
    
            results.push({
              lineNumber,
              lineContent: line,
              matchPositions,
              contextBefore: before,
              contextAfter: [], // Will be filled after
              chunkIndex: Math.floor((lineNumber - 1) / 500),
            });
    
            if (results.length >= maxResults) {
              rl.close();
            }
          }
    
          // Fill context after for previous results
          if (results.length > 0) {
            const lastResult = results[results.length - 1];
            const linesSince = lineNumber - lastResult.lineNumber;
            if (linesSince > 0 && linesSince <= contextAfter) {
              lastResult.contextAfter.push(line);
            }
          }
        });
    
        rl.on('close', () => resolve(results));
        rl.on('error', reject);
      });
    }
  • src/server.ts:116-162 (registration)
    Tool registration in getTools() method, defining name, description, and input schema for listTools MCP protocol.
    {
      name: 'search_in_large_file',
      description: 'Search for a pattern in a large file with context lines. Supports regex and case-sensitive search.',
      inputSchema: {
        type: 'object',
        properties: {
          filePath: {
            type: 'string',
            description: 'Absolute path to the file',
          },
          pattern: {
            type: 'string',
            description: 'Search pattern (supports regex if regex=true)',
          },
          caseSensitive: {
            type: 'boolean',
            description: 'Case sensitive search (default: false)',
          },
          regex: {
            type: 'boolean',
            description: 'Use regex pattern (default: false)',
          },
          maxResults: {
            type: 'number',
            description: 'Maximum number of results (default: 100)',
          },
          contextBefore: {
            type: 'number',
            description: 'Number of context lines before match (default: 2)',
          },
          contextAfter: {
            type: 'number',
            description: 'Number of context lines after match (default: 2)',
          },
          startLine: {
            type: 'number',
            description: 'Start searching from line number (optional)',
          },
          endLine: {
            type: 'number',
            description: 'End searching at line number (optional)',
          },
        },
        required: ['filePath', 'pattern'],
      },
    },
    {
  • JSON Schema defining input parameters and validation for the search_in_large_file tool.
    inputSchema: {
      type: 'object',
      properties: {
        filePath: {
          type: 'string',
          description: 'Absolute path to the file',
        },
        pattern: {
          type: 'string',
          description: 'Search pattern (supports regex if regex=true)',
        },
        caseSensitive: {
          type: 'boolean',
          description: 'Case sensitive search (default: false)',
        },
        regex: {
          type: 'boolean',
          description: 'Use regex pattern (default: false)',
        },
        maxResults: {
          type: 'number',
          description: 'Maximum number of results (default: 100)',
        },
        contextBefore: {
          type: 'number',
          description: 'Number of context lines before match (default: 2)',
        },
        contextAfter: {
          type: 'number',
          description: 'Number of context lines after match (default: 2)',
        },
        startLine: {
          type: 'number',
          description: 'Start searching from line number (optional)',
        },
        endLine: {
          type: 'number',
          description: 'End searching at line number (optional)',
        },
      },
      required: ['filePath', 'pattern'],
    },
  • MCP server tool handler wrapper that extracts arguments, calls FileHandler.search, and formats JSON response for MCP protocol.
    private async handleSearch(
      args: Record<string, unknown>
    ): Promise<{ content: Array<{ type: string; text: string }> }> {
      const filePath = args.filePath as string;
      const pattern = args.pattern as string;
    
      const results: SearchResult[] = await FileHandler.search(filePath, pattern, {
        caseSensitive: args.caseSensitive as boolean,
        regex: args.regex as boolean,
        maxResults: (args.maxResults as number) || 100,
        contextBefore: (args.contextBefore as number) || 2,
        contextAfter: (args.contextAfter as number) || 2,
        startLine: args.startLine as number | undefined,
        endLine: args.endLine as number | undefined,
      });
    
      return {
        content: [
          {
            type: 'text',
            text: JSON.stringify({
              totalResults: results.length,
              results,
            }, null, 2),
          },
        ],
      };
    }
  • TypeScript interfaces defining SearchResult (output structure) and SearchOptions (input options) used by the search handler.
    export interface SearchResult {
      /** Line number (1-indexed) */
      lineNumber: number;
      /** Line content */
      lineContent: string;
      /** Match positions in line */
      matchPositions: Array<{ start: number; end: number }>;
      /** Context lines before */
      contextBefore: string[];
      /** Context lines after */
      contextAfter: string[];
      /** Chunk index containing this result */
      chunkIndex: number;
    }
    
    export interface SearchOptions {
      /** Case sensitive search */
      caseSensitive?: boolean;
      /** Regular expression search */
      regex?: boolean;
      /** Maximum results to return */
      maxResults?: number;
      /** Number of context lines before match */
      contextBefore?: number;
      /** Number of context lines after match */
      contextAfter?: number;
      /** Start searching from line */
      startLine?: number;
      /** End searching at line */
      endLine?: number;
    }
Behavior2/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. It mentions regex and case-sensitive search support, which adds some context beyond the input schema. However, it lacks critical behavioral details: it doesn't specify performance characteristics (e.g., memory usage for large files), error handling (e.g., for invalid patterns), output format (e.g., structured results vs. raw text), or whether it's read-only (implied but not stated). This is a significant gap for a tool with 9 parameters.

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

Conciseness5/5

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

The description is extremely concise and front-loaded: two sentences that directly state the core functionality. Every word earns its place, with no redundancy or fluff. It efficiently communicates key features without unnecessary elaboration.

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

Completeness2/5

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

Given the tool's complexity (9 parameters, no annotations, no output schema), the description is incomplete. It lacks behavioral details (e.g., how results are returned, error conditions), doesn't explain the relationship with sibling tools, and provides minimal guidance on usage. While the input schema is well-documented, the description fails to compensate for the absence of annotations and output schema, leaving gaps in understanding the tool's full behavior.

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

Parameters3/5

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

Schema description coverage is 100%, meaning all parameters are documented in the input schema. The description adds minimal value beyond the schema: it mentions regex and case-sensitive search, which are already covered by the 'regex' and 'caseSensitive' parameter descriptions. It doesn't provide additional context like parameter interactions (e.g., how 'contextBefore' affects output) or examples. Baseline 3 is appropriate as the schema does the heavy lifting.

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

Purpose4/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 pattern in a large file with context lines. Supports regex and case-sensitive search.' It specifies the verb (search), resource (large file), and key capabilities (regex, case-sensitive). However, it doesn't explicitly distinguish this from sibling tools like 'navigate_to_line' or 'read_large_file_chunk', which might also involve file content access.

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

Usage Guidelines2/5

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

The description provides no guidance on when to use this tool versus alternatives. It doesn't mention sibling tools like 'get_file_summary' for overviews or 'stream_large_file' for reading without searching, nor does it specify scenarios where this tool is preferred (e.g., pattern-based analysis vs. line-by-line navigation). Usage is implied by the name but not explicitly stated.

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/willianpinho/large-file-mcp'

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