247arjun

Grep MCP Server

Community 247arjun
Updated

An MCP Server wrapper around the grep CLI tool

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 search
  • case_sensitive (boolean, optional): Case-sensitive search (default: false)
  • max_results (number, optional): Limit number of results
  • show_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 pattern
  • target (string): File or directory path to search
  • case_sensitive (boolean, optional): Case-sensitive search
  • whole_words (boolean, optional): Match whole words only
  • invert_match (boolean, optional): Show non-matching lines
  • max_results (number, optional): Limit results
  • show_context (boolean, optional): Show context lines
  • context_lines (number, optional): Context line count
  • file_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 count
  • target (string): Search target
  • case_sensitive (boolean, optional): Case sensitivity
  • whole_words (boolean, optional): Whole word matching
  • by_file (boolean, optional): Show count per file
  • file_extensions (array, optional): File extension filter

4. grep_files_with_matches

List files containing the pattern.

Parameters:

  • pattern (string): Search pattern
  • target (string): Directory to search
  • case_sensitive (boolean, optional): Case sensitivity
  • whole_words (boolean, optional): Whole word matching
  • file_extensions (array, optional): File extensions to include
  • exclude_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 with shell: 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

MCP Server ยท Populars

MCP Server ยท New