rag-mcp

rag-mcp

A local hybrid-search MCP server that enables coding agents to query files and folders using natural language, returning relevant code chunks with exact source paths. Everything runs on-device with no API keys or network calls.

Category
访问服务器

README


name: rag-mcp type: local semantic-search MCP server for coding agents and document workflows

rag-mcp

A coding agent should not have to choose between opening files one at a time and dumping an entire repository into context.

rag-mcp is a local hybrid-search MCP server: point it at a file or folder, ask a question, and get back ranked chunks with exact source paths. Embeddings, vector search, and reranking run locally; the server exposes one read-only MCP tool to compatible clients.

Quick start

git clone https://github.com/MasihMoafi/rag-mcp
cd rag-mcp
uv sync

Run the automated tests:

.venv/bin/python -m pytest tests/ -v

Then register the server with an MCP client.

Claude Code

claude mcp add rag -s user -- /absolute/path/to/rag-mcp/.venv/bin/python /absolute/path/to/rag-mcp/server.py

Codex / Elpis

Add to ~/.codex/config.toml:

[mcp_servers.rag]
command = "/absolute/path/to/rag-mcp/.venv/bin/python"
args = ["/absolute/path/to/rag-mcp/server.py"]

[mcp_servers.rag.env]
RAG_MCP_WORKSPACE_ROOT = "/absolute/path/to/your/project"

Expected result: the client discovers query_knowledge_base, and a query returns ranked passages with source paths from the requested scope.

The problem

Coding agents commonly retrieve context by either opening files one by one or loading a large portion of the repository. The first can miss relevant files; the second consumes context with material the current task may not need.

rag-mcp moves retrieval into one local tool call so the agent can search by meaning without making the entire tree part of every prompt.

How it works

query + optional path
        ↓
chunking
        ↓
BM25 lexical search + local embeddings / Qdrant
        ↓
Reciprocal Rank Fusion
        ↓
CrossEncoder reranking
        ↓
ranked chunks + exact source paths

Repository structure:

rag-mcp/
├── server.py       # stdio JSON-RPC MCP host
├── rag/            # chunking, BM25, vector search, reranking
└── utils/proxy.py  # local proxy-environment handling

Technical boundaries:

  • one MCP tool: query_knowledge_base(query, doc_path?);
  • default embeddings: all-MiniLM-L6-v2;
  • reranker: cross-encoder/ms-marco-MiniLM-L-6-v2;
  • local embedded/on-disk Qdrant;
  • doc_path can scope each call to a file or directory;
  • per-path indexes are persisted under rag/rag_db_v2/;
  • common large/build directories such as .git, node_modules, .venv, dist, build, and target are rejected;
  • configurable depth/token limits fail explicitly instead of scanning an unbounded tree.

Current state

Implemented and verified

  • MCP initializetools/listtools/call protocol path.
  • Read-only query_knowledge_base tool.
  • Workspace-root and explicit doc_path scoping.
  • Local hybrid retrieval and reranking.
  • Guardrails for excluded directories and oversized scopes.
  • End-to-end registration was exercised through a real MCP client during development.

Implemented but not yet covered by the current tests

  • The alternative Ollama embedding-provider path in rag/core.py.

Planned

Nothing is formally tracked yet. Extend it when a concrete retrieval failure or client requirement appears.

Intentionally unsupported

  • Hosted/remote vector databases.
  • File types outside the extension allowlist in server.py.
  • Write/mutation tools; this server is retrieval-only.

What sets this apart

These are design choices, not novelty claims:

  • Local retrieval: source files, embeddings, vector search, and reranking stay on the machine.
  • Small transport layer: the MCP host uses direct stdio JSON-RPC rather than depending on an MCP SDK.
  • Per-call scope: one server can search different files/directories instead of requiring one fixed knowledge base per project.
  • Evidence in the response: returned chunks include source paths rather than only synthesized prose.

Evals and test series

Five lightweight tests live under tests/:

uv sync --group dev
.venv/bin/python -m pytest tests/ -v

They cover:

  • read-only tool annotations;
  • default workspace scoping;
  • explicit doc_path scoping;
  • rejection of excluded directories;
  • rejection of depth-limit violations.

Protocol-level check, without another MCP client:

printf '%s\n%s\n%s\n' \
  '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-06-18"}}' \
  '{"jsonrpc":"2.0","id":2,"method":"tools/list"}' \
  '{"jsonrpc":"2.0","id":3,"method":"tools/call","params":{"name":"query_knowledge_base","arguments":{"query":"how does reciprocal rank fusion combine bm25 and vector results"}}}' \
  | RAG_MCP_WORKSPACE_ROOT="$PWD" .venv/bin/python server.py

A successful self-query should return evidence pointing at the RRF implementation in rag/core.py.

What the tests prove: MCP transport/scoping/guardrail behavior covered by those cases.

What they do not prove: retrieval quality across arbitrary corpora, cross-client compatibility, or superiority to grep/code-search/RAG alternatives.

Example

query_knowledge_base(
  "how does retry backoff work for failed jobs",
  doc_path="codex-rs/memories"
)

The response is intended for the calling agent: ranked source passages it can use as task context rather than a standalone chat answer.

Future development

Keep the surface small. Add capability only when real usage shows a retrieval, compatibility, or performance gap worth testing.

License

MIT — see LICENSE.

推荐服务器

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 模型以安全和受控的方式获取实时的网络信息。

官方
精选