Skip to main content
Glama
lofcz

MCP Smart Filesystem Server

by lofcz

search_in_file

Find text patterns in specific files using regex search with context lines and case sensitivity options to locate code sections and content efficiently.

Instructions

Search for patterns within a specific file using ripgrep. Like Ctrl+F but with regex support. Useful for finding specific sections in a known file.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
pathYesFile path to search within
patternYesRegex pattern to search for
caseInsensitiveNoIgnore case in search. Default: true
contextLinesNoLines of context before/after match
literalStringNoTreat pattern as literal string, not regex
wordBoundaryNoMatch whole words only

Implementation Reference

  • Core handler function that performs the ripgrep search within a specific file, constructing arguments and executing the command.
    export async function ripgrepSearchInFile(
      filePath: string,
      pattern: string,
      options: {
        caseInsensitive?: boolean;
        contextLines?: number;
        literalString?: boolean;
        wordBoundary?: boolean;
      } = {}
    ): Promise<RipgrepResult> {
      const args: string[] = ['--json', '--no-config'];
      
      if (options.contextLines !== undefined && options.contextLines > 0) {
        args.push('-C', options.contextLines.toString());
      }
      
      if (options.caseInsensitive) {
        args.push('-i');
      }
      
      if (options.literalString) {
        args.push('-F');
      }
      
      if (options.wordBoundary) {
        args.push('-w');
      }
      
      args.push(pattern);
      args.push(filePath);
      
      const startTime = Date.now();
      const result = await executeRipgrep(args, pattern, [filePath]);
      result.summary.searchTimeMs = Date.now() - startTime;
      
      return result;
    }
  • index.ts:169-206 (registration)
    Registration of the 'search_in_file' tool in the MCP tools list, including name, description, and JSON input schema.
    {
      name: 'search_in_file',
      description: 'Search for patterns within a specific file using ripgrep. Like Ctrl+F but with regex support. Useful for finding specific sections in a known file.',
      inputSchema: {
        type: 'object',
        properties: {
          path: {
            type: 'string',
            description: 'File path to search within'
          },
          pattern: {
            type: 'string',
            description: 'Regex pattern to search for'
          },
          caseInsensitive: {
            type: 'boolean',
            description: 'Ignore case in search. Default: true',
            default: true
          },
          contextLines: {
            type: 'number',
            description: 'Lines of context before/after match',
            default: 3
          },
          literalString: {
            type: 'boolean',
            description: 'Treat pattern as literal string, not regex',
            default: false
          },
          wordBoundary: {
            type: 'boolean',
            description: 'Match whole words only',
            default: false
          }
        },
        required: ['path', 'pattern']
      }
    },
  • Zod validation schema matching the tool inputSchema for runtime input validation in the dispatch handler.
    const schema = z.object({
      path: z.string(),
      pattern: z.string(),
      caseInsensitive: z.boolean().optional().default(true),
      contextLines: z.number().optional().default(3),
      literalString: z.boolean().optional().default(false),
      wordBoundary: z.boolean().optional().default(false)
    });
  • MCP server dispatch handler case for 'search_in_file' that validates arguments, calls the core handler, and formats the response.
    case 'search_in_file': {
      const schema = z.object({
        path: z.string(),
        pattern: z.string(),
        caseInsensitive: z.boolean().optional().default(true),
        contextLines: z.number().optional().default(3),
        literalString: z.boolean().optional().default(false),
        wordBoundary: z.boolean().optional().default(false)
      });
      const { path, pattern, caseInsensitive, contextLines, literalString, wordBoundary } = schema.parse(args);
      const validatedPath = await validatePath(path);
      
      const result = await ripgrepSearchInFile(
        validatedPath,
        pattern,
        { caseInsensitive, contextLines, literalString, wordBoundary }
      );
      
      return {
        content: [{
          type: 'text',
          text: JSON.stringify(result, null, 2)
        }]
      };
    }
  • Type definition for the output structure returned by search functions.
    export interface RipgrepResult {
      query: {
        pattern: string;
        searchedPaths: string[];
        options?: any;
      };
      summary: {
        totalMatches: number;
        filesWithMatches: number;
        searchTimeMs: number;
      };
      pagination?: {
        page: number;
        pageSize: number;
        totalPages: number;
        hasMore: boolean;
      };
      matches: RipgrepMatch[];
      ripgrepDetails: {
        commandUsed: string;
        explanation: string;
      };
      suggestions?: string[];
Behavior2/5

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

No annotations are provided, so the description carries the full burden of behavioral disclosure. It mentions the tool uses ripgrep and supports regex, which adds some context, but it doesn't describe what happens on errors (e.g., if the file doesn't exist), the output format, performance characteristics, or any limitations. For a tool with no annotation coverage, this is a significant gap.

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 concise and front-loaded: the first sentence states the core purpose, followed by analogies and usage hints. Every sentence adds value without redundancy, making it efficient and easy to parse.

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

Completeness3/5

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

Given the tool's moderate complexity (6 parameters, no output schema, no annotations), the description is minimally adequate. It covers the purpose and basic usage but lacks details on behavior, error handling, and output format. Without annotations or an output schema, more context would be helpful for safe and effective use.

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%, so the schema fully documents all 6 parameters. The description doesn't add any parameter-specific details beyond what's in the schema (e.g., it doesn't explain regex syntax or path requirements). With high schema coverage, the baseline score of 3 is appropriate, as the description doesn't compensate but also doesn't detract.

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 patterns within a specific file using ripgrep.' It specifies the verb (search), resource (file), and method (ripgrep with regex support). However, it doesn't explicitly differentiate from sibling tools like 'search_code' or 'find_files' beyond mentioning it's for 'a specific file'.

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

Usage Guidelines3/5

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

The description provides some usage context: 'Useful for finding specific sections in a known file' and 'Like Ctrl+F but with regex support.' This implies it's for targeted searches within a single file, but it doesn't explicitly state when to use this versus alternatives like 'search_code' or 'find_files,' nor does it mention any exclusions or prerequisites.

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/lofcz/mcp-filesystem-smart'

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