code-rag-mcp

code-rag-mcp

Enables AI assistants to perform hybrid semantic and lexical code search across multiple repositories, retrieve symbol definitions and call hierarchies, and manage repository relations through MCP tools.

Category
访问服务器

README

⚡ Multi-Repository Code Search Engine

A production-grade code retrieval and search system engineered for querying and navigating multiple source code repositories simultaneously, designed and planned using the OpenSpec Spec-Driven Development framework. Ranked results (with repository, file, line, symbol, and graph metadata) are the integration boundary for external cloud LLM clients, which perform generation in their own environment.


🌟 Key Features

  1. Multi-Repository Ingestion & Incremental Sync:

    • Manages local codebase directories and remote Git repositories.
    • Respects .gitignore rules and excludes binaries/lockfiles automatically.
    • SHA-256 hash tracking and Git commit detection for instantaneous incremental updates.
  2. AST-Aware Semantic Code Chunking:

    • Language-aware structural parsing for Python, TypeScript/JavaScript, Go, Rust, Java, C/C++, HTML/CSS, SQL, and Markdown.
    • Preserves function, method, class, and interface boundaries.
    • Injects scope headers (// [Context] Repository | File | Scope | Imports | Doc).
  3. Hybrid Dense + Lexical Indexing:

    • Dense Vector Search: Semantic subword feature vectors with cosine similarity + support for external embeddings (Gemini, OpenAI, Voyage AI, Ollama). Local Ollama embeddings default to qwen3-embedding:0.6b, loaded on demand and released when idle.
    • Sparse BM25 Search: Code-tailored tokenizer splitting camelCase and snake_case tokens with symbol boosting.
    • Reciprocal Rank Fusion (RRF): Merges dense and sparse rankings with exact identifier boosts.
  4. Symbol Graph & Cross-Repository Dependency Linkage:

    • Extracts symbol definitions, callers, callees, and imports in SQLite.
    • Automatically maps frontend client API calls (e.g. apiClient.post('/api/v1/auth/login')) to backend API route handlers across different repositories.
  5. Interfaces:

    • Modern Web UI: Hybrid Search as the primary query experience, repository manager, cross-repo API contract map, and code inspector drawer.
    • Model Context Protocol (MCP) Server: Exposes stdio tools (search_codebases, get_symbol_definition, get_call_hierarchy, list_repositories) to AI coding assistants (Antigravity, Cursor, Claude Code, Windsurf).
    • CLI: Fast terminal commands for indexing and searching.
    • REST API: POST /api/v1/search returns ranked code chunks for external cloud LLM consumers.

📂 OpenSpec Spec-Driven Planning

All specifications, architectural contracts, and task breakdowns are maintained under openspec/:

openspec/
├── config.json                     # OpenSpec project configuration
├── specs/                          # Living System Specifications (Source of Truth)
│   ├── repository-management.md    # Repo ingestion & git tracking
│   ├── ast-code-chunking.md        # AST semantic parsing & context injection
│   ├── hybrid-indexing.md          # Dense vector + BM25 lexical index
│   ├── symbol-graph-retrieval.md   # Call graph & cross-repo API linkage
│   ├── context-fusion-reranking.md # RRF fusion & citation packaging
│   ├── rag-generation.md           # LLM prompting & grounded citations
│   ├── mcp-server.md               # Model Context Protocol tools
│   └── api-and-web-ui.md           # REST & Web UI specifications
└── changes/
    └── 01-foundation-and-core-rag/ # Phase 1 Change Proposal
        ├── proposal.md             # Goals, scope, and motivation
        ├── design.md               # Technical architecture & contracts
        └── tasks.md                # Implementation checklist (Completed)

🚀 Quick Start

1. Register & Index Repositories

# Add a local repository
python3 main.py add auth-service ./fixtures/repo_auth_service

# Add another repository
python3 main.py add web-client ./fixtures/repo_web_client

# List all indexed repositories
python3 main.py list

2. Manage Repository Groups & Dependency Relations

# Create a repository group
python3 main.py group create platform --repos auth-service shared-schemas

# Declare a dependency edge: web-client depends on auth-service
python3 main.py relation add web-client auth-service

# Inspect relations for a repository
python3 main.py relation show web-client

# Search with group scoping and upstream dependency expansion
python3 main.py search "jwt token" --group platform --expand upstream --expand-depth 1

3. Search Across Repositories (CLI)

# Hybrid search across all codebases
python3 main.py search "login user authenticate"

# Search scoped to a group with upstream dependency expansion
python3 main.py search "How does authentication flow between web-client and auth-service?" --group platform --expand upstream

4. Launch the Interactive Web UI

python3 main.py serve --host 127.0.0.1 --port 8000

Open http://localhost:8000 in your browser.

5. Connect to AI IDEs via MCP (Model Context Protocol)

Add this MCP server entry to your AI IDE configuration (Antigravity / Cursor / Claude Code):

{
  "mcpServers": {
    "multi-repo-code-rag": {
      "command": "python3",
      "args": ["/Users/nick-work-pc/.gemini/antigravity/scratch/multi-repo-code-rag/main.py", "mcp"]
    }
  }
}

🧠 Embedding Model Runtime

The engine runs as a single instance per data directory and keeps the local embedding model resident only while it is working.

  • Default model: qwen3-embedding:0.6b (install once with ollama pull qwen3-embedding:0.6b). Override with --embedding-model or $OLLAMA_EMBEDDING_MODEL.

  • On-demand residency: the model is never loaded at startup. It loads on the first embedding of an indexing run or search, and is released once the last in-flight operation finishes and the idle grace elapses. Overlapping requests share one load and produce one release.

  • Residency policy via --keep-alive or $EMBEDDING_KEEP_ALIVE:

    Value Behavior
    (unset) Release after 30s of inactivity (default)
    0 Release immediately after the last operation
    45s, 5m Release after that idle grace
    always Keep the model resident for the process lifetime
  • Single instance: startup takes an exclusive lock on <data-dir>/.rag-instance.lock. A second instance fails fast with the owning pid; pass --allow-multi-instance to downgrade this to a warning.

  • Inspect / release manually: GET /api/v1/models/status reports residency, active operations, policy, and index provenance. POST /api/v1/models/unload (or python3 main.py unload) releases the model, returning 409 busy while an operation is in flight.

Automatic reindex on model change

The dense index records the provider, model, and vector dimension that produced its vectors (<data-dir>/index_meta.json). When the configured embedding model changes — for example on upgrade from qwen3-embedding:4b (2560 dims) to the qwen3-embedding:0.6b default (1024 dims) — the affected repositories are automatically re-embedded before search results are served:

  • chunk text, symbol graph, and BM25 lexical index are preserved (embedding-only pass, not a re-parse);
  • progress is reported through the normal indexing progress output;
  • provenance is written per repository, so an interrupted rebuild resumes with the repositories still outstanding;
  • searches arriving during a rebuild get 503 reindexing instead of being scored against vectors from another model.

Rollback to the previous behavior: OLLAMA_EMBEDDING_MODEL=qwen3-embedding:4b EMBEDDING_KEEP_ALIVE=always restores the old model and always-resident policy; the provenance check then rebuilds back into the 4b vector space with no code change.


🏷️ Repository Groups & Dependency Relations Architecture

Topology & Domain Rules

  • Named Repository Groups: Flat collections of repositories (e.g. core, platform, billing). Deleting a group never deletes underlying repositories.
  • Directed Dependency DAG: Explicit dependency edges A -> depends on -> B. Adding an edge runs write-time cycle detection (raising DependencyCycleError on cycles).
  • Scope Resolution: Combines explicit repository IDs and group members into a primary set, then expands along the graph in upstream (dependencies), downstream (dependents), or both directions up to expand_depth.
  • Hop-Decay Ranking: Chunks retrieved from expanded repositories receive a score multiplier penalty (0.85 ** hops) to ensure primary repositories rank first.
  • Provenance Metadata: Results originating from expanded repositories carry metadata (repo_relation='expanded', relation_direction, relation_hops) and are visually badged in the UI.

REST API Endpoints

Method Endpoint Description
GET /api/v1/groups List all repository groups and their members
POST /api/v1/groups Create a new repository group {"name": "...", "repo_ids": [...]}
DELETE /api/v1/groups/{name} Delete a repository group
POST /api/v1/groups/{name}/members Add members to group {"repo_ids": [...]}
DELETE /api/v1/groups/{name}/members/{repo_id} Remove a member from a group
GET /api/v1/models/status Embedding model residency, policy, and dense index provenance
POST /api/v1/models/unload Release models now (409 while an operation is in flight)
GET /api/v1/repos/{repo_id}/relations Get repository groups, direct dependencies, and direct dependents
POST /api/v1/repos/{repo_id}/dependencies Add dependency edge {"depends_on": "..."}
DELETE /api/v1/repos/{repo_id}/dependencies/{target_id} Remove a dependency edge
POST /api/v1/search Search with optional groups, expand, and expand_depth

MCP Tools

  • manage_repository_relations: Actions create_group, delete_group, add_to_group, remove_from_group, add_dependency, remove_dependency.
  • get_repository_relations: Returns relations for a single repository or the entire relation graph.
  • search_codebases: Extended with optional groups, expand, and expand_depth arguments.

🧪 Running Tests

python3 -m unittest discover -s tests -p "test_*.py" -v

All unit and integration test suites pass verifying AST chunking, symbol extraction, cross-repo API detection, repository relation DAG & cycle detection, scope resolution & hop-decay retrieval, REST API handlers, MCP protocol, and end-to-end hybrid search retrieval.

推荐服务器

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

官方
精选