代理RAG MCP服务器
一种智能代码库处理服务器,通过模型上下文协议(MCP)提供代理RAG(检索增强生成)功能。
特性
- 智能代码索引:自动分块和嵌入代码库以进行语义搜索
- 代理检索:确保全面上下文的自我批评检索循环
- 多模型架构:使用GPT-4o进行检索,使用Claude 3进行规划
- 实时更新:文件系统监视自动重新索引
- 成本控制:内置遥测和预算管理
快速安装
1.克隆和安装
# Clone the repository
git clone https://github.com/aibozo/agenticrag-mcp.git
cd agenticrag-mcp
# Run the installation script
./install.sh安装脚本将:
- 检查Python版本(需要3.8+)
- 创建虚拟环境
- 安装所有依赖项
- 提示输入API密钥
- 创建必要的目录
- 生成Claude配置
2.添加到克劳德
安装后,将AgentRAG添加到Claude:
Windows(克劳德桌面):
- 打开
%APPDATA%\Claude\claude_desktop_config.json - 从添加配置
claude_config_snippet.json
macOS/Linux(克劳德桌面版):
- 打开
~/.config/claude/claude_desktop_config.json - 从添加配置
claude_config_snippet.json
3.重启克劳德
重新启动Claude以加载新的MCP服务器。
手动安装
如果您更喜欢手动安装:
# Create virtual environment
python3 -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
# Install dependencies
pip install -r requirements.txt
# Copy and configure environment
cp .env.example .env
# Edit .env and add your OPENAI_API_KEY
# Add to Claude Desktop
claude mcp add agenticrag python3 /path/to/agenticrag-mcp/mcp_launcher.py用法
安装后,您可以在Claude中使用这些工具:
初始化存储库
Please index the repository at /path/to/my/project as "myproject"搜索您的代码
Search myproject for "how does authentication work?"获取存储库统计信息
Show me the stats for myproject对话示例
You: Can you index my FastAPI project at ~/projects/api?
Claude: I'll index your FastAPI project for you.
[Calls init_repo tool]
✓ Successfully indexed repository 'api'
- Total files: 47
- Total chunks: 312
- Total tokens: 52,841
- Duration: 4.32 seconds
You: How does the authentication middleware work?
Claude: Let me search for information about the authentication middleware.
[Calls search_repo tool]
Based on my search, the authentication middleware in your FastAPI project:
1. JWT Token Validation (auth/middleware.py, lines 15-45):
- Validates Bearer tokens on protected routes
- Extracts user claims from JWT payload
- Returns 401 for invalid/expired tokens
2. User Context (auth/middleware.py, lines 50-72):
- Adds user object to request state
- Enables access via request.state.user
The middleware is registered in main.py on line 23.配置
所需的环境变量
# .env file
OPENAI_API_KEY=sk-... # Required for embeddings and GPT-4
ANTHROPIC_API_KEY=sk-ant-... # Optional, for Claude models可选配置
# Vector Database
CHROMA_PERSIST_DIRECTORY=./chroma_db
# Logging
LOG_LEVEL=INFO
LOG_FILE=./logs/agenticrag.log
# Models
DEFAULT_EMBEDDING_MODEL=text-embedding-3-large
DEFAULT_LLM_MODEL=gpt-4o建筑
agenticrag-mcp/
├── src/
│ ├── agents/ # Agentic RAG implementation
│ │ ├── base.py # Base agent class
│ │ ├── retriever.py # Self-evaluating retriever
│ │ ├── compressor.py # Result compression
│ │ └── workflow.py # LangGraph orchestration
│ ├── indexing/ # Code indexing pipeline
│ │ ├── chunker.py # Semantic code chunking
│ │ ├── embedder.py # OpenAI embeddings
│ │ └── indexer.py # Repository indexer
│ ├── storage/ # Vector storage
│ │ └── vector_store.py # ChromaDB interface
│ └── mcp_server.py # MCP server implementation
├── mcp_launcher.py # MCP entry point
├── install.sh # Installation script
└── requirements.txt # Python dependencies运作原理
- 索引:系统会根据语言边界对代码进行分块,并创建嵌入
- 检索:搜索时,AI代理会生成优化的查询并检索相关块
- 自我评估:代理评估它是否有足够的上下文并可以执行其他搜索
- 压缩:对结果进行智能总结,以提供清晰、可操作的答案
故障排除
“没有名为'chromadb'的模块”
激活虚拟环境:
source venv/bin/activate“找不到OpenAI API密钥”
确保你的 .env 文件包含:
OPENAI_API_KEY=your-key-here“在Claude中找不到MCP服务器”
- 确保您已将配置添加到Claude的配置文件中
- 完全重新启动克劳德桌面
- 检查登录
./logs/agenticrag.log
搜索未返回任何结果
确保您首先使用init_repo工具对存储库进行了索引。
发展
运行测试
source venv/bin/activate
python -m pytest tests/局部测试
# Test indexing
python test_indexing.py
# Test agentic RAG
python test_agentic_rag.py贡献
欢迎投稿!拜托:
- 分叉存储库
- 创建要素分支(
git checkout -b feature/amazing-feature) - 提交您的更改(
git commit -m 'Add amazing feature') - 推到分支(
git push origin feature/amazing-feature) - 打开拉取请求
许可证
MIT许可证-请参阅 许可证 文件以获取详细信息。
致谢
- 用途 ChromaDB 用于矢量存储
如果您更喜欢手动安装:
# Create virtual environment
python3 -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
# Install dependencies
pip install -r requirements.txt
# Copy and configure environment
cp .env.example .env
# Edit .env with your API keys用法
安装后,您可以在Claude中使用这些工具:
为存储库建立索引
Use the init_repo tool to index a codebase:
- path: /path/to/your/project
- repo_name: my-project搜索代码
Use the search_repo tool to find relevant code:
- query: "How does the authentication system work?"
- repo_name: my-project获取统计信息
Use the get_repo_stats tool to see indexing statistics:
- repo_name: my-project对话示例
User: Index my Python project at /home/user/myproject
Claude: I'll index your Python project for semantic search.
[Uses init_repo tool with path="/home/user/myproject" and repo_name="myproject"]
User: Find all the database connection code
Claude: I'll search for database connection code in your project.
[Uses search_repo tool with query="database connection" and repo_name="myproject"]
[Returns relevant code snippets with file paths and explanations]配置
服务器可以通过中的环境变量进行配置 .env:
# API Keys (required)
OPENAI_API_KEY=sk-...
ANTHROPIC_API_KEY=sk-ant-...
# Optional configurations
CHUNK_SIZE_TOKENS=1280 # Size of code chunks
MAX_FILE_SIZE_MB=2 # Maximum file size to index
DAILY_BUDGET_USD=100 # Cost control limit故障排除
未找到模块
- 确保虚拟环境已激活:
source venv/bin/activate - 检查安装:
pip list | grep agenticrag
API密钥错误
- 验证密钥
.env文件 - 确保按键周围没有多余的空格或引号
- 检查所需模型的关键权限
克劳德找不到工具
- 验证配置路径是绝对的,而不是相对的
- 查看克劳德日志:帮助→ 显示日志
- 确保配置中存在MCP服务器部分
服务器无法启动
- 检查Python版本:
python3 --version(需要3.8+) - 验证Redis是否正在运行:
redis-cli ping - 检查端口可用性:
lsof -i:8000
性能问题
- 调整
CHUNK_SIZE_TOKENS为您的代码库 - 增加
EMBEDDING_BATCH_SIZE更快的索引 - 监控成本
get_repo_stats工具
发展
运行测试
# Activate virtual environment
source venv/bin/activate
# Run all tests
pytest
# Run with coverage
pytest --cov=src代码格式化
# Format code
black src tests
# Lint code
ruff check src tests项目结构
agenticrag-mcp/
├── src/ # Source code
│ ├── agents/ # AI agents
│ ├── api/ # API endpoints
│ ├── indexing/ # Code indexing
│ └── storage/ # Vector storage
├── tests/ # Test files
├── install.sh # Installation script
├── requirements.txt # Dependencies
└── .env.example # Environment template贡献
- 分叉存储库
- 创建要素分支:
git checkout -b feature-name - 进行更改和测试
- 提交拉取请求
许可证
MIT许可证-有关详细信息,请参阅许可证文件
支持
- 问题:
- 讨论:
- 文档: 维基
