使用说明
MCP文件搜索服务器
提供文件搜索功能的MCP(模型上下文协议)服务器。
特性
- 搜索文件:在指定文件中搜索关键字
- 返回具有行号的匹配行
- 支持区分大小写和不区分大小写的搜索
- 提供详细的结果,包括匹配计数
安装
npm install
快速测试
我们包含了一个交互式测试客户端,以帮助您快速验证服务器是否正常工作。
运行交互式测试
node test-client.js
这将:
- 启动MCP服务器
- 通过MCP SDK客户端连接到它
- 列出可用工具
- 以交互方式提示您:
- 要搜索的文件路径
- 要搜索的关键字
- 区分大小写选项(是/否)
示例用法
=== MCP File Search Server - Interactive Test ===
✓ Connected to MCP server
Available tools:
- search_file: Search for a keyword within a specified file and return matching lines with line numbers
Enter file path to search (or 'exit' to quit): test.txt
Enter keyword to search for: JavaScript
Case-sensitive search? (y/n, default: n): n
Searching...
File: test.txt
Keyword: "JavaScript"
Case-sensitive: false
Matches found: 2
Results:
Line 4: JavaScript is a popular programming language.
Line 5: Node.js makes server-side JavaScript possible.
==================================================
Enter file path to search (or 'exit' to quit): exit
✓ Goodbye!
提示
- 类型
exit当系统提示输入文件路径以退出程序时 - 服务器将一直运行,直到您退出,允许进行多次搜索
- 使用随附的
test.txt用于测试的文件或任何其他文本文件
与MCP客户端一起使用
启动服务器
npm start
该服务器运行在stdio上,可以与兼容MCP的客户端(如Claude Desktop)集成。
在Claude桌面中配置
添加到您的Claude Desktop配置文件中:
MacOS: ~/Library/Application Support/Claude/claude_desktop_config.json
视窗: %APPDATA%\Claude\claude_desktop_config.json
{
"mcpServers": {
"file-search": {
"command": "node",
"args": ["path/to/MCP-Server/index.js"]
}
}
}
工具:搜索文件
参数
file_path(必填):要搜索的文件的路径keyword(必填):要在文件中搜索的关键字case_sensitive(可选):搜索是否应区分大小写(默认值:false)
示例响应
{
"file": "/path/to/file.txt",
"keyword": "example",
"case_sensitive": false,
"matches_found": 3,
"results": [
{
"line_number": 5,
"content": "This is an example line"
},
{
"line_number": 12,
"content": "Another example here"
}
]
}
使用您自己的文件进行测试
只需运行交互式测试客户端,并在提示时输入您自己的文件路径和关键字:
node test-client.js
当被问及:
- 文件路径:输入文件的路径(例如。,
myfile.txt或C:\path\to\file.txt) - 关键词:输入要搜索的单词或短语
- 能识别大小写字母的:选择
y对于yes或n否(默认)
包含的文件
index.js-主MCP服务器实现test-client.js-交互式测试客户端,用于验证服务器功能test.txt-测试示例文件(包含用于搜索演示的各种文本)package.json-项目依赖关系和配置.gitignore-Git忽略规则
许可证
麻省理工学院
