mcp-rag
Enables semantic search and question answering over a knowledge base using hybrid retrieval and grounded answers, all running offline with no API keys.
README
mcp-rag — Semantic RAG served over the Model Context Protocol
A semantic Retrieval-Augmented Generation engine, exposed as an MCP server so any MCP client (Claude Desktop, an IDE agent, …) can search and question a knowledge base as a native tool.
documents ─▶ chunk ─▶ embed ─▶ index ─┐
├─▶ hybrid retrieval ─▶ grounded answer
query ─────────────────────────────────┘ (semantic + BM25, with citations
fused by RRF) │
▼
MCP server ─▶ any MCP client
The whole thing runs offline out of the box — local LSA embeddings + an extractive, citation-grounded answerer — with zero API keys or model downloads. Production backends (Voyage / OpenAI / sentence-transformers for embeddings, Anthropic / OpenAI for generation) are a one-line config switch.
Demo corpus: a self-contained fictional SaaS knowledge base ("Nimbus" — a cloud data platform): authentication, billing, rate limits, data retention, security, incident runbook, webhooks, SDK. Nothing copyrighted; every answer is traceable to a source.
What it does
Ask a question phrased in your own words and get an answer grounded in the docs:
$ python scripts/demo_query.py "what happens if I go over my included usage?"
A: When you exceed your included quota, Nimbus does not cut off your service;
instead, additional usage is billed as overage at the metered rate
($0.50 per extra 10k calls, $0.10 per extra GB). [1]
Sources:
[1] billing.md — Billing and quotas
Note there's no keyword overlap between "go over my included usage" and "exceed your quota / overage" — that match is semantic, which is the point.
MCP tools exposed
| Tool | Purpose |
|---|---|
search_documents(query, top_k, method) |
Return the most relevant passages (semantic / lexical / hybrid). |
answer_question(question, top_k) |
A grounded answer with citations. |
list_sources() |
Documents currently indexed. |
get_stats() |
Index size + active backends. |
Verified end-to-end over the real MCP stdio protocol (see tests/). Register it
in a client with mcp.json.
Retrieval ablation (computed by make eval)
17 gold questions, deliberately paraphrased away from the documents' wording.
hit@k = correct document in the top-k; MRR = how highly it's ranked.
| Method | hit@4 | MRR |
|---|---|---|
| Lexical (BM25) | 0.941 | 0.873 |
| Semantic (LSA) | 0.941 | 0.912 |
| Hybrid (RRF fusion) | 0.941 | 0.941 |
All three usually find the right document on this clean corpus, but hybrid ranks it highest most consistently — fusing dense (semantic) and sparse (keyword) retrieval is a tuning-free win, and the paraphrased questions are exactly where pure keyword search ranks worse.
With a neural embedding backend (Voyage/OpenAI/ST) on a larger, noisier corpus the gap between lexical and semantic widens further; the local LSA backend keeps the demo runnable anywhere while preserving the same ranking behaviour.
Quickstart
pip install -r requirements.txt && pip install -e .
make demo # ask a question from the CLI
make eval # retrieval ablation → reports/eval_results.json
make server # run the MCP server (stdio)
make test # 10 tests, incl. an end-to-end MCP protocol check
Use it from Claude Desktop
Copy mcp.json into your client config (set the absolute cwd), restart the
client, and the four tools appear. Ask "search the Nimbus docs for how failover
works" and the model calls search_documents / answer_question.
Switch to production backends
pip install -r requirements-prod.txt
export MCPRAG_EMBEDDING_BACKEND=voyage VOYAGE_API_KEY=...
export MCPRAG_GENERATOR_BACKEND=anthropic ANTHROPIC_API_KEY=...
No code changes — the retriever and server are backend-agnostic.
Layout
src/mcprag/
ingest.py markdown loading + section-aware chunking w/ overlap
embeddings/ Embedder protocol · local LSA (offline) · neural backends
index/vector_store.py cosine + BM25 + hybrid RRF retrieval
generator/ extractive (offline, cited) · LLM backends
rag.py RAGEngine (ingest→embed→index→retrieve→generate)
evaluation.py hit@k / MRR
server.py FastMCP server exposing the tools
data/corpus/ the Nimbus knowledge base (8 markdown docs)
eval/qa_gold.json paraphrased gold questions
scripts/ demo_query · run_eval
tests/ retrieval, answers, evaluation, MCP protocol
Design notes
- Grounded by construction. The offline generator only emits sentences taken verbatim from retrieved chunks, each with a citation — it cannot hallucinate.
- Hybrid retrieval. Reciprocal Rank Fusion of dense + sparse rankings needs no weight tuning and is robust across query types.
- Backend-agnostic. Embeddings and generation are pluggable Protocols; offline and production share identical retrieval/serving code.
See docs/ARCHITECTURE.md,
docs/RESULTS.md, and
docs/IMPROVEMENTS.md.
Tech stack
Python · MCP SDK (FastMCP) · scikit-learn (TF-IDF + LSA) · rank_bm25 · numpy · pydantic. Optional: Voyage / OpenAI / sentence-transformers / Anthropic.
License
MIT. The demo corpus is fictional.
推荐服务器
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 模型以安全和受控的方式获取实时的网络信息。