开源项目 GitHub Trending 2026-08-09T12:00 正文 ok 722 words

Code-Graph-RAG:面向 monorepo 的多语言代码知识图谱 RAG

英文标题:🔥 vitali87/code-graph-rag

Code-Graph-RAG 使用 Tree-sitter 解析多语言代码库,并将函数、类、模块及其关系写入 Memgraph 知识图谱,使开发者能够以自然语言查询、理解、编辑和优化 monorepo。项目现已完整支持 Python、TypeScript、JavaScript、Rust、Go、Java、C/C++、C#、PHP、Lua 和 Dart,并通过可插拔的 ast-grep 层为 Ruby 提供结构化支持。其新增能力包括基于 AST 的结构化搜索与替换,以及覆盖 C#、Java、C 和 Go 的 `FLOWS_TO` 污点数据流追踪。该工具可作为 MCP 服务器接入 Claude Code 等客户端,并提供本地开源版本、托管云部署和本地/隔离网络部署选项。
打开原文相关 3 · 机会 3
media128.0 KB media1.3 KB media2.2 KB media5.9 KB media1.4 KB media1.3 KB media1.2 KB media1.0 KB media1.3 KB media1.2 KB media2.2 MB media107.1 KB

中文

Code-Graph-RAG 使用 Tree-sitter 解析多语言代码库,在 Memgraph 中构建其结构知识图谱,并允许你使用自然语言查询、编辑和优化代码。它可在统一的图谱 schema 下处理包含多种语言的 monorepo。

演示

  • Ruby 支持:Ruby 通过新的可插拔 ast-grep 层加入图谱。该层仅需一个 YAML 模式文件即可添加一门语言,无需手写解析器,并可生成 Module、Function 和 Class 节点及导入边。
  • 结构化搜索与替换:通过 ast-grep 按 AST 模式查找和重写代码,并将其作为 Agent 工具提供。因此,你可以在整个代码库中匹配和转换代码结构,而不是依赖纯文本或正则表达式。
  • 数据流追踪:新增的 FLOWS_TO 污点边可通过赋值、函数调用和 I/O 汇点追踪值的流向,目前覆盖 C#、Java、C 和 Go。

有关完整历史记录,请参阅 NEWS.md。

将 Code-Graph-RAG 指向一个仓库后,它会读取每一个源文件,提取函数、类、方法、模块及其相互关系,并将结果存储为互联图谱。图谱构建完成后,你可以:

  • 使用自然语言询问代码库相关问题,并获得基于真实代码结构的回答。
  • 按名称或意图获取任意函数、类或方法的实际源代码。
  • 通过 Agent 使用基于 AST 的精确补丁编辑代码,并在任何变更生效前预览 diff。
  • 根据语言最佳实践或你自己的编码标准优化代码。
  • 从入口点沿调用边和引用边遍历,以查找死代码。
  • 使用 ast-grep 按 AST 模式执行结构化搜索和重写。

系统由两个组件构成:

  1. 多语言解析器。 基于 Tree-sitter 的解析器读取代码库,并在统一、与语言无关的 schema 下,将函数、类、方法、模块及其关系导入 Memgraph。

  2. RAG 系统(codebase_rag/)。一个交互式 CLI,可将自然语言转换为 Cypher 查询,检索匹配代码,并驱动由 AI 支持的编辑与优化。

    Source Code -> Tree-sitter Parser -> AST Analysis -> Memgraph Knowledge Graph | User Query -> AI Model (Cypher Gen) -> Cypher Query -> Graph Results -> Response

完整架构请参阅架构概览和图谱 Schema。

Python、TypeScript、TSX、JavaScript、Rust、Go、Java、C、C++、C#、PHP、Lua 和 Dart 已获完整支持。Scala 正在开发中;Ruby 则通过可插拔 ast-grep 层获得结构化支持,包括模块、函数、类和导入。各语言的能力详情请参阅语言支持矩阵。

cgr 已发布至 PyPI。可通过 treesitter-full(所有语言)和 semantic(向量搜索)extras 在系统范围内安装:

# with uv (recommended)
uv tool install "code-graph-rag[treesitter-full,semantic]"

# or with pipx
pipx install "code-graph-rag[treesitter-full,semantic]"

你还需要 Docker(用于 Memgraph)、cmake 和 ripgrep。完整的前置条件、从源码安装和环境配置请参阅安装指南。

# Start the packaged Memgraph + Qdrant stack (no compose file needed)
cgr daemon up

# Parse a repository into the graph, then query it
cgr start --repo-path /path/to/repo --update-graph
cgr start --repo-path /path/to/repo

对每个希望建立索引的仓库,都重复执行带有 --update-graph 的命令;图谱为共享图谱,同步一个项目不会影响其他项目。若要从空图谱重新开始,请添加 --clean——它会删除共享图谱中的所有项目,而不仅是当前项目;如果可能销毁其他项目,系统会先请求确认。

快速开始指南可带你在五分钟内完成解析、查询和导出。

Code-Graph-RAG 可作为 MCP 服务器运行,因此 Claude Code 和其他 MCP 客户端可以直接查询和编辑你的代码库。配置方式请参阅 MCP Server 指南。

