# Code-Graph-RAG

> Parse a monorepo into a knowledge graph, then query and edit it in plain English

- Category: Retrieval & Knowledge (RAG) / Code Indexing & Search
- Language: Python
- Licence: MIT
- Repository: https://github.com/vitali87/code-graph-rag
- Website: https://code-graph-rag.com
- Canonical page: https://ai-tldr.dev/tools/code-graph-rag/

## In short

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 sits in the Code Indexing & Search category of the Retrieval & Knowledge (RAG) tools and is open-source, written in Python and licensed MIT.

## Overview

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. A monorepo of mixed languages ends up under one unified graph schema, so a question can cross language boundaries the way the code itself does.

Point the `cgr` CLI at a repository and it reads every source file, extracts functions, classes, methods and modules together with the relationships between them, and stores the result as an interconnected graph. Once the graph exists you can ask natural-language questions grounded in the real structure, retrieve the source of any symbol by name or by intent, find dead code by walking call and reference edges from entry points, and search or rewrite structurally by AST pattern with ast-grep.

It also edits. Changes go through AST-based surgical patching with a diff preview before anything is written, and the tool can optimise code against language best practices or your own coding standards. Beyond static structure, `cgr trace` overlays runtime behaviour — trace a test run, or pull production eBPF profiles — and merges the calls that actually happened into the graph, exposing dynamic dispatch that static analysis alone cannot see.

The project runs as an MCP server as well, so Claude Code and other MCP clients can query and edit your codebase through the same graph. Recent work added Neo4j as an alternative graph backend through a pluggable dialect seam, and a Gloss node for agent-authored notes that can be written and read over MCP.

## What it does

- Tree-sitter parsing of mixed-language monorepos into one unified Memgraph graph schema
- Natural-language questions answered against the real call and reference structure, not text similarity
- AST-based surgical editing with a diff preview before any file changes
- Dead-code detection by walking call and reference edges out from entry points
- Structural search and rewrite by AST pattern via ast-grep
- `cgr trace` merges real runtime calls (test runs or production eBPF profiles) into the graph
- MCP server so Claude Code and other MCP clients can query and edit through the graph
- Pluggable graph backend — Memgraph or Neo4j

## Getting started

`cgr` is published to PyPI. You need Python 3.12+, Docker (for Memgraph), cmake and ripgrep. Install with the treesitter-full extra for all languages and semantic for vector search.

1. Install the CLI

   uv is the recommended installer; pipx works too. Both give you the newest PyPI release.

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

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

2. Start the graph stack

   This brings up the packaged Memgraph and Qdrant services — you do not need to write a compose file.

```bash
cgr daemon up
```

3. Index a repository, then query it

   The first command parses the repo into the graph; the second opens querying against it. Repeat the parse for each repository you want indexed — the graph is shared and syncing one project leaves the others alone.

```bash
cgr start --repo-path /path/to/repo --update-graph
cgr start --repo-path /path/to/repo
```

4. Run ahead of the release cadence (optional)

   PyPI publishes every 50th version plus security fixes, so the newest git tag usually runs ahead of the newest release. Install from git to track main.

```bash
uv tool install "code-graph-rag[treesitter-full,semantic] @ git+https://github.com/vitali87/code-graph-rag@main"
```

## When to use it

- Answer 'who calls this and what breaks if I change it' across a monorepo that spans several languages
- Give a coding agent structural context over your codebase through the MCP server instead of raw file dumps
- Find genuinely unreachable code by walking the call graph from real entry points
- Catch dynamic dispatch that static analysis misses by merging a traced test run into the graph
- Apply a coding standard across a large codebase with AST patching and a diff preview per change
