这是一个使用 grep 命令行工具提供强大文本搜索能力的模型上下文协议(MCP)服务器。该服务器允许您通过自然语言描述或直接正则表达式模式在文件和目录中搜索模式。
child_process.spawn 安全执行命令# 全局安装
npm install -g @247arjun/mcp-grep
# 或者在项目中本地安装
npm install @247arjun/mcp-grep
# 克隆仓库
git clone https://github.com/247arjun/mcp-grep.git
cd mcp-grep
# 安装依赖
npm install
# 构建项目
npm run build
# 可选:全局链接
npm link
# 直接从 GitHub 安装
npm install -g git+https://github.com/247arjun/mcp-grep.git
添加到您的 Claude Desktop 配置文件中:
位置:
~/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": ["/绝对路径/to/mcp-grep/build/index.js"]
}
}
}
添加配置后,请重启 Claude Desktop 以加载 MCP 服务器。
测试服务器是否正常工作:
# 测试构建的服务器
node build/index.js
# 应显示:"Grep MCP 服务器正在 stdio 上运行"
# 按 Ctrl+C 退出
grep_search_intent使用自然语言描述进行搜索。
参数:
intent (字符串):简单英语描述(例如,“电子邮件地址”,“TODO 注释”)target (字符串):要搜索的文件或目录路径case_sensitive (布尔值,可选):区分大小写的搜索(默认:false)max_results (数字,可选):限制结果数量show_context (布尔值,可选):显示周围行(默认:false)context_lines (数字,可选):上下文行数(默认:2)示例:
{
"intent": "电子邮件地址",
"target": "./src",
"show_context": true,
"context_lines": 1
}
grep_regex使用直接正则表达式模式进行搜索。
参数:
pattern (字符串):正则表达式模式target (字符串):要搜索的文件或目录路径case_sensitive (布尔值,可选):区分大小写的搜索whole_words (布尔值,可选):仅匹配整个单词invert_match (布尔值,可选):显示非匹配行max_results (数字,可选):限制结果show_context (布尔值,可选):显示上下文行context_lines (数字,可选):上下文行数file_extensions (数组,可选):按文件扩展名过滤示例:
{
"pattern": "function\\s+\\w+\\s*\\(",
"target": "./src",
"file_extensions": ["js", "ts"],
"show_context": true
}
grep_count统计模式出现次数。
参数:
pattern (字符串):要计数的模式target (字符串):搜索目标case_sensitive (布尔值,可选):大小写敏感性whole_words (布尔值,可选):整词匹配by_file (布尔值,可选):按文件显示计数file_extensions (数组,可选):文件扩展名过滤grep_files_with_matches列出包含模式的文件。
参数:
pattern (字符串):搜索模式target (字符串):要搜索的目录case_sensitive (布尔值,可选):大小写敏感性whole_words (布尔值,可选):整词匹配file_extensions (数组,可选):要包含的文件扩展名exclude_patterns (数组,可选):要排除的文件模式grep_advanced执行带有自定义参数的 grep(高级用户)。
参数:
args (数组):grep 参数数组(不包括 'grep' 本身)服务器识别这些自然语言意图:
{
"tool": "grep_search_intent",
"intent": "电子邮件地址",
"target": "./src",
"show_context": true
}
{
"tool": "grep_search_intent",
"intent": "TODO 注释",
"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"]
}
# 开发时自动重新构建
npm run dev
# 生产构建
npm run build
# 启动服务器
npm start
mcp-grep/
├── src/
│ └── index.ts # 主服务器实现
├── build/ # 编译后的 JavaScript 输出
├── package.json # 项目配置
├── tsconfig.json # TypeScript 配置
└── README.md # 本文档
"命令未找到" 错误
npm install -g @247arjun/mcp-grep"command": "npx", "args": ["@247arjun/mcp-grep"]"权限被拒绝" 错误
chmod +x build/index.jsnpm run buildMCP 服务器未出现在 Claude 中
"grep 命令未找到"
通过设置环境变量启用详细日志记录:
# 开发时
DEBUG=1 node build/index.js
# 使用示例输入测试
echo '{"jsonrpc": "2.0", "method": "initialize", "params": {}}' | node build/index.js
spawn 并设置 shell: false 来防止命令注入