What Is Code-Graph-RAG? A Multi-Language Code Knowledge Graph Framework Explained

Code-Graph-RAG is an open-source Python framework that parses entire code repositories into a unified, language-agnostic knowledge graph stored in Memgraph, enabling natural-language queries, semantic search, and AI-powered code analysis.

Code-Graph-RAG transforms static source code into an interactive, queryable knowledge base. Built around Tree-sitter parsers and a Memgraph backend, it extracts functions, classes, imports, and data-flow relationships from Python, TypeScript, Rust, Go, and 10+ other languages—then exposes everything through a conversational CLI or programmatic Python SDK.

Core Architecture: Parsing to Retrieval

The system operates across two distinct layers that bridge raw source code and natural-language interaction.

Layer 1: Tree-Sitter Parsing and Graph Construction

Every file in a target repository passes through Tree-sitter grammars to extract structural and semantic entities. The parser identifies:

  • Nodes: Functions, classes, modules, variables, and type definitions
  • Relationships: CALLS, IMPORTS, INHERITS, and FLOWS_TO edges

This extracted data serializes to JSON, then loads into Memgraph through the GraphLoader class in [codebase_rag/graph_loader.py](https://github.com/vitali87/code-graph-rag/blob/main/codebase_rag/graph_loader.py). The loader builds in-memory indexes for fast node and relationship lookups without repeated database round-trips.

The schema is strictly unified across all supported languages. A Function node in Python carries identical property keys to a Function node in Rust—enabling cross-language queries and tooling reuse.

Layer 2: RAG Interface and Natural-Language Queries

The interactive layer converts user questions into Cypher queries, executes them against Memgraph, and packages results for a language model. Key components include:

The complete data flow follows this path:

Source Code → Tree-sitter Parser → AST Analysis → Memgraph Knowledge Graph
                │
User Query → AI Model (Cypher generation) → Cypher → Graph Results → Response

Multi-Language Support and Graph Schema

Code-Graph-RAG supports production-ready grammars for Python, TypeScript/TSX, JavaScript, Rust, Go, Java, C, C++, C#, PHP, Lua, and Dart, with experimental support for Scala and Ruby.

The unified schema is documented in [docs/architecture/graph-schema.md](https://github.com/vitali87/code-graph-rag/blob/main/docs/architecture/graph-schema.md). Every node follows this structure:

  • label: Entity type (Function, Class, Module, Variable, etc.)
  • properties: Name, filepath, line numbers, docstrings, and language-specific metadata

Relationships encode both structural dependencies (call graphs, inheritance) and data-flow edges that track how values propagate through the codebase.

Quick Start: CLI Commands

Start the Backend Services

cgr daemon up

This launches the bundled Memgraph and optional Qdrant (for semantic search) containers.

Parse a Repository

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

The --update-graph flag triggers a fresh Tree-sitter parse and JSON serialization before loading into Memgraph.

Interactive Query Mode

cgr start --repo-path /path/to/repo

Drop into a REPL where natural-language questions resolve to Cypher executions:


> Which functions in utils.py call the database?

The system returns matching function definitions with source locations and relationship context.

Programmatic Python SDK

For custom pipelines, import the core modules directly:

from codebase_rag.graph_loader import load_graph
from codebase_rag.cypher_queries import find_functions_by_name

loader = load_graph("graph.json")
cypher = find_functions_by_name("calculate_total")
results = loader.graph.run(cypher)

The GraphLoader class provides these inspection methods:

  • loader.nodes() — iterate all graph nodes
  • loader.relationships() — iterate all edges
  • loader.summary() — return node counts by label

Export and Analyze Graph Data

from codebase_rag.graph_loader import load_graph

loader = load_graph("graph.json")
summary = loader.summary()
print(summary)

Optional Features: Semantic Search and MCP Agent Integration

Semantic Search with Qdrant

Enable vector search by installing with the semantic extra:

pip install code-graph-rag[semantic]

The embedder in [codebase_rag/embedder.py](https://github.com/vitali87/code-graph-rag/blob/main/codebase_rag/embedder.py) caches embeddings for code snippets, enabling similarity search alongside structured Cypher queries.

MCP Server for External Agents

Code-Graph-RAG exposes a Model Context Protocol (MCP) server for integration with agentic tools like Claude Code. The server runs as:

cgr daemon --mode mcp

Documentation in [docs/guide/mcp-server.md](https://github.com/vitali87/code-graph-rag/blob/main/docs/guide/mcp-server.md) covers authentication, available tools, and message formats.

Key Implementation Files

Path Purpose
[codebase_rag/graph_loader.py](https://github.com/vitali87/code-graph-rag/blob/main/codebase_rag/graph_loader.py) JSON graph loading, index construction, lookup APIs
[codebase_rag/cli.py](https://github.com/vitali87/code-graph-rag/blob/main/codebase_rag/cli.py) Command-line interface and REPL
[codebase_rag/cgr_state.py](https://github.com/vitali87/code-graph-rag/blob/main/codebase_rag/cgr_state.py) Runtime state, AI model interaction
[codebase_rag/embedder.py](https://github.com/vitali87/code-graph-rag/blob/main/codebase_rag/embedder.py) Vector embedding generation and caching
[docs/architecture/graph-schema.md](https://github.com/vitali87/code-graph-rag/blob/main/docs/architecture/graph-schema.md) Unified schema specification
[pyproject.toml](https://github.com/vitali87/code-graph-rag/blob/main/pyproject.toml) Dependencies including treesitter-full and semantic extras

Summary

  • Code-Graph-RAG converts multi-language repositories into a queryable Memgraph knowledge graph using Tree-sitter parsing
  • A unified schema enables cross-language analysis regardless of source language
  • The CLI (cgr start) and Python SDK (GraphLoader, cypher_queries) provide flexible access patterns
  • Optional semantic search through Qdrant embeddings enhances keyword-based Cypher retrieval
  • MCP server mode allows external AI agents to query and modify the graph programmatically

Frequently Asked Questions

What databases does Code-Graph-RAG require?

Code-Graph-RAG uses Memgraph as its primary graph database for Cypher queries. Optional Qdrant vector storage enables semantic search when installed with the semantic extra. Both services launch together via cgr daemon up.

How does Code-Graph-RAG handle multiple programming languages?

The framework leverages Tree-sitter grammars to parse each language into an abstract syntax tree, then normalizes extracted entities into a single schema. A Function node has identical properties whether parsed from Python, Rust, or TypeScript—enabling unified queries across polyglot codebases.

Can I use Code-Graph-RAG without the CLI?

Yes. The Python SDK exposes core functionality through imports like codebase_rag.graph_loader and codebase_rag.cypher_queries. You can load parsed graphs, execute custom Cypher, and build entirely custom interfaces without touching the CLI.

What is the MCP server used for?

The MCP (Model Context Protocol) server exposes Code-Graph-RAG functionality to external AI agents that speak the MCP protocol—such as Claude Code or custom agent frameworks. Agents can query the graph, receive context about code relationships, and request structural modifications through standardized tool calls.

Have a question about this repo?

These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:

Share the following with your agent to get started:
curl -s "https://instagit.com/install.md"

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client

Maintain an open-source project? Get it listed too →