首页 / 文章 / CodeGraph: Give Your AI Coding Agent a Knowledge Graph of Any Folder

本文以英文发布。

CodeGraph: Give Your AI Coding Agent a Knowledge Graph of Any Folder

How CodeGraph indexes a codebase with Tree-sitter and SQLite, serves it over MCP and cuts agent tool calls by 70% and token costs by nearly half, all locally.

849 词

Start a fresh session with an AI coding assistant such as Claude Code or Cursor and you'll see the same routine every time. Ask about a feature and the agent begins looping through ls, grep, read_file, and again. It calls this exploring, and you pay for every token it burns while hunting for the place where your authentication logic lives.

That discovery phase is a hidden tax on agent-driven development. A model with expert-level reasoning ends up behaving like a librarian who flips through every book in the building just to locate the index. CodeGraph changes the arithmetic. It serves a pre-built structural map of your codebase over a Model Context Protocol (MCP) server, so the agent can skip the search and go straight to the answer. On average that cuts tool calls by more than 70% and token costs by close to half.

What's under the hood

CodeGraph is built for fast code intelligence, and three pieces make it work.

Tree-sitter parsing

At the center is a Tree-sitter parsing pipeline. Search tools built on regular expressions match text patterns, but CodeGraph runs a real grammar-based parser, so it understands the building blocks of your code, such as functions, classes and their relationships, with much greater precision.

An MCP bridge

CodeGraph talks to your agent through MCP, which standardizes the exchange between agent and graph using JSON-RPC.

A SQLite graph schema

Everything extracted, symbols and the relationships between them, goes into an optimized SQLite graph schema built for quick graph queries, and it all stays on your own machine.

Two advantages beyond speed

Denser context, fewer tokens

Agents are often swamped by context bloat: large, messy files fed to the model just so it can find one function. CodeGraph supplies high-density context instead. It pulls out only the relevant symbols and their relationships, so the model gets the most information in the fewest tokens.

Your code stays local

With so much AI running in the cloud, privacy deserves attention. CodeGraph works inside a local privacy boundary. Your source and the resulting SQLite graph never leave your computer, and the agent queries a local server. Your intellectual property isn't exposed to outside indexing or training.

How it works

The efficiency comes from a four-stage internal pipeline that turns raw source files into a high-performance knowledge graph. In short, the parser reads the code, the symbols and relationships land in the local SQLite store and the MCP server answers the agent's queries from it.

Setup

Turning a folder into a knowledge graph is quick, local and needs few steps.

Install

CodeGraph is a lightweight command-line tool. Install it globally with npm:

npm install -g @colbymchenry/codegraph

Index the project

From your project folder, run the init command. CodeGraph scans the files, parses their structure with Tree-sitter and builds the local SQLite graph.

codegraph init -i

Connect the agent

Once indexing finishes, link your coding assistant, whether Claude Code, Cursor or another, through the MCP bridge. From then on the agent queries the graph directly and skips the costly exploration.

codegraph install

Developer, agent and graph working together

The real payoff comes from a three-way workflow. You supply the intent, the agent does the reasoning and CodeGraph provides the structural map. Because the loop is circular, each solution is anchored in what the codebase actually contains.

Where it sits among similar tools

CodeGraph is strong at structural mapping, but it's one tool in a wider set built to make coding agents smarter. Graphify extracts semantic meaning from a codebase, and lat.md helps manage intent. CodeGraph supplies the foundational structural layer underneath both, so the three complement each other.

The full flow

The whole workflow is local, private and wired into the coding assistants you already use. By mapping symbols and how they connect, CodeGraph gives the agent a real architectural understanding. A dedicated set of MCP commands lets it navigate complex codebases and run blast-radius analysis, which reveals which parts depend on a piece of code before a change turns into a problem.

The bottom line

Agents that spend time and money exploring a codebase get expensive fast. CodeGraph hands them a precise, pre-indexed map, so they stop acting like librarians and start solving problems. Think of it as one layer of an agentic stack: structure from CodeGraph, semantic meaning from Graphify, intent from lat.md. The principle is simple. The costliest thing an agent can do is look around, and a map lets it just look.