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; } }

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