MCP RAG Server

MCP RAG Server

Provides Retrieval-Augmented Generation capabilities by parsing documents (PDF, Markdown) and storing them in ChromaDB for semantic search and context retrieval.

Category
访问服务器

README

MCP RAG Server

A Model Context Protocol (MCP) server for Retrieval-Augmented Generation with document parsing and vector database storage using ChromaDB.

Features

  • Document Processing: Supports PDF, Markdown, and text files with enhanced parsing
  • Vector Search: Uses ChromaDB v2 API for semantic similarity search
  • File Watching: Automatically processes new and changed files
  • MCP Integration: Full MCP protocol support with resources and tools
  • Large Document Support: Intelligent chunking for documents of any size
  • Robust Error Handling: Comprehensive error recovery and retry mechanisms
  • Silent Operation: No console output for clean MCP server behavior
  • Enhanced PDF Processing: Node.js-optimized PDF.js with memory management

Recent Improvements

Version 1.x Features

  • ChromaDB v2 Compatibility: Updated to use ChromaDB 2.4.6 API
  • PDF.js Node.js Fix: Resolved DOMMatrix errors using legacy build
  • Large Document Handling: Automatic chunking for files >4KB with reconstruction
  • Enhanced Error Recovery: Retry logic with exponential backoff
  • Silent MCP Operation: Removed all console logging for proper MCP server behavior
  • Memory Optimization: Batch processing and garbage collection for large PDFs
  • Content Sanitization: Robust handling of special characters and encoding issues

Prerequisites

  • Node.js 18+
  • ChromaDB server running and accessible (v0.4.0+)
  • Network access to your ChromaDB instance

Installation

  1. Clone and install dependencies:
npm install
  1. Build the project:
npm run build

Configuration

Environment Variables

Create a .env file in the project root:

# Chroma Database URL (required)
CHROMA_URL=http://localhost:8000

# Directory to watch for documents (optional)
WATCH_DIRECTORY=./documents

# Document chunking settings (optional)
CHUNK_SIZE=1000
CHUNK_OVERLAP=200

Command Line Options

You can also configure via command line:

node build/index.js \
  --chroma-url http://localhost:8000 \
  --watch-directory ./documents \
  --chunk-size 1000 \
  --chunk-overlap 200

Set the Chroma server URL via the CHROMA_URL environment variable. For example, in your MCP config:

{
  "env": {
    "CHROMA_URL": "http://localhost:8000"
  }
}

If not set, the default is http://localhost:8000.

ChromaDB Setup

This server requires ChromaDB v2 API. Install and run ChromaDB:

# Using pip
pip install chromadb

# Run ChromaDB server
chroma run --host 0.0.0.0 --port 8000

For Docker:

docker run -p 8000:8000 chromadb/chroma:latest

Testing ChromaDB Connection

Run the included test suite to verify your setup:

# Run all tests including connection verification
npm test

# Run specific connection tests
npm run test -- --grep "connection"

This will verify:

  • Network connectivity to ChromaDB v2 API
  • Document insertion and retrieval
  • Large document chunking and reconstruction
  • Search functionality

Running Tests

Run the comprehensive test suite:

# Run all tests
npm test

# Run with verbose output
npm run test:verbose

# Watch mode for development
npm run test:watch

Note: You may see a deprecation warning about the punycode module during tests. This is from a dependency and does not affect functionality.

Usage

As MCP Server

The server runs in stdio mode for MCP protocol communication:

npm run dev

The server operates silently without console output, suitable for MCP integration.

MCP Resources

  • rag://documents - List all documents
  • rag://document/{id} - Get specific document

MCP Tools

  • search - Query documents with semantic similarity
  • add_document - Manually add document content
  • remove_document - Delete a document by ID
  • status - Get server statistics

Document Support

File Type Extension Parser Features
PDF .pdf PDF.js (Legacy) ✅ Large file support, ✅ Memory optimization, ✅ Batch processing
Markdown .md, .markdown Marked ✅ HTML tag removal, ✅ UTF-8 support
Text .txt, .text UTF-8 ✅ Encoding detection, ✅ Special character handling

Large Document Handling

The server automatically handles large documents:

  • Files >4KB: Split into 4KB chunks with smart sentence boundaries
  • Content Reconstruction: Seamlessly rebuilds full documents when requested
  • Memory Efficiency: Batch processing prevents memory overflow
  • Progress Tracking: Metadata tracks chunk relationships for proper reconstruction

Troubleshooting

Connection Issues

Error: ECONNREFUSED or fetch failed

  1. Verify ChromaDB v2 is running:

    curl http://localhost:8000/api/v1/heartbeat
    
  2. Check for v1/v2 API compatibility:

    # Should return 200 OK for v2
    curl http://localhost:8000/api/v1/version
    
  3. Verify ChromaDB server configuration allows external connections

Error: 410 Gone responses

This indicates a v1/v2 API mismatch. Ensure you're running ChromaDB v0.4.0+ and the client is using v2.4.6+.

