grep_advanced
Execute custom grep commands with advanced arguments to search files and directories efficiently. Supports regex and tailored search options for precise text retrieval.
Instructions
Execute grep with custom arguments (advanced usage)
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| args | Yes | Array of grep arguments (excluding 'grep' itself) |
Implementation Reference
- src/index.ts:534-579 (handler)Handler function for the grep_advanced tool. Validates input args for dangerous flags and executes the grep command using the executeGrep helper.async ({ args }) => { // Basic validation to prevent potentially dangerous operations const dangerousFlags = [ '--devices=', // Device operations could be dangerous '--binary-files=', // Binary file operations '-f', '--file', // Reading patterns from files '-D', '--devices', // Device handling ]; const hasUnsafeFlag = args.some(arg => dangerousFlags.some(dangerous => arg.startsWith(dangerous)) ); if (hasUnsafeFlag) { return { content: [ { type: "text", text: `Error: This command contains potentially unsafe flags. Please use the specific grep tools for safety.`, }, ], }; } try { const result = await executeGrep(['grep', ...args]); return { content: [ { type: "text", text: `Exit Code: ${result.exitCode}\n\nResults:\n${result.stdout}${result.stderr ? `\n\nErrors:\n${result.stderr}` : ''}`, }, ], }; } catch (error) { return { content: [ { type: "text", text: `Error executing grep: ${error instanceof Error ? error.message : String(error)}`, }, ], }; } }
- src/index.ts:531-533 (schema)Input schema using Zod, defining 'args' as an array of strings.{ args: z.array(z.string()).describe("Array of grep arguments (excluding 'grep' itself)"), },
- src/index.ts:528-580 (registration)Registration of the grep_advanced tool via server.tool(), including name, description, schema, and inline handler.server.tool( "grep_advanced", "Execute grep with custom arguments (advanced usage)", { args: z.array(z.string()).describe("Array of grep arguments (excluding 'grep' itself)"), }, async ({ args }) => { // Basic validation to prevent potentially dangerous operations const dangerousFlags = [ '--devices=', // Device operations could be dangerous '--binary-files=', // Binary file operations '-f', '--file', // Reading patterns from files '-D', '--devices', // Device handling ]; const hasUnsafeFlag = args.some(arg => dangerousFlags.some(dangerous => arg.startsWith(dangerous)) ); if (hasUnsafeFlag) { return { content: [ { type: "text", text: `Error: This command contains potentially unsafe flags. Please use the specific grep tools for safety.`, }, ], }; } try { const result = await executeGrep(['grep', ...args]); return { content: [ { type: "text", text: `Exit Code: ${result.exitCode}\n\nResults:\n${result.stdout}${result.stderr ? `\n\nErrors:\n${result.stderr}` : ''}`, }, ], }; } catch (error) { return { content: [ { type: "text", text: `Error executing grep: ${error instanceof Error ? error.message : String(error)}`, }, ], }; } } );
- src/index.ts:21-60 (helper)Shared helper function executeGrep that spawns the grep process safely and captures stdout, stderr, and exit code. Used by grep_advanced and other tools.async function executeGrep(args: string[]): Promise<{ stdout: string; stderr: string; exitCode: number }> { return new Promise((resolve) => { // Ensure we're only calling grep with safe arguments if (!args.includes('grep')) { args.unshift('grep'); } const child = spawn('grep', args.slice(1), { stdio: ['pipe', 'pipe', 'pipe'], shell: false, }); let stdout = ''; let stderr = ''; child.stdout.on('data', (data) => { stdout += data.toString(); }); child.stderr.on('data', (data) => { stderr += data.toString(); }); child.on('close', (code) => { resolve({ stdout, stderr, exitCode: code || 0, }); }); child.on('error', (error) => { resolve({ stdout: '', stderr: error.message, exitCode: 1, }); }); }); }