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[];

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