检索增强生成服务器

一个提供检索增强生成(RAG)功能的模型上下文协议(MCP)服务器,用于文档查询,使AI助手能够智能查询文档数据库并检索带有源信息的上下文答案。

  • by M1T8E6
  • Python
  • 本地部署
  • 4 星标
  • 更新于
在 GitHub 查看 M1T8E6/RAG-MCP

使用说明

RAG MCP服务器

一种模型上下文协议(MCP)服务器,为文档查询提供RAG(检索增强生成)功能。此服务器使AI助手能够对您的文档数据库执行智能查询,并使用源信息检索上下文答案。

特性

  • 🔍 智能文档查询:对文档集合执行语义搜索
  • 📚 消息来源:获取具有相关性得分的源文档参考
  • 🔒 安全认证:API访问的基于令牌的身份验证
  • ⚡ 异步性能:内置async/await以实现高性能操作
  • 🛠️ MCP集成:与MCP兼容的AI助手无缝集成
  • 🔧 可配置的:针对不同RAG后端的灵活配置选项

安装

先决条件

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

使用pip

# Clone the repository
git clone https://github.com/yourusername/rag-mcp.git
cd rag-mcp-server

# Install dependencies
pip install -e .

使用紫外线(推荐)

# Clone the repository
git clone https://github.com/yourusername/rag-mcp.git
cd rag-mcp

# Install with uv
uv sync

配置

环境变量

创建一个 .env 项目根目录中的文件:

# RAG API Configuration
RAG_API_TOKEN=your_api_token_here
RAG_BASE_URL=https://your-rag-service.com/api-path

# Optional: Logging level
LOG_LEVEL=INFO

配置选项

  • RAG_API_TOKEN:用于RAG服务身份验证的API令牌(必需)
  • RAG_BASE_URL:RAG API服务的基本URL(可选,默认为占位符)

用法

运行服务器

直接执行

python src/server.py

使用Makefile

make run

MCP集成

将此服务器添加到MCP客户端配置中:

{
  "mcpServers": {
    "rag-server": {
      "command": "python",
      "args": ["/path/to/rag-mcp-server/src/server.py"],
      "env": {
        "RAG_API_TOKEN": "your_api_token_here",
        "RAG_BASE_URL": "https://your-rag-service.com/api-path"
      }
    }
  }
}

可用工具

configure_rag

使用您的API凭证配置RAG工具。

参数:

  • api_token (字符串,必需):您的RAG API令牌
  • base_url (字符串,可选):RAG服务的基本URL

例子:

{
  "api_token": "your_api_token_here",
  "base_url": "https://your-rag-service.com/api-path"
}

rag_docs

使用源属性对文档数据库执行智能查询。

参数:

  • query (string,必填):向RAG系统提出的问题

例子:

{
  "query": "What are the main features of our product?"
}

答复包括:

  • 根据您的文档生成答案
  • 具有相关性得分的源文件参考
  • 相关文档的内容片段

API集成

服务器希望您的RAG API遵循此界面:

请求格式

{
  "query": "Your question here",
}

响应格式

{
  "answer": "Generated response",
  "sources": [
    {
      "document": "document_name.pdf",
      "score": 0.95,
      "content": "Relevant text snippet..."
    }
  ]
}

发展

建立开发环境

# Install development dependencies
pip install -e ".[dev]"

# Or with uv
uv sync --group dev

代码质量

该项目使用多种工具来提高代码质量:

  • 黑色:代码格式
  • 拉夫:梳理和进口分拣
  • MyPy:类型检查
  • pytest:测试

运行质量检查:

# Format code
black src/

# Lint code
ruff check src/

# Type checking
mypy src/

# Run tests
pytest

项目结构

├── src/
│   ├── __init__.py
│   ├── server.py              # Main MCP server
│   └── tools/
│       ├── __init__.py
│       ├── base.py            # Base tool class
│       └── rag_tools.py       # RAG tool implementation
├── pyproject.toml             # Project configuration
├── Makefile                   # Build commands
└── README.md                  # This file

贡献

  1. 分叉存储库
  2. 创建要素分支: git checkout -b feature-name
  3. 进行更改
  4. 运行测试和质量检查
  5. 提交您的更改: git commit -am 'Add feature'
  6. 推到分支: git push origin feature-name
  7. 提交拉取请求

代码的风格

  • 遵循PEP 8指南
  • 使用类型提示
  • 为所有公共函数编写文档字符串
  • 保持功能集中和小型化
  • 为新功能添加测试

故障排除

常见问题

“RAG工具未配置”错误

  • 请务必致电 configure_rag 使用API令牌首先使用工具

连接超时错误

  • 检查你的 RAG_BASE_URL 正确且可访问
  • 验证您的网络连接
  • 确保RAG服务正在运行

身份验证失败

  • 验证您的 RAG_API_TOKEN 有效
  • 检查令牌是否具有适当的权限

调试模式

通过设置环境变量启用调试日志记录:

export LOG_LEVEL=DEBUG

许可证

此项目根据MIT许可证获得许可-请参阅 许可证 文件以获取详细信息。

支持

  • 📧 电子邮件: andreoni.matteo@proton.me

致谢