Skip to main content
Glama

grep

Search for text patterns in files using regular expressions to locate specific content within directories and file types.

Instructions

Search for text in files

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
patternYesThe regular expression pattern to search for in file contents
pathNoThe directory to search in. Defaults to the current working directory.
includeNoFile pattern to include in the search (e.g. "*.js", "*.{ts,tsx}")

Implementation Reference

  • Handler function that executes the grep tool logic by calling grepSearch helper, formatting the response, and handling errors.
    async ({ pattern, path, include }) => {
      try {
        const results = await grepSearch(pattern, path, include);
        return {
          content: [{ type: "text", text: results }]
        };
      } catch (error) {
        return {
          content: [{ 
            type: "text", 
            text: error instanceof Error ? error.message : String(error)
          }],
          isError: true
        };
      }
    }
  • Zod input schema defining parameters for the grep tool: pattern (required), path (optional), include (optional).
    {
      pattern: z.string().describe("The regular expression pattern to search for in file contents"),
      path: z.string().optional().describe("The directory to search in. Defaults to the current working directory."),
      include: z.string().optional().describe("File pattern to include in the search (e.g. \"*.js\", \"*.{ts,tsx}\")")
    },
  • MCP server.tool registration for the 'grep' tool, including name, description, schema, and handler reference.
    server.tool(
      "grep",
      "Search for text in files",
      {
        pattern: z.string().describe("The regular expression pattern to search for in file contents"),
        path: z.string().optional().describe("The directory to search in. Defaults to the current working directory."),
        include: z.string().optional().describe("File pattern to include in the search (e.g. \"*.js\", \"*.{ts,tsx}\")")
      },
      async ({ pattern, path, include }) => {
        try {
          const results = await grepSearch(pattern, path, include);
          return {
            content: [{ type: "text", text: results }]
          };
        } catch (error) {
          return {
            content: [{ 
              type: "text", 
              text: error instanceof Error ? error.message : String(error)
            }],
            isError: true
          };
        }
      }
    );
  • Core helper function implementing file text search using the system's 'grep' command via child_process.exec, with support for include patterns and handling no-match scenarios.
    export async function grepSearch(
      pattern: string,
      searchPath: string = process.cwd(),
      include?: string
    ): Promise<string> {
      try {
        const includeFlag = include ? `--include="${include}"` : '';
        const { stdout } = await execPromise(`grep -r ${includeFlag} "${pattern}" ${searchPath}`);
        return stdout;
      } catch (error: any) {
        if (error.code === 1 && error.stdout === '') {
          // grep returns exit code 1 when no matches are found
          return 'No matches found';
        }
        throw error;
      }
    }
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. 'Search for text in files' implies a read-only operation, but it doesn't specify whether this is safe, if it requires permissions, how it handles errors, or what the output format looks like. For a tool with three parameters and no annotations, this is a significant gap in behavioral context.

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 with zero waste—'Search for text in files' is front-loaded and perfectly concise. Every word earns its place by directly conveying the core functionality 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 (3 parameters, no annotations, no output schema), the description is incomplete. It doesn't explain what the tool returns (e.g., matches, line numbers, file names), how results are formatted, or any behavioral nuances like case sensitivity or recursion. For a search tool, this leaves critical gaps for an AI agent to use it effectively.

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?

The description adds no parameter-specific information beyond what's already in the schema, which has 100% coverage with clear descriptions for 'pattern', 'path', and 'include'. According to the rules, when schema description coverage is high (>80%), the baseline is 3 even with no param info in the description, which applies here.

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 'Search for text in files' clearly states the verb (search) and resource (text in files), making the purpose immediately understandable. However, it doesn't distinguish this tool from sibling tools like 'searchGlob' or 'listFiles' that might also involve searching or file operations, so it doesn't reach the highest level of sibling differentiation.

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 like 'searchGlob' or 'listFiles'. There's no mention of prerequisites, context, or exclusions, leaving the agent to infer usage based solely on the tool name and basic purpose.

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/auchenberg/claude-code-mcp'

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