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
Tool call template
Replace each placeholder with a value matching the input schema. Only required parameters are included; add optional parameters as needed.
{
"id": 1,
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"arguments": {
"pattern": "<pattern>"
},
"name": "grep"
}
}Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| pattern | Yes | The regular expression pattern to search for in file contents | |
| path | No | The directory to search in. Defaults to the current working directory. | |
| include | No | File pattern to include in the search (e.g. "*.js", "*.{ts,tsx}") |
Implementation Reference
- src/server/tools.ts:141-156 (handler)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 }; } } - src/server/tools.ts:136-140 (schema)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}\")") }, - src/server/tools.ts:133-157 (registration)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 }; } } ); - src/utils/file.ts:89-105 (helper)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; } }