Skip to main content
Glama
nguyenngocdue

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.