Error: chromadb-default-embed not found

The chromadb-default-embed package should be automatically installed. If you see this error:

npm install chromadb-default-embed

Document Processing Issues

PDF.js DOMMatrix errors:

Fixed in v1.x - Now uses Node.js-compatible legacy build.

Large PDF processing:

  • Enhanced in v1.x - Automatic batch processing with 100MB file size limits
  • Memory management with garbage collection between batches
  • 30-second timeout protection per PDF

Files not being processed:

  1. Check the watch directory exists and is readable
  2. Verify file extensions are supported
  3. Check file permissions
  4. Run tests to verify parsing functionality

Memory Issues

For large document collections:

  1. The server now includes automatic memory management
  2. Large documents are automatically chunked
  3. If still needed, increase Node.js memory limit:
    node --max-old-space-size=4096 build/index.js
    

Silent Operation

The server now runs silently by design for proper MCP integration. If you need debugging:

  1. Run tests with verbose output: npm run test:verbose
  2. Use the debug test files in src/tests/
  3. Check error responses from MCP tools

Known Issues

  • Punycode Deprecation Warning: You may see deprecation warnings about the punycode module from Node.js dependencies. This does not affect functionality and will be resolved when dependencies are updated.

Development

Project Structure

src/
├── database/
│   └── manager.ts          # ChromaDB v2 interface with chunking
├── parsers/
│   └── document-parser.ts  # Enhanced document parsing with PDF.js legacy
├── vector/
│   └── vector-store.ts     # Vector operations wrapper
├── watchers/
│   └── directory-watcher.ts # File system monitoring
├── tests/
│   ├── database.test.ts    # Database integration tests
│   ├── pdf.test.ts         # PDF parsing tests
│   ├── chroma-debug.test.ts # ChromaDB debugging
│   └── verification.test.ts # Document verification
└── index.ts               # Main MCP server

Building

# Clean build
npm run clean && npm run build

# Development with watch mode
npm run watch

Adding New Document Types

  1. Add parser logic to src/parsers/document-parser.ts
  2. Update file extension handling
  3. Add tests in src/tests/
  4. Consider chunking strategy for large files

Performance Optimization

ChromaDB Settings

For optimal performance, configure your ChromaDB server with:

  • ChromaDB v0.4.0+ for v2 API compatibility
  • Sufficient memory allocation (4GB+ recommended)
  • SSD storage for better I/O
  • Network optimization for remote connections

Document Chunking

The server now uses intelligent chunking:

  • Automatic chunking: Files >4KB split at sentence boundaries
  • Content reconstruction: Full documents rebuilt on retrieval
  • Metadata tracking: Chunk relationships preserved
  • Memory efficiency: Batch processing prevents overflow

Adjust chunk settings based on your use case:

  • Small chunks (500-800 chars): Better precision, more storage
  • Large chunks (1200-2000 chars): Better context, less storage
  • Overlap: 10-20% of chunk size for continuity

Security Considerations

  • ChromaDB server should be behind a firewall
  • Use authentication if ChromaDB supports it
  • Validate document sources before processing
  • Monitor resource usage to prevent DoS
  • Content sanitization prevents injection attacks

API Reference

MCP Tools

search

Query documents using semantic similarity.

Parameters:

  • query (string): Search query
  • limit (number, optional): Max results (default: 5)

Returns: Array of matching document chunks with scores

Enhanced Features:

  • ✅ Searches across chunked content
  • ✅ Includes parent document information
  • ✅ Cosine similarity scoring

add_document

Add document content manually.

Parameters:

  • content (string): Document text content
  • filename (string): Document filename
  • metadata (object, optional): Additional metadata

Returns: Success message with chunk count

Enhanced Features:

  • ✅ Automatic chunking for large content
  • ✅ Content sanitization
  • ✅ Retry logic with exponential backoff

remove_document

Delete a document by ID.

Parameters:

  • id (string): Document ID

Returns: Success confirmation

Enhanced Features:

  • ✅ Removes all associated chunks
  • ✅ Graceful error handling

status

Get server statistics and configuration.

Returns: Object with document count, chunk count, total size, and configuration

Enhanced Features:

  • ✅ Real-time statistics
  • ✅ Memory usage information
  • ✅ ChromaDB connection status

Migration Guide

Future Versions

When upgrading to future versions:

  1. Update Dependencies: Run npm update to get latest compatible versions
  2. Update Environment Variables: Check for any new configuration options
  3. Rebuild Project: Run npm run clean && npm run build
  4. Test Connection: Run npm test to verify compatibility

Breaking Changes in v1.x

  • Environment variable CHROMA_HOST renamed to CHROMA_URL
  • ChromaDB v1 API no longer supported
  • Console logging removed (use tests for debugging)

License

MIT

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Add tests for new functionality
  4. Ensure all tests pass (npm test)
  5. Verify no console output in main code
  6. Submit a pull request

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选