Grep MCP Server
A Model Context Protocol (MCP) server that provides powerful text search capabilities using the grep
command-line utility. This server allows you to search for patterns in files and directories using both natural language descriptions and direct regex patterns.
Features
๐ง Natural Language Search
- Describe what you're looking for in plain English
- Automatic conversion to appropriate regex patterns
- Built-in patterns for common searches (emails, URLs, phone numbers, etc.)
๐ Advanced Search Capabilities
- Direct regex pattern matching
- Recursive directory searching
- File extension filtering
- Case-sensitive/insensitive search
- Whole word matching
- Context line display
- Match counting
- File listing with matches
๐ก๏ธ Security First
- Safe command execution using
child_process.spawn
- Input validation with Zod schemas
- No shell injection vulnerabilities
- Path validation and sanitization
Installation
# Clone or download the project
cd grep-mcp
# Install dependencies
npm install
# Build the project
npm run build
# Make globally available (optional)
npm link
Available Tools
1. grep_search_intent
Search using natural language descriptions.
Parameters:
intent
(string): Plain English description (e.g., "email addresses", "TODO comments")target
(string): File or directory path to searchcase_sensitive
(boolean, optional): Case-sensitive search (default: false)max_results
(number, optional): Limit number of resultsshow_context
(boolean, optional): Show surrounding lines (default: false)context_lines
(number, optional): Number of context lines (default: 2)
Example:
{
"intent": "email addresses",
"target": "./src",
"show_context": true,
"context_lines": 1
}
2. grep_regex
Search using direct regex patterns.
Parameters:
pattern
(string): Regular expression patterntarget
(string): File or directory path to searchcase_sensitive
(boolean, optional): Case-sensitive searchwhole_words
(boolean, optional): Match whole words onlyinvert_match
(boolean, optional): Show non-matching linesmax_results
(number, optional): Limit resultsshow_context
(boolean, optional): Show context linescontext_lines
(number, optional): Context line countfile_extensions
(array, optional): Filter by file extensions
Example:
{
"pattern": "function\\s+\\w+\\s*\\(",
"target": "./src",
"file_extensions": ["js", "ts"],
"show_context": true
}
3. grep_count
Count matches for a pattern.
Parameters:
pattern
(string): Pattern to counttarget
(string): Search targetcase_sensitive
(boolean, optional): Case sensitivitywhole_words
(boolean, optional): Whole word matchingby_file
(boolean, optional): Show count per filefile_extensions
(array, optional): File extension filter
4. grep_files_with_matches
List files containing the pattern.
Parameters:
pattern
(string): Search patterntarget
(string): Directory to searchcase_sensitive
(boolean, optional): Case sensitivitywhole_words
(boolean, optional): Whole word matchingfile_extensions
(array, optional): File extensions to includeexclude_patterns
(array, optional): File patterns to exclude
5. grep_advanced
Execute grep with custom arguments (advanced users).
Parameters:
args
(array): Array of grep arguments (excluding 'grep' itself)
Built-in Natural Language Patterns
The server recognizes these natural language intents:
Communication
- "email", "email address", "emails" โ Email address pattern
- "url", "urls", "website", "link", "links" โ URL pattern
- "phone", "phone number", "phone numbers" โ Phone number pattern
Network
- "ip", "ip address", "ip addresses" โ IPv4 address pattern
Data Types
- "number", "numbers", "integer", "integers" โ Numeric patterns
- "date", "dates" โ Date patterns
Code Patterns
- "function", "functions" โ Function declarations
- "class", "classes" โ Class definitions
- "import", "imports" โ Import statements
- "export", "exports" โ Export statements
- "comment", "comments" โ Comment lines
- "todo", "todos" โ TODO/FIXME/HACK comments
Error Patterns
- "error", "errors" โ Error messages
- "warning", "warnings" โ Warning messages
Usage Examples
Search for email addresses in a project
{
"tool": "grep_search_intent",
"intent": "email addresses",
"target": "./src",
"show_context": true
}
Find all TODO comments
{
"tool": "grep_search_intent",
"intent": "todo comments",
"target": "./",
"file_extensions": ["js", "ts", "py"]
}
Search for function definitions with regex
{
"tool": "grep_regex",
"pattern": "^\\s*function\\s+\\w+",
"target": "./src",
"file_extensions": ["js"]
}
Count occurrences of a word
{
"tool": "grep_count",
"pattern": "async",
"target": "./src",
"by_file": true
}
List files containing import statements
{
"tool": "grep_files_with_matches",
"pattern": "^import",
"target": "./src",
"file_extensions": ["js", "ts"]
}
Development
Build and Run
# Development with auto-rebuild
npm run dev
# Production build
npm run build
# Start the server
npm start
Project Structure
grep-mcp/
โโโ src/
โ โโโ index.ts # Main server implementation
โโโ build/ # Compiled JavaScript output
โโโ package.json # Project configuration
โโโ tsconfig.json # TypeScript configuration
โโโ README.md # This file
MCP Integration
This server implements the Model Context Protocol and can be used with any MCP-compatible client.
Server Configuration
Add to your MCP client configuration:
{
"mcpServers": {
"grep-mcp": {
"command": "grep-mcp",
"args": []
}
}
}
Security Notes
- Uses
spawn
withshell: false
to prevent command injection - Validates all file paths before execution
- Blocks potentially dangerous grep flags in advanced mode
- Input validation with Zod schemas
- No access to system files outside specified targets