Skip to main content
Glama

🔌 MCP Docs Assistant

生产级检索增强生成(RAG)流水线,基于官方 Model Context Protocol 文档构建——通过 REST、MCP 工具和 Docker 提供。

用自然语言提问关于 MCP 的问题(架构、构建服务器/客户端、工具/资源/提示词、安全等),即可获得基于真实文档、可验证答案来源的回答,内置护栏、PII 脱敏、重排序、语义缓存和幻觉检测。


✨ 功能特性

能力

实现

🔀 多密钥 LLM 网关

由 Portkey 路由,在 2 个 Gemini key + 2 个 Groq key 之间负载均衡,并自动进行供应商回退

📚 基于文档的检索

官方 MCP 文档,切分块后嵌入到持久化的 Qdrant 向量存储

重排序

Cross-encoder(ms-marco-MinLiM-L-6-v2)将大广候选池缩小到最相关的知识块

🔯 安全护栏

NeMo Guardrails(Colang 2x)——输入/输出安全检查,越狱 + 指令泄漏检测

PII 遮罩

Microsoft Presidio——在输入和输出中屏蔽邮箱、电话、信用卡等信息

🧮 Token 运用

检索结果在进入 LLM 前按固定 token 预算进行贪婪适配

⚡ 语义缓存

基于嵌入相似度(非精确完全匹配下),带是否存在及容量期

💬 多轮会话

LangGraph 分析检查点 + 会话跟踪查询更新(可用于“展示一个 Python 示例”)

⌠ 幻觉检测

每次生成答案时使用 LL-as-judge 评分(GROUNDED / HALLUCINATED)

📄 离线评估

RAGAS 指标(忠实度、相关性、上下文准确/召回)数据库,基于 25 个参考 Q& 对

MCP 原生

将自身暴露为 MCP 工具(ask_mcp_docs、search_mcp_docs 等)——可直接在 Claude Desktop、Claude Code 或任意 MCP Host 脚本中使用

a RST API

提供 FastAPI 端点,适合任何传统 HTTP 客户端

Docker

使用 docker compose up 一键部署


Related MCP server: RLM Knowledge Base

Architecture

flowchart TD
    A[User Question] --> B[Guard Input<br/>NeMo Guardrails]
    B -->|blocked| Z[Refusal message]
    B -->|allowed| C[Mask Input PII<br/>Presidio]
    C --> D[Condense Follow-up<br/>into standalone question]
    D --> E{Semantic<br/>Cache Hit?}
    E -->|yes| F[Return cached answer]
    E -->|no| G[Retrieve Top-15<br/>Qdrant Vector Store]
    G --> H[Rerank Top-5<br/>Cross-Encoder]
    H --> I[Fit to Token Budget]
    I --> J[Generate Answer<br/>Portkey: Gemini / Groq]
    J --> K[Guard Output<br/>leak / PII pattern check]
    K --> L[Hallucination Check<br/>LLM-as-judge]
    L --> M[Mask Output PII]
    M --> N[Cache + Store History]
    N --> O[Return Answer]

Everything in the direct graph is run by modules in [rag_pipeline/] (./rag_pipeline), which are wired in a LangGraph StateGrap in [rag_pipeline/graph.py]. rag_core.py dial every dependency once-as a singleton-and builds a stable wrapper of APIs-chat()、search()、get_history (), cache_stats () — that are used identically by the REST layer ([main.py)] and the MCP layer ([mcp_server.py]). No matter which interface a request comes from, same vector store / cache / history is used.


📁 Project Structure

mcp-docs-rag-assistant/
├── main.py                    # FastAPI app — REST endpoints + mounts MCP at /mcp
├── mcp_server.py               # MCP tools (stdio standalone, or mounted in main.py)
├── rag_core.py                 # Singleton facade wiring the whole pipeline together
├── rag_pipeline/
│   ├── config.py                 # Env vars / secrets (single source of truth)
│   ├── logging_setup.py          # Logging + Logfire
│   ├── gateway.py                 # Portkey multi-key LLM gateway
│   ├── errors.py                   # Retry + safe-node error handling
│   ├── ingestion.py                 # MCP docs loader + splitter
│   ├── vectorstore.py                # Embeddings + persistent Qdrant store
│   ├── reranker.py                    # Cross-encoder reranking
│   ├── pii_masking.py                  # Presidio PII masking
│   ├── guardrails.py                    # NeMo Guardrails (Colang 2.x)
│   ├── token_management.py               # Context window budgeting
│   ├── semantic_cache.py                  # Embedding-similarity cache
│   ├── query_condensation.py               # Follow-up question rewriting
│   ├── hallucination.py                     # Runtime hallucination judge
│   └── graph.py                              # LangGraph StateGraph — full pipeline
├── scripts/
│   └── evaluate_ragas.py        # Offline RAGAS evaluation (25 reference Q&A)
├── tests/
│   └── test_pipeline.py         # Fast smoke tests (no API keys needed)
├── configs/guardrails/           # Colang rail files (generated at first run)
├── data/                          # Persisted Qdrant vector store (gitignored)
├── notebooks/                      # Original development notebook
├── Dockerfile
├── docker-compose.yml
├── requirements.txt
└── .env.example

Getting Started

Prerequisites

1. clone and set up a virtual environment

git clone https://github.com/<your-username>/mcp-docs-rag-assistant.git
cd mcp-docs-rag-assistant
python -m venv venv
venv\Scripts\activate          # Windows
# source venv/bin/activate     # macOS/Linux

