RAG Knowledge Base MCP Server
Enables searching a knowledge base and asking grounded questions with hybrid retrieval, reranking, and cited answers.
README
RAG Knowledge Base — Hybrid Search + Evaluation
A production-style Retrieval-Augmented Generation pipeline over a document knowledge base. It combines dense and sparse retrieval, cross-encoder reranking, source-grounded answers with citations, and a full evaluation harness that measures both retrieval and answer quality. Exposed over a REST API and as an MCP server for agentic access.
Built to run fully local at zero cost (PostgreSQL + pgvector, on-device embeddings), with a pluggable embedding backend so the same code runs against an API provider by changing one config value.
Why this is more than a basic RAG
| Concern | Approach |
|---|---|
| Retrieval | Hybrid search: pgvector cosine (dense) + Postgres full-text (sparse), fused with Reciprocal Rank Fusion |
| Ranking | Cross-encoder reranker scores each (query, chunk) pair directly |
| Grounding | Answers cite sources with [n] markers and refuse when the context is insufficient |
| Evaluation | Retrieval metrics (precision@k, recall@k, MRR) + LLM-as-judge faithfulness and answer relevance + refusal accuracy |
| A/B evaluation | Same harness runs each retrieval mode (vector / hybrid / hybrid+rerank) and reports the lift with numbers |
| Streaming | Answers stream token by token over Server-Sent Events |
| UI | Minimal web frontend with live streaming and clickable citations |
| Portability | Pluggable embedding backend (local sentence-transformers or Voyage API) |
| Agentic access | MCP server exposing search_knowledge_base and ask_knowledge_base tools |
Architecture
graph LR
subgraph Ingestion
DOCS[Documents\nmd / txt / pdf]
CHUNK[Chunker\nparagraph-aware + overlap]
EMB[Embedding backend\nlocal or api]
end
subgraph Store ["Vector Store — PostgreSQL + pgvector"]
VEC[(chunks\nvector + tsvector)]
end
subgraph Retrieval
DENSE[Vector search\ncosine / hnsw]
SPARSE[Keyword search\nfull-text / gin]
RRF[Reciprocal Rank Fusion]
RER[Cross-encoder rerank]
end
subgraph Generation
GEN[Claude\ngrounded + cited answer]
end
DOCS --> CHUNK --> EMB --> VEC
VEC --> DENSE --> RRF
VEC --> SPARSE --> RRF
RRF --> RER --> GEN
Stack
| Layer | Tool |
|---|---|
| Vector store | PostgreSQL + pgvector (HNSW index) |
| Keyword search | Postgres full-text search (GIN index) |
| Embeddings | sentence-transformers (local) / Voyage AI (optional) |
| Reranking | cross-encoder (sentence-transformers) |
| Generation | Claude (Anthropic) |
| Serving | FastAPI (REST + SSE streaming) + web UI + MCP server |
Quickstart
# 1. start the vector store
make db
# 2. install dependencies and set your key
make install
cp .env.example .env # add ANTHROPIC_API_KEY
# 3. ingest the sample knowledge base (fictional "Nimbus" product docs)
make ingest RESET=1
# 4. start the API and open the web UI
make api
# then open http://localhost:8000 in a browser, or query the API directly:
curl -X POST localhost:8000/ask \
-H "content-type: application/json" \
-d '{"question": "How much does the Standard tier cost?"}'
# 5. run the evaluation harness and the retrieval a/b comparison
make eval
make compare
Example response
{
"answer": "The Standard tier costs 99 US dollars per month. [1]",
"citations": [
{"marker": 1, "source": "nimbus_pricing.md", "title": "nimbus_pricing", "score": 8.42}
],
"retrieved": [
{"chunk_id": 7, "source": "nimbus_pricing.md", "score": 8.42, "preview": "..."}
]
}
Evaluation
The harness runs a gold question set (eval/dataset.py) and reports:
- Retrieval — precision@k, recall@k, mean reciprocal rank against known relevant sources
- Generation — faithfulness (are all claims grounded in the retrieved context) and answer relevance (does it match the reference), both judged by an LLM on a 0-1 scale
- Refusal accuracy — whether the system correctly declines to answer a question the knowledge base does not cover
python -m eval.run_eval
Results are printed as a summary table and written to eval/results/latest.json.
A/B comparison of retrieval modes
eval/compare.py runs the same gold set through each retrieval mode and reports
the lift, so design decisions are backed by numbers rather than asserted. It uses
only deterministic retrieval metrics, so it makes no LLM calls and costs nothing.
python -m eval.compare
On the sample corpus, reranking lifts top-1 retrieval accuracy from 92% to 100%:
mode k=1 k=3 k=5
------------------------------------------------------------------
vector only 0.923 / 0.846 1.0 / 0.885 1.0 / 0.885
hybrid (rrf) 0.923 / 0.846 1.0 / 0.885 1.0 / 0.885
hybrid + rerank 1.0 / 0.923 1.0 / 0.923 1.0 / 0.923
(recall@k / mrr@k)
The cross-encoder reranker fixes the case where a semantically-close distractor outranked the correct passage in the top position.
Web UI
Start the API with make api and open http://localhost:8000. The frontend
streams the answer token by token and renders the cited sources with their rerank
scores, so you can see exactly which passages grounded the response.
Adding your own documents
Drop .md, .txt or .pdf files into data/documents/ and re-run
make ingest RESET=1. The schema adapts to the embedding dimension of the
configured backend automatically.
Using it as an MCP server
The pipeline is exposed as an MCP server so an LLM agent can retrieve grounded facts on demand:
python -m mcp_server.server
Tools: search_knowledge_base(query, top_k) for raw passages and
ask_knowledge_base(question) for a grounded, cited answer.
The retrieval, ranking, generation and evaluation core was designed by hand. AI agents assisted with documentation, the web frontend and peripheral scaffolding.
推荐服务器
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 模型以安全和受控的方式获取实时的网络信息。