Sebaescu/rag-mcp-server
If you are the rightful owner of rag-mcp-server and would like to certify it and/or have it hosted online, please leave a comment on the right or send an email to dayong@mcphub.com.
The RAG MCP Server is a production-ready Retrieval-Augmented Generation server implementing the Model Context Protocol, designed for seamless integration with AI assistants like Cline.
RAG MCP Server
A production-ready Retrieval-Augmented Generation (RAG) server implementing the Model Context Protocol (MCP). Built for seamless integration with AI assistants like Cline, providing powerful semantic search capabilities over your documents.
✨ Features
- 🔍 Semantic Search: Query documents using natural language with vector similarity
- 🌐 Website Indexing: Automatically crawl and index entire websites
- 🚀 Dual Embedding Support: Choose between local (Xenova transformers) or OpenAI embeddings
- 🐳 Docker Ready: Complete Docker Compose setup with PostgreSQL + pgvector and Redis
- 📊 Health Monitoring: Built-in health checks and metrics endpoints
- 🔄 Batch Operations: Efficient bulk document processing
- 💾 Redis Caching: Fast query results with intelligent caching
- 🛡️ Production Grade: Graceful shutdown, error handling, and comprehensive logging
🏗️ Architecture
┌─────────────┐
│ Cline AI │
└──────┬──────┘
│ MCP Protocol (stdio)
↓
┌─────────────────────────┐
│ RAG MCP Server │
│ - Query Handler │
│ - Document Manager │
│ - Website Crawler │
└──────┬──────────────────┘
│
├──→ PostgreSQL + pgvector (Vector DB)
├──→ Redis (Cache Layer)
└──→ Embedding Service (Local/OpenAI)
🚀 Quick Start
Using Docker (Recommended)
-
Clone and setup:
git clone <your-repo-url> cd rag-mcp-server cp .env.example .env -
Configure environment:
Edit
.envand set your preferences:EMBEDDING_PROVIDER=local # or 'openai' OPENAI_API_KEY=sk-... # only if using OpenAI -
Start services:
npm run docker:up -
Verify health:
curl http://localhost:3000/health
Local Development
-
Prerequisites:
- Node.js 20+
- PostgreSQL 16 with pgvector
- Redis 7+
-
Install dependencies:
npm install -
Setup database:
psql -U postgres -f docker/init.sql -
Configure environment:
cp .env.example .env # Edit .env with your database credentials -
Build and run:
npm run build npm startOr for development with hot reload:
npm run dev
⚙️ Configuration
Embedding Providers
Local Embeddings (Default)
Uses Xenova/transformers for on-device embeddings:
EMBEDDING_PROVIDER=local
- ✅ Free and private
- ✅ No API keys needed
- ✅ Works offline
- ⚠️ Lower quality than OpenAI
- ⚠️ Slower for large batches
OpenAI Embeddings
Uses OpenAI's embedding models for highest quality:
EMBEDDING_PROVIDER=openai
OPENAI_API_KEY=sk-your-api-key
OPENAI_EMBEDDING_MODEL=text-embedding-3-small # or text-embedding-3-large
- ✅ Best quality embeddings
- ✅ Fast batch processing
- ⚠️ Requires API key and costs money
- ⚠️ Sends data to OpenAI
Environment Variables
| Variable | Description | Default |
|---|---|---|
EMBEDDING_PROVIDER | local or openai | local |
OPENAI_API_KEY | OpenAI API key (if provider=openai) | - |
OPENAI_EMBEDDING_MODEL | OpenAI model name | text-embedding-3-small |
DB_HOST | PostgreSQL host | localhost |
DB_PORT | PostgreSQL port | 5432 |
DB_NAME | Database name | rag_db |
DB_USER | Database user | rag_user |
DB_PASSWORD | Database password | - |
REDIS_HOST | Redis host | localhost |
REDIS_PORT | Redis port | 6379 |
REDIS_DB | Redis database number | 1 |
PORT | HTTP server port | 3000 |
NODE_ENV | Environment | production |
🛠️ MCP Tools Reference
rag_query
Query the RAG system for relevant documents.
Parameters:
query(string, required): Search querytopK(number, optional): Number of results (default: 5)similarityThreshold(number, optional): Minimum similarity 0-1 (default: 0.7)
Example:
{
"query": "How to deploy with Docker?",
"topK": 3,
"similarityThreshold": 0.75
}
rag_add_document
Add a single document to the system.
Parameters:
content(string, required): Document textmetadata(object, optional): Custom metadata
Example:
{
"content": "Docker deployment guide...",
"metadata": {
"source": "docs",
"tags": ["docker", "deployment"]
}
}
rag_add_documents
Batch add multiple documents (more efficient).
Parameters:
documents(array, required): Array of document objects
Example:
{
"documents": [
{"content": "First doc...", "metadata": {"tag": "intro"}},
{"content": "Second doc...", "metadata": {"tag": "advanced"}}
]
}
rag_delete_document
Delete a document by ID.
Parameters:
documentId(number, required): Document ID to delete
rag_index_website
Crawl and index an entire website.
Parameters:
url(string, required): Website URLmaxDepth(number, optional): Crawl depth (default: 2)maxPages(number, optional): Max pages to crawl (default: 50)
Example:
{
"url": "https://docs.example.com",
"maxDepth": 3,
"maxPages": 100
}
rag_get_stats
Get system statistics and health information.
🏥 Health Checks & Monitoring
Health Endpoint
curl http://localhost:3000/health
Response:
{
"status": "healthy",
"uptime": 3600,
"database": "connected",
"timestamp": "2024-01-15T10:30:00.000Z"
}
Metrics Endpoint
curl http://localhost:3000/metrics
Response:
{
"requestCount": 1247,
"errorCount": 3,
"uptime": 86400,
"errorRate": "0.24%"
}
📦 Development Commands
npm run build # Compile TypeScript to dist/
npm run dev # Development mode with hot reload
npm start # Start production server
npm test # Run tests
npm run lint # Run ESLint
# Docker commands
npm run docker:build # Build Docker image
npm run docker:up # Start all services
npm run docker:down # Stop all services
npm run docker:logs # View server logs
🗄️ Database Schema
Tables
rag.documents
id: Primary keycontent: Document textmetadata: JSONB metadatasource_url: Origin URLtitle: Document titlecreated_at,updated_at: Timestamps
rag.embeddings
id: Primary keydocument_id: Foreign key to documentsembedding: vector(384) - Embedding vectorcreated_at,updated_at: Timestamps
Indexes
- HNSW index on embeddings for fast vector similarity
- GIN index on metadata for JSON queries
- B-tree indexes on common query fields
🔧 Troubleshooting
Database connection issues
# Check PostgreSQL is running
docker-compose ps
# Check database logs
docker-compose logs postgres
Embedding model download issues
# Clear model cache
rm -rf models/
# Restart with fresh download
npm run docker:down && npm run docker:up
Port conflicts
If port 3000, 5432, or 6379 is already in use, update .env:
PORT=3001
DB_PORT=5433
REDIS_PORT=6380
📚 Learn More
Built with ❤️ for the AI community