2. Install dependencies

pip install -r requirements.txt
python -m spacy download en_core_web_sm   # required by Presidio for PII detection

3. Configure environment variables

cp .env.example .env

Open .env and fill in with your own keys (GEMINI_API_KEY_1/2, GROQ_API_KEY_1/2, PORTKEY_API_KEY, PORTKEY_CONFIG_ID).

4. Launch the server

uvicorn main:app --reload

First time only:: the vector store doesn't exist yet, so server ingests MCP docs and embs in limited-rate batches—this can take 5–10 minutes. On later runs it loads the stored store from data/qdrant_mcp_db/ instantly.

Once you see Startup: RAG pipeline ready., go to:

  • http://localhost:8000/docs — interactive Swgger UI, try POST /chat

  • http://localhost:8000/health — health check


📡 RST API

Method

Endpoint

Description

POST

/chat

Sent a question. Bory: {"question": ..., "thread_id": maybe}

POST

/search

Retrieve + re-rank raw context, no generation. body: {"query": ..., "top_n": 5}

GET

/history/{thread_}

Get 历史 for a thread

GET

/cache/sstats

Semantic cache observability

GET

/health

Health check

🔌 Using it as a MCP server

Standalone (stdio) — for Claude Desktop

Run directly:

python mcp_server.py

Or point a local MCP host at it, e.g. in Claude Desktop's config (claude_desktop_config.json):

{
  "mcpServers": {
    "mcp-docs-assistant": {
      "command": "python",
      "args": ["E:\\mcp-docs-rag-assistant\\mcp_server.py"]
    }
  }
}

Remote (streamable-http) — via FastAPI

When main.py is running, the same MCP tools are reachable at:

http://localhost:8000/mcp

Available tools: ask_mcp_docs, search_mcp_docs, get_conversation_history, cache_stats.

Docker

The only prerequisite is Docker Desktop (with Docker Compose), so you don't need to install Python, pip dependencies, or SpaCy model separately. All is done inside the image at build time (see the Dockerfile — it runs pip install -r requirements.txt and python -m spacy download en_core_web_sm).

# 1. Make sure .env exists (same as the local setup, step 3 above)
cp .env.example .env   # then fill in real keys

# 2. Build and run
docker compose up --build

That single command builds the image, installs everything, and starts the container. The data/ folder is mounted as a volume (see docker-compose.yml), so the vector store survives container restarts — you only pay the first-run embedding cost once.

Server is available at same as running locally: http://localhost:8000/docs.

To stop:

docker compose down

To rebuild after code changes:

docker compose up --build

🐳 Testing

Fast smoke tests — no API keys or network calls required (fake embeds):

pip install pytest
pytest tests/ -v

📄 Offline Evaluation (RAGAS)

The pipeline is evaluated against 25 hand-written MCP questions with reference answers using RAGAS:

python scripts/evaluate_ragas.py

This does not require the server to be running — it constructs the pipeline itself (same class as main.py/mcp_server.py) and displays metrics table:

  • Faithfulness — is the answer based on retrieved context?

  • Response Relevancy — does the answer exactly address the question?

  • Context Precision — is retrieved context relevant?

  • Context Recall — does the retrieved context cover the reference answer's needs?

⏱️ A few minutes: each of 25 questions performs a real retrieval + generation, then every metric is judged by LLM-as-judge.


⚙️ Configuration reference

All config in .env (see .env.example). Key vars:

Variable

Purpose

GEMINI_API_KEY_1/2, GROQ_API_KEY_1/2

Provider keys, load-balanced by Portkey

PORTKEY_API_KEY, PORTKEY_CONFIG_ID

For the Portkey gateway (credentials + routing config)

QDRANT_PATH, QDRANT_COLLECTION

Vector DB path + collection name

LOGFIRE_TOKEN

Optional — set to fall back to console logging

HOST,PORT

Server address and port


🛠️ Tech Stack

FastAPI · LangChain · LangGraph · Qdrant · Portkey · Sentence-Transformers · Presidio · NeMo Guardrails · RAGAS · MCP Python SDK · Docker


📄 License

MIT — see LICENSE.

Related MCP Connectors

  • Query your own PDFs and documents from any MCP client. Every answer cites the page it came from.

    https://mcp.docs2mcp.com/mcp
  • The AWS Knowledge MCP server is a fully managed remote Model Context Protocol server that provides real-time access to official AWS content in an LLM-compatible format. It offers structured access to AWS documentation, code samples, blog posts, What's New announcements, Well-Architected best practices, and regional availability information for AWS APIs and CloudFormation resources. Key capabilities include searching and reading documentation in markdown format, getting content recommendations, listing AWS regions, and checking regional availability for services and features.

  • The CustomGPT.ai MCP server is a fully managed, RAG-powered endpoint that connects large language models with private knowledge bases and external data sources. It provides tools for retrieval-augmented generation queries (send_message), data ingestion (upload_file), and source listing, enabling AI agents to query private documents like PDFs with high accuracy and real-time citations.

    https://mcp.customgpt.ai
  • The Grafbase MCP server sits in front of a GraphQL API and exposes an MCP protocol-compliant interface that allows AI agents and LLMs to explore and query GraphQL APIs using natural language. It provides tools to search schemas, introspect types and fields, and execute GraphQL queries while minimizing context bloat by returning only relevant schema subsets, with built-in support for authentication, authorization, and configurable access control.

    https://api.grafbase.com/mcp

Related MCP Servers