使用说明
RAG MCP服务器
一种模型上下文协议(MCP)服务器,使用本地嵌入和Qdrant向量数据库提供RAG(检索增强生成)功能。
特性
- 🔍 语义搜索:使用自然语言查询搜索文档
- 📚 文档管理:添加、搜索和管理集合中的文档
- 🤖 本地嵌入:使用具有可配置嵌入模型的Olama(默认:nomic嵌入文本)
- 💾 Qdrant集成:快速高效的矢量存储和检索
- 🔧 可配置的:通过环境变量轻松配置
先决条件
使用Docker启动Qdrant
docker run -p 6333:6333 -p 6334:6334 -v qdrant_storage:/qdrant/storage qdrant/qdrant
安装嵌入提供程序
选项A:使用Olama
# Pull the embedding model
ollama pull nomic-embed-text
# Start Ollama (if not already running)
ollama serve
选项B:使用LMStudio
- 打开LMStudio
- 下载嵌入模型(例如。,
nomic-embed-text-v1.5) - 在端口1234上启动本地服务器
- 加载嵌入模型
安装
- 克隆存储库:
git clone <your-repo>
### For Ollama:
```env
# Qdrant Configuration
QDRANT_URL=http://localhost:6333
# Embedding Provider
EMBEDDING_PROVIDER=ollama
EMBEDDING_BASE_URL=http://localhost:11434
EMBEDDING_MODEL=nomic-embed-text
# Default Collection
DEFAULT_COLLECTION=documents
对于LMStudio:
# Qdrant Configuration
QDRANT_URL=http://localhost:6333
# Embedding Provider
EMBEDDING_PROVIDER=lmstudio
EMBEDDING_BASE_URL=http://localhost:1234
EMBEDDING_MODEL=text-embedding-nomic-embed-text-v1.5
3. Configure environment (optional):
```bash
cp .env.example .env
# Edit .env with your settings
配置
创建一个 .env 文件或设置环境变量:
# Qdrant Configuration
QDRANT_URL=http://localhost:6333
# Ollama Configuration
OLLAMA_BASE_URL=http://localhost:11434
EMBEDDING_MODEL=nomic-embed-text
# Default Collection
DEFAULT_COLLECTION=documents
支持的嵌入模型
该服务器设计用于与任何Ollama兼容的嵌入模型配合使用:
- nomic嵌入文本 (默认):768d,8192上下文,需要任务前缀
- qwen3-embedding-0.6b:1024d,32k上下文,多语言
- qwen3-embedding-4b:2560d,32k上下文,最高性能
- 埋置宝石-300m:768d,2048上下文,轻量级
通过设置更改模型 EMBEDDING_MODEL 在你的 .env 文件。
可用工具
1. add_documents
使用可选元数据将文档添加到RAG系统。
参数:
documents(array,必填):要添加的文档列表content(string,必填):要索引的文本内容metadata(对象,可选):附加元数据(源、日期、标签等)
collection(字符串,可选):集合名称(默认:“documents”)
例子:
{
"documents": [
{
"content": "Python is a high-level programming language.",
"metadata": {"source": "wiki", "topic": "programming"}
}
],
"collection": "knowledge"
}
2. search
使用语义搜索搜索类似于查询的文档。
参数:
query(字符串,必填):搜索查询collection(字符串,可选):集合名称(默认:“documents”)limit(整数,可选):最大结果(1-50,默认值:5)score_threshold(数字,可选):最小相似性得分(0-1,默认值:0.0)
例子:
{
"query": "What is Python?",
"collection": "knowledge",
"limit": 3,
"score_threshold": 0.7
}
3. list_collections
在Qdrant中列出所有可用收藏。
参数: 无
4. delete_collection
从Qdrant中删除集合。 ⚠️ 小心使用!
参数:
collection(字符串,必填):要删除的集合名称
使用MCP
添加到LMStudio MCP配置中:
使用Olama进行嵌入:
{
"mcpServers": {
"rag": {
"command": "python",
"args": ["-m", "src.server"],
"cwd": "/path/to/rag-mcp",
"env": {
"QDRANT_URL": "http://localhost:6333",
"EMBEDDING_PROVIDER": "ollama",
"EMBEDDING_BASE_URL": "http://localhost:11434",
"EMBEDDING_MODEL": "nomic-embed-text"
}
}
}
}
使用LMStudio进行嵌入:
{
"mcpServers": {
"rag": {
"command": "python",
"args": ["-m", "src.server"],
"cwd": "/path/to/rag-mcp",
"env": {
"QDRANT_URL": "http://localhost:6333",
"EMBEDDING_PROVIDER": "lmstudio",
"EMBEDDING_BASE_URL": "http://localhost:1234",
"EMBEDDING_MODEL": "text-embedding-nomic-embed-text-v1.5
### Using with LMStudio
Add to your LMStudio MCP configuration:
```json
{
"mcpServers": {
"rag": {
"command": "python",
"args": ["-m", "src.server"],
"cwd": "/path/to/rag-mcp",
"env": {
"QDRANT_URL": "http://localhost:6333",
"OLLAMA_BASE_URL": "http://localhost:11434",
"EMBEDDING_MODEL": "nomic-embed-text"
}
}
}
}
工作流示例
- 将文档添加到知识库:
# Use add_documents tool
{
"documents": [
{"content": "MCP is a protocol for AI tools"},
{"content": "Qdrant is a vector database"},
{"content": "Ollama runs local AI models"}
]
}
- 搜索相关信息:
# Use search tool
{Embedding Provider Issues
**Ollama:**
- Ensure Ollama is running: `ollama serve`
- Check the model is pulled: `ollama list`
- Verify `EMBEDDING_BASE_URL` is correct (default: `http://localhost:11434`)
**LMStudio:**
- Ensure LMStudio Local Server is running
- Verify the embedding model is loaded in LMStudio
- Check `EMBEDDING_BASE_URL` is correct (default: `http://localhost:1234`)
- Ensure CORS is enabled if needed
### Qdrant Connection Issues
- Ensure Qdrant is running on port 6333
- Check Docker logs: `docker logs <qdrant-container-id>`
- Verify `QDRANT_URL` is correct
### Embedding Errors
- nomic-embed-text requires task prefixes (handled automatically)
- Make sure `EMBEDDING_PROVIDER` is set correctly ("ollama" or "lmstudio"
## Development
### Project Structure
rag mcp/ ├── src/ │ ├── __初始化__南美国家巴拉圭的缩写(Paraguay) │ ├── server.py#MCP服务器实现 │ └── rag_engine.py#rag逻辑(嵌入,Qdrant) ├── pyproject.toml#项目配置 ├── .env.example#环境变量模板 └── README.md
### Adding New Features
The server is designed to be extensible. To add new tools:
1. Add the tool definition in `server.py` (`handle_list_tools`)
2. Implement the handler in `server.py` (`handle_call_tool`)
3. Add the business logic in `rag_engine.py`
## Troubleshooting
### Ollama Connection Issues
- Ensure Ollama is running: `ollama serve`
- Check the model is pulled: `ollama pull nomic-embed-text`
- Verify `OLLAMA_BASE_URL` is correct
### Qdrant Connection Issues
- Ensure Qdrant is running on port 6333
- Check Docker logs: `docker logs <qdrant-container-id>`
- Verify `QDRANT_URL` is correct
### Embedding Errors
- nomic-embed-text requires task prefixes (handled automatically)
- For other models, you may need to adjust the prefix logic in `rag_engine.py`
## License
MIT
## Contributing
Contributions welcome! Please open an issue or PR.
