RAG-Anything MCP Server
README.md
# ๐ RAG-Anything MCP Server
A powerful **Model Context Protocol (MCP)** server that exposes **RAG-Anything** capabilities, enabling any MCP-compatible application to perform advanced multi-modal document processing and retrieval.
## ๐ Features
- **๐ Multi-format Document Processing**: PDF, Office documents, images, text files
- **๐ Intelligent RAG Queries**: Text and multimodal query support
- **๐ง Knowledge Graph Integration**: Powered by LightRAG
- **๐ผ๏ธ Vision Language Model**: Analyze images, tables, and equations
- **๐ MCP Protocol**: Standardized interface for any client application
- **โก Async Operations**: High-performance async processing
## ๐๏ธ Architecture
```
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ MCP Clients โ
โ (VS Code, Claude Desktop, Custom Apps, etc.) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
MCP Protocol
โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ RAG-Anything MCP Server โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ Document Processing Tools โ โ
โ โ - upload_document โ โ
โ โ - process_document โ โ
โ โ - batch_process โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ Query Tools โ โ
โ โ - query_text โ โ
โ โ - query_multimodal โ โ
โ โ - query_with_context โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ Management Tools โ โ
โ โ - list_documents โ โ
โ โ - get_document_info โ โ
โ โ - delete_document โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ RAG-Anything Core โ
โ โโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโ โ
โ โ MinerU โ โ LightRAG โ โ VLM โ โ
โ โ Parser โ โ Knowledge โ โ Analysis โ โ
โ โ โ โ Graph โ โ โ โ
โ โโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
```
## ๐ Quick Start
### Installation
```bash
# Clone repository
git clone <your-repo-url>
cd twin-editor-rag
# Install dependencies
pip install -e .
# Or with development dependencies
pip install -e ".[dev]"
```
### Configuration
Create a `.env` file:
```bash
# API Configuration
OPENAI_API_KEY=your_openai_api_key
OPENAI_BASE_URL=https://api.openai.com/v1 # Optional
# Model Configuration
LLM_MODEL=gpt-4o-mini
VISION_MODEL=gpt-4o
EMBEDDING_MODEL=text-embedding-3-large
EMBEDDING_DIM=3072
# Model Profile Configuration (optional)
MODEL_PROFILE=openai_default
MODEL_PROFILES_PATH=./model_profiles.json
# Or set MODEL_PROFILES to a JSON object string with profile definitions
# Storage Configuration
RAG_STORAGE_DIR=./rag_storage
UPLOAD_DIR=./uploads
MAX_FILE_SIZE=100 # MB
# Server Configuration
LOG_LEVEL=INFO
```
Model profiles let you switch the entire model set with a single env var. If
`MODEL_PROFILE` is set, it overrides `LLM_MODEL`, `VISION_MODEL`,
`EMBEDDING_MODEL`, and `EMBEDDING_DIM` using either `MODEL_PROFILES` (JSON
string) or `MODEL_PROFILES_PATH` (JSON file, defaults to
`./model_profiles.json`).
### Running the Server
```bash
# Start MCP server
python src/server.py
# Or use with specific config
python src/server.py --config config.json
```
### Using with MCP Clients
#### VS Code (with MCP Extension)
Add to your VS Code settings:
```json
{
"mcp.servers": {
"rag-anything": {
"command": "python",
"args": ["/path/to/twin-editor-rag/src/server.py"],
"env": {
"OPENAI_API_KEY": "your_key"
}
}
}
}
```
#### Claude Desktop
Add to `claude_desktop_config.json`:
```json
{
"mcpServers": {
"rag-anything": {
"command": "python",
"args": ["/path/to/twin-editor-rag/src/server.py"],
"env": {
"OPENAI_API_KEY": "your_key"
}
}
}
}
```
## ๐ Available Tools
### Document Processing
#### `upload_document`
Upload a document file to the server.
```python
# Parameters:
- file_path: str - Path to document file
- doc_id: Optional[str] - Custom document ID
# Returns:
- doc_id: str - Document identifier
- status: str - Upload status
```
#### `process_document`
Process an uploaded document with RAG-Anything.
```python
# Parameters:
- doc_id: str - Document identifier
- parser: Optional[str] - Parser to use (mineru/docling)
- parse_method: Optional[str] - Parse method (auto/ocr/txt)
# Returns:
- doc_id: str
- status: str
- stats: dict - Processing statistics
```
#### `batch_process_documents`
Process multiple documents in batch.
```python
# Parameters:
- file_paths: List[str] - List of file paths
- max_concurrent: Optional[int] - Max parallel processing
# Returns:
- results: List[dict] - Processing results
```
### Query Operations
#### `query_text`
Query the RAG system with text.
```python
# Parameters:
- query: str - Query text
- mode: str - Query mode (hybrid/local/global/naive)
- top_k: Optional[int] - Number of results
# Returns:
- answer: str - RAG answer
- sources: List[dict] - Source documents
```
#### `query_multimodal`
Query with multimodal content (images, tables, equations).
```python
# Parameters:
- query: str - Query text
- multimodal_content: List[dict] - Multimodal content
- mode: str - Query mode
# Returns:
- answer: str - RAG answer with multimodal understanding
```
### Management
#### `list_documents`
List all processed documents.
```python
# Returns:
- documents: List[dict] - Document list with metadata
```
#### `get_document_info`
Get detailed information about a document.
```python
# Parameters:
- doc_id: str - Document identifier
# Returns:
- doc_info: dict - Detailed document information
```
#### `delete_document`
Remove a document from the system.
```python
# Parameters:
- doc_id: str - Document identifier
# Returns:
- status: str - Deletion status
```
## ๐ง Advanced Configuration
### Custom RAG Configuration
```python
# config.json
{
"rag_config": {
"enable_image_processing": true,
"enable_table_processing": true,
"enable_equation_processing": true,
"context_window": 2,
"max_context_tokens": 3000
},
"parser_config": {
"default_parser": "mineru",
"default_parse_method": "auto"
}
}
```
### LLM Provider Configuration
Supports multiple providers:
- OpenAI
- Azure OpenAI
- Anthropic (Claude)
- Local models (LM Studio, Ollama)
## ๐ Examples
### Example 1: Process and Query Documents
```python
# Using MCP client
import mcp
client = mcp.Client("rag-anything")
# Upload and process
result = await client.call_tool("upload_document", {
"file_path": "./research_paper.pdf"
})
doc_id = result["doc_id"]
await client.call_tool("process_document", {
"doc_id": doc_id
})
# Query
answer = await client.call_tool("query_text", {
"query": "What are the main findings?",
"mode": "hybrid"
})
print(answer["answer"])
```
### Example 2: Multimodal Query
```python
# Query with table data
result = await client.call_tool("query_multimodal", {
"query": "Compare these metrics with document content",
"multimodal_content": [{
"type": "table",
"table_data": "Method,Accuracy\nRAG,95.2%\nBaseline,87.3%",
"table_caption": "Performance Comparison"
}],
"mode": "hybrid"
})
```
## ๐งช Testing
```bash
# Run tests
pytest tests/
# Run with coverage
pytest --cov=src tests/
# Test specific functionality
pytest tests/test_tools.py -v
```
## ๐ค Contributing
Contributions welcome! Please:
1. Fork the repository
2. Create a feature branch
3. Add tests for new features
4. Submit a pull request
## ๐ License
MIT License - See LICENSE file for details
## ๐ Related Projects
- [RAG-Anything](https://github.com/HKUDS/RAG-Anything) - Core RAG framework
- [LightRAG](https://github.com/HKUDS/LightRAG) - Knowledge graph RAG
- [MCP](https://github.com/modelcontextprotocol) - Model Context Protocol
## โญ Star History
If you find this useful, please star the repo!
## ๐ Deployment to Render.com
### Prerequisites
1. **Render Account**: Sign up at [render.com](https://render.com)
2. **OpenAI API Key**: Get your key from [OpenAI Platform](https://platform.openai.com)
3. **Git Repository**: Push your code to GitHub/GitLab
### Option 1: Deploy with render.yaml (Recommended)
1. **Connect Repository**: Link your GitHub/GitLab repo to Render
2. **Set Environment Variables**: In Render dashboard, add:
- `OPENAI_API_KEY`: Your OpenAI API key
- Other optional variables from `.env.example`
3. **Deploy**: Render will auto-detect `render.yaml` and deploy
### Option 2: Manual Deployment
1. **Create New Web Service** in Render Dashboard
2. **Configure Service**:
- **Environment**: Docker
- **Dockerfile Path**: `./Dockerfile`
- **Instance Type**: Standard (512 MB+ RAM recommended)
3. **Add Disk** (for persistent storage):
- **Name**: `rag-storage`
- **Mount Path**: `/app/rag_storage`
- **Size**: 10 GB
4. **Set Environment Variables**:
```
OPENAI_API_KEY=your_key_here
LLM_MODEL=gpt-4o-mini
VISION_MODEL=gpt-4o
EMBEDDING_MODEL=text-embedding-3-large
```
5. **Deploy**: Click "Create Web Service"
### Post-Deployment
Your MCP server will be available at:
```
https://your-app-name.onrender.com/mcp
```
Connect from your MCP client using HTTP transport:
```json
{
"servers": {
"rag-document-mcp": {
"url": "https://your-app-name.onrender.com/mcp",
"type": "http"
}
}
}
```
### Cost Estimation
- **Free Tier**: 750 hours/month (suitable for testing)
- **Starter**: $7/month (recommended for production)
- **Storage**: $0.25/GB/month
## ๐ง Contact
For questions or support, please open an issue on GitHub.