Code-Graph-RAG 为开源项目,可免费使用。对于有更高需求的组织,我们提供全托管云端解决方案和本地部署:

  • 云托管部署:为图数据库和 AI Agent 连接提供托管云基础设施。无需承担基础设施运维成本;我们负责扩缩容、更新和可用性,你的团队则可以专注于构建产品。
  • 本地部署与隔离网络部署:可将 Code-Graph-RAG 完全部署在你自己的环境中,包括隔离网络。为受监管行业和安全敏感型组织提供完整的数据主权。

我们还提供定制开发、集成咨询、技术支持合同和团队培训。

在 code-graph-rag.com 查看套餐与定价

贡献指南请参阅 CONTRIBUTING.md。适合首次提交的 PR 可从 TODO issues 开始。

如有问题或疑问,请先查看故障排除指南,然后再创建 issue。

MIT。请参阅 LICENSE。

English / 原文

Code-Graph-RAG parses a multi-language codebase with Tree-sitter, builds a knowledge graph of its structure in Memgraph, and lets you query, edit, and optimise that code in plain English. It works across a monorepo of mixed languages under one unified graph schema.

demo

  • Ruby Support : Ruby joins the graph through a new pluggable ast-grep tier that adds a language from a single YAML pattern file, emitting Module, Function, and Class nodes plus import edges without a hand-written parser.
  • Structural Search & Replace: Find and rewrite code by AST pattern with ast-grep, exposed as agent tools so you can match and transform structure across the whole codebase instead of relying on text or regex.
  • Data-Flow Tracing : New FLOWS_TO taint edges follow values through assignments, function calls, and I/O sinks, with coverage across C#, Java, C, and Go.

See NEWS.md for the full history.

Point Code-Graph-RAG at a repository and it reads every source file, extracts functions, classes, methods, modules, and the relationships between them, and stores the result as an interconnected graph. Once the graph exists you can:

  • Ask questions about the codebase in natural language and get answers grounded in the real structure.
  • Retrieve the actual source of any function, class, or method by name or by intent.
  • Edit code through the agent with AST-based surgical patching and a diff preview before anything changes.
  • Optimise code against language best practices or your own coding standards.
  • Find dead code by walking call and reference edges from entry points.
  • Search and rewrite structurally by AST pattern with ast-grep.

The system has two components:

  1. Multi-language parser. A Tree-sitter based parser reads the codebase and ingests functions, classes, methods, modules, and their relationships into Memgraph under a single language-agnostic schema.
  2. RAG system (codebase_rag/). An interactive CLI that turns natural language into Cypher queries, retrieves matching code, and drives AI-powered editing and optimisation.
Source Code -> Tree-sitter Parser -> AST Analysis -> Memgraph Knowledge Graph
                                                             |
User Query -> AI Model (Cypher Gen) -> Cypher Query -> Graph Results -> Response

See the Architecture Overview and Graph Schema for the full picture.

Python, TypeScript, TSX, JavaScript, Rust, Go, Java, C, C++, C#, PHP, Lua, and Dart are fully supported. Scala is in development, and Ruby has structural support (modules, functions, classes, and imports) through the pluggable ast-grep tier. See the Language Support matrix for per-language capabilities.

cgr is published to PyPI. Install it system-wide with the treesitter-full (all languages) and semantic (vector search) extras:

# with uv (recommended)
uv tool install "code-graph-rag[treesitter-full,semantic]"

# or with pipx
pipx install "code-graph-rag[treesitter-full,semantic]"

You also need Docker (for Memgraph), cmake, and ripgrep. Full prerequisites, source installs, and environment setup are in the Installation guide.

# Start the packaged Memgraph + Qdrant stack (no compose file needed)
cgr daemon up

# Parse a repository into the graph, then query it
cgr start --repo-path /path/to/repo --update-graph
cgr start --repo-path /path/to/repo

Repeat the first command for each repository you want indexed; the graph is shared, and syncing one project leaves the others alone. To start over from an empty graph, add --clean — it deletes every project in the shared graph, not just this one, and asks for confirmation first when other projects would be destroyed.

The Quick Start guide walks through parsing, querying, and exporting in five minutes.

Code-Graph-RAG runs as an MCP server so Claude Code and other MCP clients can query and edit your codebase directly. See the MCP Server guide for setup.

Getting Started

User Guide

Architecture

Python SDK

Advanced

Code-Graph-RAG is open source and free to use. For organisations that need more, we offer fully managed cloud-hosted solutions and on-premise deployments :

  • Cloud-Hosted Deployment : Managed cloud infrastructure for both the graph database and the AI agent connection. Zero infrastructure overhead, so we handle scaling, updates, and availability while your team focuses on building.
  • On-Premise & Air-Gapped Deployment: Deploy Code-Graph-RAG entirely within your own environment, including air-gapped networks. Full data sovereignty for regulated industries and security-sensitive organisations.

We also offer custom development, integration consulting, technical support contracts, and team training.

View plans& pricing at code-graph-rag.com

Please see CONTRIBUTING.md for contribution guidelines. Good first PRs come from the TODO issues.

For issues or questions, check the Troubleshooting guide first, then open an issue.

MIT. See LICENSE.