Grep搜索服务

一个通过模型上下文协议(MCP)暴露grep功能的服务器实现,提供文件内容搜索、正则表达式匹配及自然语言提示理解等功能。

  • by erniebrodeur
  • Python
  • 本地部署
  • 24 星标
  • 更新于
在 GitHub 查看 erniebrodeur/mcp-grep

使用说明

MCP润滑脂

通过模型上下文协议(MCP)公开grep功能的grep服务器实现。

安装

通过Smithery安装

通过以下方式自动安装Grep Server for Claude Desktop 史密瑟里:

npx -y @smithery/cli install @erniebrodeur/mcp-grep --client claude

手动安装

pip install mcp-grep

用法

MCP Grep作为可供MCP兼容客户端使用的服务器运行:

# Start the MCP-Grep server
mcp-grep-server

# Or use the MCP Inspector for interactive debugging and testing
mcp-grep-inspector

服务器公开以下MCP功能:

  • 资源: grep://info -返回有关系统grep二进制文件的信息
  • 工具: grep -使用系统grep二进制文件在文件中搜索模式

特性

  • 有关系统grep二进制文件的信息(路径、版本、支持的功能)
  • 使用正则表达式在文件中搜索模式
  • 支持常见的grep选项:
    • 不区分大小写的匹配
    • 上下文行(匹配前后)
    • 最大匹配计数
    • 固定字符串匹配(非正则表达式)
    • 递归目录搜索
  • 自然语言提示理解,便于LLM使用
  • 通过MCP Inspector进行交互式调试和测试

API使用示例

使用MCP Python客户端:

from mcp.client import MCPClient

# Connect to the MCP-Grep server
client = MCPClient()

# Get information about the grep binary
grep_info = client.get_resource("grep://info")
print(grep_info)

# Search for a pattern in files
result = client.use_tool("grep", {
    "pattern": "search_pattern",
    "paths": ["file.txt", "directory/"],
    "ignore_case": True,
    "recursive": True
})
print(result)

自然语言提示

MCP Grep理解自然语言提示,使其更容易与LLM一起使用。示例:

# Basic search
Search for 'error' in log.txt

# Case-insensitive search
Find all instances of 'WARNING' regardless of case in system.log

# With context lines
Search for 'exception' in error.log and show 3 lines before and after each match

# Recursive search
Find all occurrences of 'deprecated' in the src directory and its subdirectories

# Fixed string search (non-regex)
Search for the exact string '.*' in config.js

# Limited results
Show me just the first 5 occurrences of 'TODO' in the project files

# Multiple options
Find 'password' case-insensitively in all .php files, show 2 lines of context, and limit to 10 results

MCP检查器集成

MCP Grep包括用于交互式调试和测试的MCP Inspector集成:

# Start the MCP Inspector with MCP-Grep
mcp-grep-inspector

这将打开一个基于web的UI,您可以在其中:

  • 探索可用的资源和工具
  • 使用不同参数测试grep操作
  • 查看格式化结果
  • 调试grep查询的问题

发展

# Clone the repository
git clone https://github.com/erniebrodeur/mcp-grep.git
cd mcp-grep

# Install in development mode
pip install -e ".[dev]"

# Run tests
pytest

许可证

麻省理工学院