RAG Chat Assistant MCP Server
Enables document Q&A and knowledge retrieval through hybrid semantic and keyword search, with tools for document ingestion, chunking, summarization, PII redaction, and RAGAS-based evaluation.
README
RAG Chat Assistant
A document Q&A Chat Assistant powered by Retrieval-Augmented Generation (RAG). Uses a hybrid retrieval system (semantic + keyword search) with an MCP (Model Context Protocol) server/client architecture, PII redaction, automated evaluation via RAGAS, and full observability tracing.
Architecture
Streamlit Chat UI (Client - .venv)
↕ MCP Protocol (Streamable HTTP on localhost:8000)
MCP Server (FastMCP - .mcpvenv)
├── Tools: filesystem, doc_loader, chunker, ingest, retriever
├── Agents: RAG Agent, Summarizer, PII Redactor, Evaluator
├── Storage: ChromaDB (vector) + BM25 (keyword) + Registry
└── External: Ollama (LLM + Embeddings), Opik (Observability)
Project Structure
L3June26_Assignment/
├── MCP_Stack/ # MCP Server (runs in .mcpvenv)
│ ├── agents/
│ │ ├── rag_agent.py # LangGraph RAG agent (retrieve → generate)
│ │ ├── summarizer_agent.py # Iterative document summarization with caching
│ │ ├── pii_redactor.py # Regex + optional LLM-based PII detection
│ │ └── evaluator_agent.py # RAGAS evaluation + ground-truth generator
│ ├── tools/
│ │ ├── doc_loader.py # Multi-format document loading (PDF/DOCX/TXT/CSV/XLSX/XML/images)
│ │ ├── chunker.py # Semantic chunking with metadata
│ │ ├── ingest.py # Ingestion pipeline + document registry
│ │ ├── retriever.py # Hybrid search (ChromaDB + BM25 + reranking)
│ │ └── filesystem.py # Sandboxed file browsing
│ ├── mcp_server.py # FastMCP server entry point
│ ├── config.py # Server configuration
│ ├── .env.example # Server secrets template
│ ├── requirements_mcp.txt # Server dependencies
│ ├── knowledge_source/ # Drop documents here for ingestion
│ ├── knowledge_base/ # ChromaDB + BM25 index + registry.json (auto-generated)
│ ├── Server_Logs/ # Per-session JSONL logs
│ └── cache/ # Summarizer cache (by content hash)
├── tests/ # All tests
│ ├── test_property_*.py # Property-based tests (Hypothesis)
│ ├── test_unit_*.py # Unit tests
│ └── test_integration_*.py # Integration tests
├── Client_Logs/ # Client JSONL logs
├── streamlit_app.py # Streamlit chat UI (runs in .venv)
├── config.py # Client configuration
├── .env.example # Client secrets template
├── requirements.txt # Client dependencies
├── test_tools.py # Manual test stub for tools/agents
└── README.md
Prerequisites
| Dependency | Purpose |
|---|---|
| Python 3.12 | Runtime (RAGAS has compatibility issues with 3.14) |
| uv | Package manager (replaces pip) |
| Ollama | Local/cloud LLM serving |
| Tesseract OCR (optional) | Primary OCR for images; if unavailable, falls back to gemma4:31b-cloud vision model |
Setup
1. Pull Required Ollama Models
# Chat model (cloud-hosted, no local GPU needed)
ollama pull gpt-oss:120b-cloud
# Embedding model
ollama pull nomic-embed-text
# Vision model (OCR fallback — cloud-hosted, no local GPU needed)
ollama pull gemma4:31b-cloud
2. Create Virtual Environments
MCP Server (.mcpvenv):
uv venv .mcpvenv --python 3.12
# Windows
.mcpvenv\Scripts\activate
# Linux/macOS
source .mcpvenv/bin/activate
uv pip install -r MCP_Stack/requirements_mcp.txt
Streamlit Client (.venv):
uv venv .venv --python 3.12
# Windows
.venv\Scripts\activate
# Linux/macOS
source .venv/bin/activate
uv pip install -r requirements.txt
3. Configure Environment Variables
# Copy templates
cp .env.example .env
cp MCP_Stack/.env.example MCP_Stack/.env
Edit each .env file with your actual values:
Client .env:
OLLAMA_BASE_URL=http://localhost:11434
ORCHESTRATOR_MODEL=gpt-oss:120b-cloud
MCP_SERVER_URL=http://localhost:8000/mcp
ENABLE_OPIK_TRACING=false
OPIK_API_KEY=<your-key>
OPIK_WORKSPACE=<your-workspace>
OPIK_PROJECT_NAME=rag-chat-assistant
Server MCP_Stack/.env:
OLLAMA_BASE_URL=http://localhost:11434
DEFAULT_MODEL=gpt-oss:120b-cloud
EMBEDDING_MODEL=nomic-embed-text
VISION_MODEL=gemma4:31b-cloud
CHUNK_SIZE=2000
CHUNK_OVERLAP=200
RETRIEVAL_TOP_K=5
SEMANTIC_WEIGHT=0.7
PII_USE_LLM=false
ENABLE_RAGAS_EVAL=false
MCP_SERVER_PORT=8000
4. Add Documents to Knowledge Source
Place your documents (PDF, DOCX, TXT, CSV, XLSX, XML, or images) into:
MCP_Stack/knowledge_source/
These will be automatically ingested when the MCP server starts.
Running the Application
Step 1: Start the MCP Server
Open a terminal and activate the server environment:
# Windows
.mcpvenv\Scripts\activate
# Linux/macOS
source .mcpvenv/bin/activate
# Start the server
python -m MCP_Stack.mcp_server
On startup, the server will:
- Inject SSL certificates (truststore)
- Load existing knowledge base from disk
- Scan
knowledge_source/and ingest any new or modified documents - Skip unchanged documents (based on content hash)
- Register all tools and agents
- Serve MCP protocol on
http://localhost:8000/mcp
Note: Documents added to
knowledge_source/while the server is running will NOT be auto-detected. Restart the server to ingest new files.
Step 2: Start the Streamlit Client
Open a separate terminal and activate the client environment:
# Windows
.venv\Scripts\activate
# Linux/macOS
source .venv/bin/activate
# Start the UI
streamlit run streamlit_app.py
The chat UI will open in your browser (typically at http://localhost:8501).
Usage
Asking Questions
Type your question in the chat input. The RAG agent will:
- Search the knowledge base using hybrid retrieval (semantic + keyword)
- Generate an answer with citations to source documents
- Display RAGAS evaluation scores (if enabled)
Document Management
Through the chat interface you can:
- Browse files — list and inspect documents in
knowledge_source/ - Ingest manually — force re-ingest of a specific file or all files
- List documents — see all ingested documents with metadata
- Delete documents — remove a document from the knowledge base
- Summarize — get a concise summary of a long document
Ground-Truth Test Data Generation
Generate evaluation test data from your documents:
- Provide a document name from
knowledge_source/ - The system generates question-answer pairs with context passages
- Output is saved as JSON for use with RAGAS evaluation (faithfulness, answer relevancy, context precision, context recall)
Configuration Reference
Server Configuration (MCP_Stack/config.py)
| Parameter | Default | Description |
|---|---|---|
OLLAMA_BASE_URL |
http://localhost:11434 |
Ollama API endpoint |
DEFAULT_MODEL |
gpt-oss:120b-cloud |
Chat model for answer generation |
EMBEDDING_MODEL |
nomic-embed-text |
Embedding model for vector search |
VISION_MODEL |
gemma4:31b-cloud |
Cloud vision model (OCR fallback) |
MAX_TOKENS |
2048 |
Max tokens for generated responses |
TEMPERATURE |
0.7 |
LLM temperature |
CHUNK_SIZE |
2000 |
Characters per chunk (~500 tokens) |
CHUNK_OVERLAP |
200 |
Overlap between consecutive chunks |
RETRIEVAL_TOP_K |
5 |
Number of chunks to retrieve |
SEMANTIC_WEIGHT |
0.7 |
Semantic vs keyword balance (0.7 = 70% semantic) |
PII_USE_LLM |
false |
Enable LLM-based PII detection (slower, catches more) |
ENABLE_RAGAS_EVAL |
false |
Auto-evaluate responses with RAGAS |
MCP_SERVER_PORT |
8000 |
Server port |
Client Configuration (config.py)
| Parameter | Default | Description |
|---|---|---|
OLLAMA_BASE_URL |
http://localhost:11434 |
Ollama API endpoint |
ORCHESTRATOR_MODEL |
gpt-oss:120b-cloud |
Model for client-side orchestration |
MCP_SERVER_URL |
http://localhost:8000/mcp |
MCP server endpoint |
ENABLE_OPIK_TRACING |
false |
Enable Opik observability tracing |
Running Tests
# Activate the server environment (has all dependencies)
# Windows
.mcpvenv\Scripts\activate
# Linux/macOS
source .mcpvenv/bin/activate
# Run all tests
python -m pytest tests/ -v
# Run only property-based tests
python -m pytest tests/test_property_*.py -v
# Run only unit tests
python -m pytest tests/test_unit_*.py -v
# Run a specific test file
python -m pytest tests/test_unit_chunker.py -v
Key Design Decisions
| Decision | Choice | Rationale |
|---|---|---|
| Protocol | MCP over Streamable HTTP | Standardized tool/agent interface; single endpoint |
| Agent Framework | LangGraph | Stateful graph workflows with conditional routing |
| Vector Store | ChromaDB (persistent) | Local file-based; no external service needed |
| Keyword Search | rank-bm25 (BM25Okapi) | Lightweight in-process; complements semantic search |
| Embedding | nomic-embed-text via Ollama | Dedicated embedding model; local inference |
| OCR | Tesseract → gemma4:31b-cloud fallback | Tesseract is fast; cloud vision is available everywhere |
| Observability | Opik (by Comet) | Native LangChain callback integration |
| Evaluation | RAGAS | Standard RAG evaluation framework |
| SSL | truststore | Corporate proxy support via Windows cert store |
Troubleshooting
| Issue | Solution |
|---|---|
| SSL errors behind corporate proxy | Ensure truststore is installed and imported first in entry points |
| Ollama connection refused | Verify Ollama is running: ollama list |
| Empty OCR results | Install Tesseract, or ensure gemma4:31b-cloud is available via ollama pull gemma4:31b-cloud |
| MCP connection timeout | Check that the server is running on the configured port (default 8000) |
| Documents not appearing after adding | Restart the MCP server — ingestion only happens at startup |
| RAGAS scores not showing | Set ENABLE_RAGAS_EVAL=true in MCP_Stack/.env |
Supported Document Formats
| Format | Extensions | Method |
|---|---|---|
.pdf |
pypdf + pdfplumber fallback | |
| Word | .docx |
python-docx |
| Plain Text | .txt |
Direct read with encoding detection |
| CSV | .csv |
pandas |
| Excel | .xlsx |
openpyxl via pandas |
| XML | .xml |
xml.etree + lxml fallback |
| Images | .png, .jpg, .jpeg, .tiff |
Tesseract OCR → gemma4:31b-cloud fallback |
推荐服务器
Baidu Map
百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
Playwright MCP Server
一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。
Magic Component Platform (MCP)
一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。
Audiense Insights MCP Server
通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。
VeyraX
一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。
graphlit-mcp-server
模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。
Kagi MCP Server
一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。
e2b-mcp-server
使用 MCP 通过 e2b 运行代码。
Neon MCP Server
用于与 Neon 管理 API 和数据库交互的 MCP 服务器
Exa MCP Server
模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。