文件搜索服务

一个基于Model Context Protocol (MCP)的服务器,提供强大的文件和目录搜索功能,支持关键词匹配和上下文显示。

  • by SAQLAINAP
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 SAQLAINAP/File_search_MCP

使用说明

文件搜索MCP服务器

一种模型上下文协议(MCP)服务器,使用关键字与上下文匹配提供强大的文件和目录搜索功能。

特性

  • 文件搜索:使用可配置的上下文行在特定文件中搜索关键字
  • 目录搜索:在目录中的所有文件中递归搜索关键字
  • 区分大小写:可选区分大小写或不区分大小写的搜索
  • 上下文行:查看周围的线条以获得更好的上下文
  • 文件筛选:按文件扩展名筛选搜索
  • 错误处理:针对文件访问问题的全面错误处理

工具

1.搜索关键字_文件

在特定文件中搜索关键字,并返回与上下文匹配的行。

参数:

  • file_path (string):要搜索的文件的路径
  • keyword (string):要搜索的关键字或短语
  • case_sensitive (boolean,可选):搜索是否应区分大小写(默认值:false)
  • context_lines (整数,可选):匹配前后要包含的行数(默认值:2)

退货: 包含所有匹配行、行号和周围上下文的格式化字符串。

例子:

Found 3 match(es) for 'import' in 'file_search_server.py':

--- Match 1 (Line 1) ---
>>>    1: import os
       2: from typing import Optional
       3: from mcp.server.fastmcp import FastMCP

2.搜索关键字_目录

在目录中的所有文件中递归搜索关键字。

参数:

  • directory_path (string):要搜索的目录的路径
  • keyword (string):要搜索的关键字或短语
  • file_pattern (字符串,可选):文件扩展名过滤器(例如“.py”、“.txt”)
  • case_sensitive (boolean,可选):搜索是否应区分大小写(默认值:false)

退货: 包含所有匹配文件及其匹配行的格式化字符串。

例子:

Found matches for 'TODO' in 3 file(s):

📄 main.py (2 match(es))
   Line 45: # TODO: Implement error handling
   Line 78: # TODO: Add unit tests

安装

先决条件

  • Python 3.8或更高版本
  • pip包管理器

步骤

  1. 克隆存储库:
git clone https://github.com/yourusername/file-search-mcp-server.git
cd file-search-mcp-server
  1. 安装依赖项:
pip install -r requirements.txt

用法

配备MCP检查员(测试)

使用MCP检查器测试您的服务器:

npx @modelcontextprotocol/inspector python file_search_server.py

这将打开一个web界面,您可以在其中:

  • 查看可用工具
  • 具有不同参数的测试工具调用
  • 查看请求/响应详细信息

使用克劳德桌面

  1. 找到您的Claude Desktop配置文件:

    • macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
    • 视窗: %APPDATA%\Claude\claude_desktop_config.json
  2. 添加服务器配置:

{
  "mcpServers": {
    "file_search": {
      "command": "python",
      "args": [
        "/absolute/path/to/file_search_server.py"
      ]
    }
  }
}
  1. 重新启动克劳德桌面

  2. 测试连接:

    • 寻找🔌 克劳德桌面中的图标
    • 您应该看到“file_search”列出了2个工具

使用Windsurf

  1. 找到Windsurf的MCP配置文件:

    • 视窗: %APPDATA%\Windsurf\User\globalStorage\windsurf-mcp\mcp_settings.json
  2. 添加服务器配置:

{
  "mcpServers": {
    "file_search": {
      "command": "python",
      "args": [
        "/absolute/path/to/file_search_server.py"
      ]
    }
  }
}
  1. 重新启动Windsurf

带光标或VS代码(临床)

  1. 安装Cline延长件 (适用于VS代码)

  2. 找到配置文件:

    • VS代码: %APPDATA%\Code\User\globalStorage\saoudrizwan.claude-dev\settings\cline_mcp_settings.json
    • 光标: %APPDATA%\Cursor\User\globalStorage\cursor-mcp\config.json
  3. 添加服务器配置 (与上述相同)

  4. 重新启动编辑器

示例用法

在单个文件中搜索

Search for "import" in /path/to/file.py

在目录中搜索

Find all occurrences of "TODO" in /path/to/project

区分大小写的搜索

Search for "API_KEY" (case-sensitive) in /path/to/config.py

搜索特定文件类型

Search for "function" in /path/to/project, only in .js files

支持的文件类型

当没有指定文件模式时,服务器会搜索这些文本文件扩展名:

  • python南美国家巴拉圭的缩写(Paraguay)
  • 网络:.html、.css、.js、.ts
  • 配置:.json、.yaml、.yml、.toml、.ini、.cfg、.conf
  • 文档:.txt、.md
  • 代码:.java、.cpp、.c、.h、.hpp、.rs、.go、.rb、.php、.sh
  • 数据:.xml

故障排除

服务器未连接

  1. 验证Python安装:
python --version
  1. 检查FastMCP安装:
pip show fastmcp
  1. 手动测试服务器:
python file_search_server.py

Windows上的路径问题

  • 使用正斜杠: C:/Users/name/file.py
  • 或者逃过一劫: C:\\Users\\name\\file.py
  • 避免文件路径中的空格或使用引号

权限错误

确保服务器具有您正在搜索的文件/目录的读取权限。

发展

项目结构

file-search-mcp-server/
├── file_search_server.py   # Main MCP server implementation
├── requirements.txt         # Python dependencies
├── README.md               # This file
└── .gitignore             # Git ignore rules

运行测试

使用MCP检查器测试您的服务器:

npx @modelcontextprotocol/inspector python file_search_server.py

技术细节

  • 协议:模型上下文协议(MCP)
  • 运输:stdio(标准输入/输出)
  • 框架:FastMCP
  • 语言:Python 3.8+

许可证

MIT许可证-可根据需要自由使用和修改。

贡献

欢迎投稿!请随时提交拉取请求。

作者

由Saqlain Ahmed P.创建,作为MCP服务器开发任务的一部分。

致谢