MCP Grep服务器
](https://badge.fury.io/js/@247arjun%2Fmcp-grep) ](https://www.npmjs.com/package/@247arjun/mcp-grep)
模型上下文协议(MCP)服务器,使用 grep 命令行实用程序。此服务器允许您使用自然语言描述和直接正则表达式模式在文件和目录中搜索模式。
特性
🧠 自然语言搜索
- 用简单的英语描述你在找什么
- 自动转换为适当的正则表达式模式
- 内置的常见搜索模式(电子邮件、网址、电话号码等)
🔍 高级搜索功能
- 直接正则表达式模式匹配
- 递归目录搜索
- 文件扩展名筛选
- 区分大小写/不区分大小写的搜索
- 整词匹配
- 上下文行显示
- 比赛计数
- 包含匹配项的文件列表
🛡️ 安全第一
- 使用安全命令执行
child_process.spawn - 使用Zod模式进行输入验证
- 无外壳注入漏洞
- 路径验证和清理
安装
方法1:NPM安装(推荐)
# Install globally
npm install -g @247arjun/mcp-grep
# Or install locally in your project
npm install @247arjun/mcp-grep方法2:来源
# Clone the repository
git clone https://github.com/247arjun/mcp-grep.git
cd mcp-grep
# Install dependencies
npm install
# Build the project
npm run build
# Optional: Link globally
npm link方法3:直接从GitHub
# Install directly from GitHub
npm install -g git+https://github.com/247arjun/mcp-grep.git配置
Claude桌面设置
添加到您的Claude Desktop配置文件中:
地点:
- macOS:
~/Library/Application Support/Claude/claude_desktop_config.json - 窗户:
%APPDATA%/Claude/claude_desktop_config.json
配置:
{
"mcpServers": {
"mcp-grep": {
"command": "mcp-grep",
"args": []
}
}
}替代方案:使用npx(无需全局安装)
{
"mcpServers": {
"mcp-grep": {
"command": "npx",
"args": ["@247arjun/mcp-grep"]
}
}
}地方发展设置
{
"mcpServers": {
"mcp-grep": {
"command": "node",
"args": ["/absolute/path/to/mcp-grep/build/index.js"]
}
}
}添加配置后,重新启动Claude Desktop以加载MCP服务器。
验证
测试服务器是否正常工作:
# Test the built server
node build/index.js
# Should show: "Grep MCP Server running on stdio"
# Press Ctrl+C to exit可用工具
1. grep_search_intent
使用自然语言描述进行搜索。
参数:
intent(字符串):简明的英文描述(例如,“电子邮件地址”、“TODO评论”)target(string):要搜索的文件或目录路径case_sensitive(布尔值,可选):区分大小写的搜索(默认值:false)max_results(数字,可选):限制结果数量show_context(布尔值,可选):显示周围的线条(默认值:false)context_lines(数字,可选):上下文行数(默认值:2)
例子:
{
"intent": "email addresses",
"target": "./src",
"show_context": true,
"context_lines": 1
}2. grep_regex
使用直接正则表达式模式进行搜索。
参数:
pattern(string):正则表达式模式target(string):要搜索的文件或目录路径case_sensitive(布尔值,可选):区分大小写的搜索whole_words(布尔值,可选):仅匹配整个单词invert_match(boolean,可选):显示不匹配的行max_results(数字,可选):限制结果show_context(布尔值,可选):显示上下文行context_lines(数字,可选):上下文行数file_extensions(数组,可选):按文件扩展名筛选
例子:
{
"pattern": "function\\s+\\w+\\s*\\(",
"target": "./src",
"file_extensions": ["js", "ts"],
"show_context": true
}3. grep_count
计算模式的匹配项。
参数:
pattern(string):要计数的模式target(字符串):搜索目标case_sensitive(布尔值,可选):区分大小写whole_words(布尔值,可选):整词匹配by_file(布尔值,可选):显示每个文件的计数file_extensions(数组,可选):文件扩展名筛选器
4. grep_files_with_matches
列出包含该模式的文件。
参数:
pattern(string):搜索模式target(string):要搜索的目录case_sensitive(布尔值,可选):区分大小写whole_words(布尔值,可选):整词匹配file_extensions(数组,可选):要包含的文件扩展名exclude_patterns(数组,可选):要排除的文件模式
5. grep_advanced
使用自定义参数执行grep(高级用户)。
参数:
args(array):grep参数数组(不包括'grep'本身)
内置自然语言模式
服务器识别这些自然语言意图:
沟通
- “电子邮件”、“电子邮件地址”、“邮件”→ 电子邮件地址模式
- “url”、“网址”、“网站”、“链接”、“连接”→ URL模式
- “电话”、“电话号码”、“手机号码”→ 电话号码模式
网络
- “ip”、“ip地址”、“ip地址”→ IPv4地址模式
数据类型
- “number”、“numbers”、“integer”、“整数”→ 数字模式
- “日期”,“日期”→ 日期模式
代码模式
- “功能”,“功能”→ 函数声明
- “类”,“类”→ 类定义
- “进口”,“进口”→ 导入语句
- “出口”,“出口”→ 出口报表
- “comment”,“comments”→ 注释行
- “一切”,“一切”→一切/fixme/hack评论
错误模式
- “错误”,“错误”→ 错误消息
- “警告”,“警告”→ 警告消息
使用示例
在项目中搜索电子邮件地址
{
"tool": "grep_search_intent",
"intent": "email addresses",
"target": "./src",
"show_context": true
}查找所有TODO评论
{
"tool": "grep_search_intent",
"intent": "todo comments",
"target": "./",
"file_extensions": ["js", "ts", "py"]
}使用正则表达式搜索函数定义
{
"tool": "grep_regex",
"pattern": "^\\s*function\\s+\\w+",
"target": "./src",
"file_extensions": ["js"]
}统计单词的出现次数
{
"tool": "grep_count",
"pattern": "async",
"target": "./src",
"by_file": true
}列出包含导入语句的文件
{
"tool": "grep_files_with_matches",
"pattern": "^import",
"target": "./src",
"file_extensions": ["js", "ts"]
}发展
构建并运行
# Development with auto-rebuild
npm run dev
# Production build
npm run build
# Start the server
npm start项目结构
mcp-grep/
├── src/
│ └── index.ts # Main server implementation
├── build/ # Compiled JavaScript output
├── package.json # Project configuration
├── tsconfig.json # TypeScript configuration
└── README.md # This file故障排除
常见问题
- “找不到命令”错误
- 确保mcp-grep已全局安装: npm install -g @247arjun/mcp-grep - 或者使用npx: "command": "npx", "args": ["@247arjun/mcp-grep"]
- “权限被拒绝”错误
- 检查文件权限: chmod +x build/index.js - 重建项目: npm run build
- MCP服务器未出现在Claude中
- 验证配置文件中的JSON语法 - 完全重新启动克劳德桌面 - 检查命令路径是否正确
- “找不到grep命令”
- 在您的系统上安装grep(通常预装在macOS/Linux上) - Windows用户:通过WSL安装或使用Git Bash
调试
通过设置环境变量启用详细日志记录:
# For development
DEBUG=1 node build/index.js
# Test with sample input
echo '{"jsonrpc": "2.0", "method": "initialize", "params": {}}' | node build/index.js安全说明
- 用途
spawn随着shell: false防止命令注入 - 执行前验证所有文件路径
- 在高级模式下阻止潜在危险的grep标志
- 使用Zod模式进行输入验证
- 无法访问指定目标之外的系统文件
