🔍 关键字搜索MCP服务器
一 MCP(模型上下文协议) 服务器构建于 FastMCP,通过注册的工具和资源实现文件读取和关键字搜索。
______________________________________________________________________
🚀 特性
- 文件资源: 读取并返回文件内容
- 搜索工具: 在包含上下文片段的文本文件中查找关键字
- 区分大小写切换: 支持灵活的搜索模式
- 轻量级异步: 使用FastMCP异步资源/工具
______________________________________________________________________
🧩 安装和设置
1️ 克隆存储库
git clone https://github.com/abhirup2565/Search_Keyword_MCP.git
cd 2.️⃣ 安装依赖项
pip install -r requirements.txt3.️⃣ 运行MCP服务器
python mcp_server.py______________________________________________________________________
🖥️ 用法
启动服务器
python keyword_search_mcp.py资源
file://{path} --读取文本文件 例子: file://./contracts.txt
工具
search_file --在文件中搜索关键字 参数:
| 名称 | 类型 | 必填 | 描述 |
|---|---|---|---|
keyword | string | ✅ | 要搜索的单词 |
path | string | ✅ | 文件路径 |
case_sensitive | bool | ❌ | 大小写匹配切换(默认值:False) |
退货: file, keyword, total_occurrences,以及列表 occurrences 带有行号和代码段。
______________________________________________________________________
📄 示例
请求:
{
"keyword": "Agreement",
"path": "contracts.txt",
"case_sensitive": false
}答复:
{
"file": "contracts.txt",
"keyword": "Agreement",
"total_occurrences": 3,
"occurrences": [
{
"line_number": 1,
"start": 12,
"end": 21,
"snippet": "MASTER SERVICE AGREEMENT"
}
]
}______________________________________________________________________
⚠️ 局限性
- 仅文本文件 (
.txt,.md,.log等等) - 建议的最大文件大小: 10 MB(将整个文件读入内存)
- 无模糊搜索: 仅精确匹配子字符串
- UTF-8编码: 安全替换非-UTF-8字符
______________________________________________________________________
🧠 错误处理
| 错误 | 描述 |
|---|---|
FileNotFoundError | 文件不存在 |
ValueError | 关键字为空或无效 |
______________________________________________________________________
