Skip to main content
Glama
pwilkin

MCP File Editor Server

by pwilkin

search_directory

Search for regex patterns in files within a specified directory, including optional recursive search, line context, and file inclusion/exclusion filters. Use to locate matches efficiently across multiple files.

Instructions

Search for regex patterns across all files in a directory.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
directory_pathYesAbsolute path to the directory to search
excludeNoGlob pattern for files/directories to exclude
includeNoGlob pattern for files to include (e.g., "*.ts", "*.js")
lines_afterNoNumber of lines to show after each match
lines_beforeNoNumber of lines to show before each match
recursiveNoSearch recursively in subdirectories
regexpYesRegular expression pattern to search for

Implementation Reference

  • The execute handler for the 'search_directory' tool. It validates the directory, defines inner helper functions for searching files and directories, and performs recursive regex search with context lines, include/exclude globs.
    execute: async ({ directory_path, regexp, recursive, lines_before, lines_after, include, exclude }) => {
      const absolutePath = validateAbsolutePath(directory_path, 'directory_path');
      validateDirectoryExists(absolutePath);
    
      try {
        const regex = new RegExp(regexp);
        const results: string[] = [];
        let totalMatches = 0;
    
        function searchInFile(filePath: string): void {
          try {
            const content = fs.readFileSync(filePath, 'utf-8');
            const lines = content.split('\n');
            const fileMatches: string[] = [];
    
            lines.forEach((line, index) => {
              if (regex.test(line)) {
                const lineNumber = index + 1;
                const startLine = Math.max(1, lineNumber - (lines_before || 0));
                const endLine = Math.min(lines.length, lineNumber + (lines_after || 0));
    
                const contextLines = lines.slice(startLine - 1, endLine);
                const context = contextLines.map((ctxLine, ctxIndex) => {
                  const ctxLineNumber = startLine + ctxIndex;
                  const marker = ctxLineNumber === lineNumber ? '>' : ' ';
                  return `${marker} ${ctxLineNumber} | ${ctxLine}`;
                }).join('\n');
    
                fileMatches.push(`Match at line ${lineNumber}:\n${context}`);
                totalMatches++;
              }
            });
    
            if (fileMatches.length > 0) {
              results.push(`File: ${filePath}\n${fileMatches.join('\n\n')}`);
            }
          } catch (error: any) {
            // Skip files that can't be read
            results.push(`Warning: Could not read file ${filePath}: ${error.message}`);
          }
        }
    
        function matchesPattern(filename: string, pattern?: string): boolean {
          if (!pattern) return true;
          // Simple glob matching (could be enhanced with a proper glob library)
          const regexPattern = pattern.replace(/\*/g, '.*').replace(/\?/g, '.');
          return new RegExp(`^${regexPattern}$`).test(filename);
        }
    
        function searchDirectory(dirPath: string): void {
          const items = fs.readdirSync(dirPath);
    
          items.forEach(item => {
            const itemPath = path.join(dirPath, item);
            const stats = fs.statSync(itemPath);
    
            if (stats.isDirectory()) {
              if (recursive && !(exclude && matchesPattern(item, exclude))) {
                searchDirectory(itemPath);
              }
            } else if (stats.isFile()) {
              const includeMatch = !include || matchesPattern(item, include);
              const excludeMatch = exclude && matchesPattern(item, exclude);
              if (includeMatch && !excludeMatch) {
                searchInFile(itemPath);
              }
            }
          });
        }
    
        searchDirectory(absolutePath);
    
        if (totalMatches === 0) {
          return `No matches found for pattern "${regexp}" in directory "${absolutePath}".`;
        }
    
        return `Found ${totalMatches} match(es) in ${results.length} file(s):\n\n${results.join('\n\n')}`;
      } catch (error: any) {
        if (error instanceof UserError) throw error;
        throw new UserError(`Error searching directory "${absolutePath}": ${error.message}`);
      }
    }
  • Zod schema defining the input parameters for the search_directory tool, including directory path, regex, recursion flag, context lines, and glob patterns for include/exclude.
    parameters: z.object({
      directory_path: z.string().describe('Absolute path to the directory to search'),
      regexp: z.string().describe('Regular expression pattern to search for'),
      recursive: z.boolean().optional().default(false).describe('Search recursively in subdirectories'),
      lines_before: z.number().int().min(0).optional().describe('Number of lines to show before each match'),
      lines_after: z.number().int().min(0).optional().describe('Number of lines to show after each match'),
      include: z.string().optional().describe('Glob pattern for files to include (e.g., "*.ts", "*.js")'),
      exclude: z.string().optional().describe('Glob pattern for files/directories to exclude')
    }),
  • src/index.ts:519-519 (registration)
    The server.addTool call registering the search_directory tool.
    server.addTool({
  • Inner helper function that recursively traverses the directory structure, applies include/exclude filters, and calls searchInFile on matching files.
    function searchDirectory(dirPath: string): void {
      const items = fs.readdirSync(dirPath);
    
      items.forEach(item => {
        const itemPath = path.join(dirPath, item);
        const stats = fs.statSync(itemPath);
    
        if (stats.isDirectory()) {
          if (recursive && !(exclude && matchesPattern(item, exclude))) {
            searchDirectory(itemPath);
          }
        } else if (stats.isFile()) {
          const includeMatch = !include || matchesPattern(item, include);
          const excludeMatch = exclude && matchesPattern(item, exclude);
          if (includeMatch && !excludeMatch) {
            searchInFile(itemPath);
          }
        }
      });
    }
  • Helper function to match filenames against simple glob patterns for include/exclude filtering.
    function matchesPattern(filename: string, pattern?: string): boolean {
      if (!pattern) return true;
      // Simple glob matching (could be enhanced with a proper glob library)
      const regexPattern = pattern.replace(/\*/g, '.*').replace(/\?/g, '.');
      return new RegExp(`^${regexPattern}$`).test(filename);
    }
Behavior2/5

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

With no annotations provided, the description carries full burden for behavioral disclosure. It mentions 'search' but doesn't clarify whether this is read-only (likely, given the verb), what happens with large directories (performance implications), whether results are paginated, or what the output format looks like. The description provides basic intent but lacks critical operational details needed for effective tool invocation.

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 a single, efficient sentence that communicates the core functionality without unnecessary words. It's appropriately sized for a search tool and front-loads the essential information. Every word earns its place in this concise formulation.

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?

For a 7-parameter search tool with no annotations and no output schema, the description is inadequate. It doesn't explain what the tool returns (e.g., match locations, context lines, file paths), how results are structured, or any limitations (e.g., maximum directory size, performance considerations). The agent lacks sufficient context to understand the tool's behavior and output.

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 all parameters are documented in the schema. The description adds minimal value beyond the schema by mentioning 'regex patterns' and 'all files in a directory,' which aligns with 'regexp' and 'directory_path' parameters. However, it doesn't explain parameter interactions or provide additional context beyond what's already in the structured schema descriptions.

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 with a specific verb ('Search') and resource ('regex patterns across all files in a directory'), making it immediately understandable. However, it doesn't explicitly differentiate from sibling tools like 'search_file' (which likely searches within a single file) or 'list_files' (which lists files without pattern matching), leaving some ambiguity about when to choose this tool over those alternatives.

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. With siblings like 'search_file' (for single-file searches) and 'list_files' (for file listing without regex), the agent must infer usage context from tool names alone. No explicit when/when-not statements or prerequisite information is included.

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

Related 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/pwilkin/mcp-file-edit'

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