文件搜索工具

一个基于MCP协议的文件搜索工具,支持在文件中搜索指定关键词并返回匹配行及行号。

  • by vishnu-m77
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 vishnu-m77/Ressl_MCP

使用说明

Ressl_MCP

使用文件搜索工具实现MCP(模型上下文协议)服务器。

特性

  • search_in_file:在文件中搜索指定的关键字,并返回具有行号的匹配行
    • 支持区分大小写和不区分大小写的搜索
    • 返回行号以便于参考
    • 处理文件路径解析(相对和绝对路径)

MCP配置

要将此服务器与MCP客户端一起使用,请添加以下配置:

MCP检查员

使用MCP检查器时,使用以下配置服务器:

  • 命令: python
  • Args: ["/absolute/path/to/Ressl_MCP/src/server.py"]

工具:search_in_file

参数

  • filePath (必填):要搜索的文件的路径(相对或绝对)
  • keyword (必填):要搜索的关键字或短语
  • caseSensitive (可选):搜索是否应区分大小写(默认值:false)

示例用法

输入:

{
  "filePath": "sample/test.txt",
  "keyword": "sample",
  "caseSensitive": false
}

输出:

{
  "filePath": "/absolute/path/to/Ressl_MCP/sample/test.txt",
  "keyword": "sample",
  "caseSensitive": false,
  "totalMatches": 3,
  "matches": [
    {
      "lineNumber": 1,
      "content": "This is a sample file for testing the file search tool."
    },
    {
      "lineNumber": 4,
      "content": "This line contains the word sample again."
    },
    {
      "lineNumber": 7,
      "content": "The search tool should find all occurrences."
    }
  ]
}

设置

示例文件在 sample/ 目录:

  • sample/test.txt -包含多个“示例”的简单文本文件

设置环境

# Create virtual environment
python3 -m venv venv

# Activate virtual environment
source venv/bin/activate

# Install dependencies
pip install -r requirements.txt

使用MCP检查员进行测试

  1. 安装Node.js和npm(如果尚未安装)

  2. 安装MCP检查器:

    npm install -g @modelcontextprotocol/inspector
    
  3. 创建测试配置文件(例如。, test-config.json):

    {
      "mcpServers": {
        "file-search-server": {
          "command": "python",
          "args": ["/absolute/path/to/Ressl_MCP/src/server.py"]
        }
      }
    }
    

    替换 /absolute/path/to/Ressl_MCP 根据您的实际项目路径。

  4. 运行MCP检查器:

    mcp-inspector --config test-config.json
    
  5. 在MCP检查器中:

    • 选择 search_in_file 工具
    • 使用以下测试输入:
      {
        "filePath": "sample/test.txt",
        "keyword": "sample",
        "caseSensitive": false
      }
      
    • 点击“调用工具”,验证输出是否显示了与行号匹配的行