MCP MongoDB服务器
高性能 模型上下文协议(MCP) 服务器,在大型语言模型(LLM)和MongoDB之间提供高效的知识接口。该服务器针对小型上下文窗口(3k个令牌)进行了优化,使LLM能够检索存储在MongoDB集合中的特定于域的知识并与之交互。
🚀 特性
- 双服务器架构:
- 基于SSE的MCP服务器,用于高效的LLM通信 - 用于数据库管理的RESTful CRUD API
- 代币效率:响应格式化为在小上下文窗口内实现最大信息密度
- MongoDB集成:与具有优化查询机制的现有MongoDB集合配合使用
- 数据模型 对于多个特定于域的实体:
- 小说 - 章节 - 角色 - 问答知识库
- Python Scraper集成:作为子模块,用于从各种来源填充数据库
🔧 建筑
┌───────────────────┐
│ │
│ Language │
│ Models │
│ │
└─────────┬─────────┘
│
│ MCP (JSON-RPC 2.0)
▼
┌─────────────────────────────────────────────────────┐
│ │
│ ┌─────────────────┐ ┌─────────────────┐ │
│ │ │ │ │ │
│ │ SSE Server │◄────────►│ MCP Handler │ │
│ │ (Port 3000) │ │ │ │
│ └─────────────────┘ └────────┬────────┘ │
│ │ │
│ ▼ │
│ ┌────────────────┐ │
│ ┌─────────────────┐ │ │ │
│ │ │ │ DB Services │ │
│ │ CRUD API │◄────────►│ │ │
│ │ (Port 3001) │ └────────────────┘ │
│ └─────────────────┘ │ │
│ │ │
└─────────────────────────────────────────┼───────────┘
│
▼
┌─────────────────┐
│ │
│ MongoDB │
│ │
└─────────────────┘🏁 入门指南
先决条件
- Rust(最新稳定的工具链)
- MongoDB实例
- Python 3.7+(用于数据抓取器)
安装
- 使用子模块克隆存储库:
git clone --recursive https://github.com/SFBB/mcp-mongodb-novel-server.git
cd mcp-mongodb-novel-server- 配置环境:
创建一个 .env 项目根目录中的文件:
MONGODB_URI=mongodb://localhost:27017
DATABASE_NAME=your_database
PORT=3000 # Base port (MCP SSE server)
# CRUD API will use PORT+1 (3001)- 构建并运行:
cargo build --release
cargo run --release成功启动后,您将看到:
- MCP SSE端点位于http://127.0.0.1:3000/sse
- MCP POST端点位于http://127.0.0.1:3000/message
- CRUD API端点位于http://127.0.0.1:3001/api/...
用法
您首先需要运行此服务器。然后基于JSON-RPC或SSE配置您的客户端。
这是VSCode副驾驶客户端的示例设置。
"mcp-mongodb-novel-server": {
"type": "sse",
"url": "http://localhost:3000/sse"
} 🔌 服务器端点
1.MCP服务器(SSE协议)
MCP服务器提供两个端点:
- SSE 终端:
http://localhost:3000/sse
- 用于建立SSE连接以接收事件
- POST端点:
http://localhost:3000/message
- 用于发送JSON-RPC 2.0格式的命令
MCP请求示例(使用POST /message):
{
"jsonrpc": "2.0",
"id": "request-1",
"method": "query_character",
"params": {
"character_id": "5f8e4c3b2a1d"
}
}2.CRUD API(REST)
CRUD API为管理数据库内容提供了RESTful端点:
- 小说:
/api/novels - 章节:
/api/chapters - 角色:
/api/characters - 问答:
/api/qa
标准REST操作(GET, POST, PATCH, DELETE)支持。
📊 数据模型
小说
{
"_id": "ObjectId",
"title": "String",
"author": "String",
"summary": "String",
"year": "Number",
"tags": ["String"]
}章节
{
"_id": "ObjectId",
"novel_id": "ObjectId",
"title": "String",
"chapter_number": "Number",
"summary": "String",
"content": "String",
"word_count": "Number"
}角色
{
"_id": "ObjectId",
"novel_id": "ObjectId",
"name": "String",
"description": "String",
"traits": ["String"],
"relationships": [
{
"character_id": "ObjectId",
"type": "String"
}
]
}问答
{
"_id": "ObjectId",
"question": "String",
"answer": "String",
"tags": ["String"],
"novel_id": "ObjectId"
}🤖 MCP方法
服务器支持以下MCP方法进行LLM交互:
查询方法
| 方法 | 说明 | 参数 |
|---|---|---|
query_character | 获取角色详细信息 | {"character_id": "string"} |
query_novel | 获取新元数据 | {"novel_id": "string"} |
query_chapter | 获取章节信息 | {"chapter_id": "string"} 或 {"chapter_number": number} 或 {"chapter_title": "string"} |
query_qa_regex | 按正则表达式查找问答条目 | {"regex_pattern": "string"} |
query_chapter_regex | 按正则表达式查找章节 | {"regex_pattern": "string"} |
query_character_regex | 通过正则表达式查找字符 | {"regex_pattern": "string"} |
更新方法
| 方法 | 说明 | 参数 |
|---|---|---|
update_chapter_summary | 更新章节摘要 | {"auth_token": "string", "chapter_id": "string", "summary": "string"} |
📥 数据总体
该项目包括Python抓取器作为子模块,以帮助填充MongoDB:
字符刮刀
cd character-scraper
pip install -r requirements.txt
python src/scraper.py --config config/settings.json新型刮刀库
cd scraper_library
pip install -r requirements.txt
python -m src.scrapers.scrape_syosetu --url 可用的刮板包括:
scrape_syosetu.py(Syosetu小说)scrape_69shu.py(蜀小说69篇)scrape_ximalaya.py(喜马拉雅有声读物)scrape_qa.py(问答内容)scrape_x_timeline.py(推特/X时间线)- 还有更多。..
🛠️ 发展
更新子模块
# Update all submodules to their latest version
git submodule update --remote --merge
# Navigate to a specific submodule and pull changes
cd scraper_library
git pull origin main
cd ..
git add scraper_library
git commit -m "Update scraper_library"运行测试
cargo test代码的风格
本项目遵循Rust的标准代码风格指南:
# Check code formatting
cargo fmt -- --check
# Run linting
cargo clippy性能调整
对于高流量部署,请考虑:
- 用于文本搜索和关系的MongoDB索引
- 为CPU受限的操作增加Tokio工作线程
- 配置MongoDB连接池
📄 许可证
此项目根据MIT许可证获得许可-有关详细信息,请参阅许可证文件。
