grep_advanced
Execute grep with custom arguments for advanced text search operations, enabling precise pattern matching and file filtering using command-line parameters.
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)The async handler function for the 'grep_advanced' tool. Validates input args against dangerous flags and executes the grep command via executeGrep helper, returning results or errors.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)Zod input schema for the 'grep_advanced' tool, defining a single parameter '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' MCP tool using 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 to safely spawn and execute grep processes, capturing stdout, stderr, and exit code. Used by 'grep_advanced' and other grep 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, }); }); }); }