Repo-Aware Code RAG MCP Server

Repo-Aware Code RAG MCP Server

Provides MCP tools for searching a codebase with exact file:line citations, reading specific source spans, and inspecting index stats, enabling agents to retrieve and cite code accurately.

Category
访问服务器

README

Repo-Aware Code RAG Assistant

ci

Note: This repository is the public demonstration version of a private, live system under continuous development. It implements the same architecture end-to-end — AST-aware chunking, hybrid retrieval, the FastAPI service, the MCP server, and the evaluation harness — in a fully local, zero-cloud-account mode. The production deployment (Azure AI Search, Databricks ingestion jobs, MLflow tracking) is private; its integration seams are visible here as thin, documented adapters.

A retrieval-augmented assistant for codebases that answers "where is X and how does it work?" with exact file:line citations — built on the observation that code retrieval fails differently than prose retrieval, and needs code-specific treatment at every stage.

                       ┌──────────────────────────────────────────────┐
   repo on disk ──────▶│  AST-aware chunker (chunking.py)             │
                       │  functions/classes with exact line spans;    │
                       │  windows only as fallback                    │
                       └──────────────┬───────────────────────────────┘
                                      ▼
                       ┌──────────────────────────────────────────────┐
                       │  Index (index.py)                            │
                       │  BM25 w/ identifier field-boost  +  dense    │
                       │  local: numpy+rank_bm25 · prod: Azure AI Search │
                       └──────────────┬───────────────────────────────┘
                                      ▼
                       ┌──────────────────────────────────────────────┐
                       │  Retrieval (retrieval.py)                    │
                       │  query expansion → RRF fusion → rank priors  │
                       │  → optional cross-encoder rerank             │
                       └──────┬───────────────────────┬───────────────┘
                              ▼                       ▼
                   FastAPI service (api.py)   MCP server (mcp_server.py)
                   /ingest /search /ask       search_code · read_span
                                              — retrieval as a live tool

Quickstart (fully local, no keys)

pip install -e .
python -m repo_rag.ingest .        # index this repo on itself
repo-rag-api                       # http://localhost:8000/docs

Real output of the retriever, dogfooding on this repository:

$ search: 'where is reciprocal rank fusion implemented'
  src/repo_rag/retrieval.py:64-73  (function reciprocal_rank_fusion, via bm25+dense)
  src/repo_rag/eval.py:45-68  (function evaluate, via bm25+dense)
  src/repo_rag/retrieval.py:109-125  (function Retriever._prior, via bm25+dense)

$ search: 'which MCP tool reads exact source lines for a citation'
  src/repo_rag/mcp_server.py:72-82  (function read_span, via bm25+dense)
  src/repo_rag/mcp_server.py:1-16  (module mcp_server docstring, via bm25+dense)
  src/repo_rag/chunking.py:83-98  (function chunk_repo, via bm25+dense)

Ask over HTTP:

curl -s localhost:8000/search -X POST -H 'content-type: application/json' \
  -d '{"query": "how are python files split into chunks", "k": 3}' | jq .

/ask returns a synthesized answer: extractive with citations by default, or LLM-written (still citation-constrained) when ANTHROPIC_API_KEY is set.

MCP: retrieval as a live tool, not a hardcoded pipeline

Instead of one fixed ask→retrieve→generate chain, the index is exposed over the Model Context Protocol, so any MCP client — Claude Desktop, Claude Code, IDE agents — decides when and how to query it, iteratively, mid-conversation.

pip install ".[mcp]"

Claude Desktop config (claude_desktop_config.json):

{
  "mcpServers": {
    "repo-rag": {
      "command": "repo-rag-mcp",
      "env": { "REPO_RAG_INDEX_DIR": "/absolute/path/to/.ragindex" }
    }
  }
}

Tools exposed: search_code(query, k), read_span(path, start, end), index_stats().

Evaluation

The harness measures recall@k and MRR against gold file:line spans (hit = line-overlap in the same file), and logs to MLflow when MLFLOW_TRACKING_URI is set. A RAGAS faithfulness pass over generated answers is available behind --ragas (needs the [eval] extra and an LLM key).

repo-rag-eval eval/sample_eval.jsonl --k 5
# recall_at_5: 0.8333
# mrr: 0.6167
# n_questions: 6

(The one miss is instructive: this README quotes the demo queries, so once indexed it outcompetes the implementation for one question - the same docs-vs-code tension the rank priors exist to manage.)

Those numbers are the illustrative sample set in this repo (6 self-referential questions, offline hash embedder, no rerank). The headline metrics for this project — recall@5 lifted 61% → 84% (0.79 MRR) with RAGAS faithfulness 0.82 — were measured on the full evaluation set of the private live deployment over a ~2K-file codebase, using the same harness with production embeddings and reranking enabled. The methodology here is the methodology there; run it on your own repo with your own eval set.

Ablations are one flag away: --no-expand disables query expansion; REPO_RAG_RERANK=true enables the cross-encoder (with the [ml] extra).

Design notes (the interesting 20%)

  • AST chunking over fixed windows (chunking.py) — windows slice through function bodies, poisoning embeddings and producing citations that start mid-def. AST boundaries keep units intact, give every chunk a name, and make path:start-end land exactly where a developer would open the file. Oversized definitions window internally, carrying the parent's name.
  • RRF over weighted score sums (retrieval.py) — BM25 scores and cosine similarities live on incomparable scales; rank fusion is scale-free and needs no corpus-specific tuning.
  • Identifier field-boosting in BM25 (index.py) — in code search, a chunk whose name matches the query nearly always beats prose that merely mentions the concept. Implemented as token repetition inside BM25 - together with the rank priors, the single biggest sample-eval lift.
  • Weak rank priors — implementations over tests, definitions over module prose. Deliberately weak multipliers, so strong matches in tests still surface.
  • Hermetic by default — the deterministic hash embedder keeps tests, CI, and the demo fully offline; sbert/azure backends are a config switch.

Production architecture (private deployment)

  • Azure AI Search serves hybrid BM25 + vector ranking over the same chunk schema (index.AzureAISearchIndex is the adapter seam).
  • Databricks runs repo_rag.ingest as a scheduled job against repository checkouts, feeding the indexer.
  • MLflow tracks every eval run across chunking/retrieval configurations.
  • Docker → Azure: docker build -t repo-rag . && az containerapp up --name repo-rag --source .

Roadmap

  • [x] AST-aware chunking with exact line spans
  • [x] Hybrid BM25 + dense retrieval with RRF
  • [x] Query expansion; optional cross-encoder rerank
  • [x] FastAPI service with citation-formatted responses
  • [x] MCP server (stdio): search_code, read_span, index_stats
  • [x] Eval harness: recall@k, MRR, MLflow logging, RAGAS hook
  • [ ] Incremental re-indexing on file change (watch mode)
  • [ ] Tree-sitter chunking for TypeScript/Go/Java (beyond line windows)
  • [ ] MCP resources: expose indexed files as browsable resources
  • [ ] Eval dashboard comparing configs across runs

License

MIT © 2026 Manav Kanaganapalli

推荐服务器

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

官方
精选