Contents8 sections
CodeGraph parses a codebase into a queryable local knowledge graph. The graph records files, functions, classes, calls, imports, inheritance, and related structures. AI coding agents can query relevant source, call paths, and change impact directly instead of rebuilding the architecture through repeated grep and file reads.
This guide covers colbymchenry/codegraph. By the end, you can install the CLI, connect Codex or another supported agent, index a project, and use explore, impact, and affected for code understanding, change assessment, and test selection.
01. How CodeGraph Works
CodeGraph produces a local code knowledge graph. It parses source with Tree-sitter, extracts symbols and relationships, and writes them to a SQLite database inside the project. The default directory is .codegraph/.
Agents query this graph through MCP. One codegraph_explore call can return line-numbered source for relevant symbols, call paths between those symbols, and the code that depends on them. It suits structural questions such as “How does a login request reach the database?” and “What could break if this interface changes?”
Source files
|
v
Tree-sitter extraction
|
v
.codegraph/codegraph.db
|
v
MCP / CLI query
|
v
Source + call paths + blast radiusCode parsing and graph queries stay on the local machine. The project also offers optional anonymous usage telemetry. That telemetry is not described as uploading code, paths, symbol names, or query contents to an external model service.
02. Install the CLI and Connect an Agent
The complete setup has three parts: install the CLI, connect an agent, and initialize a project. Installing the CLI alone does not configure MCP, and running codegraph install alone does not build a graph for a project.
Initial CodeGraph Setup
-
Install the CodeGraph CLI
Use the official standalone installer or install the global npm package.
-
Connect an AI Coding Agent
Run the interactive installer so it can detect and configure Codex CLI, Claude Code, Cursor, Gemini CLI, and other supported clients.
-
Initialize the Target Project
Enter the repository and run
codegraph init. The command creates.codegraph/and builds the first graph in the same step.
# Install the CodeGraph CLI globally with npm
npm i -g @colbymchenry/codegraph
# One-shot alternative: download and run the interactive installer
npx @colbymchenry/codegraphIf Node.js is unavailable, use the standalone installer for Windows, macOS, or Linux from the official Installation page. The standalone installer adds codegraph to PATH but does not refresh the current terminal. Open a new terminal before running the next commands.
# Connect CodeGraph to detected agents
codegraph install
# Confirm that the CLI resolves in the new terminal
codegraph versionThe installer can detect Codex CLI, Claude Code, Cursor, opencode, Hermes Agent, Gemini CLI, Antigravity IDE, and Kiro. Restart the agent after configuration so the MCP server loads in a new session.
03. Initialize a Project and Run the First Query
Run codegraph init inside the target repository. This command creates the project index and performs the first full graph build in one step, so a separate codegraph index command is unnecessary.
# Enter the project that should be indexed
cd your-project
# Create .codegraph/ and build the first full graph
codegraph init
# Verify file, node, edge, SQLite, and journal statistics
codegraph statuscodegraph status should report file, node, and edge counts along with the SQLite backend and journal mode. If it reports that the project is not initialized, the current directory is usually wrong or codegraph init has not been run for that project.
For the first structural query, start with the natural-language codegraph explore entry point. It combines relevant source, relationship maps, and impact context in one result.
# Ask a natural-language architecture question
codegraph explore "how does login work"
# Find a class by name
codegraph query UserService --kind class --limit 10
# Inspect both directions of a call relationship
codegraph callers handleRequest
codegraph callees handleRequest| Command | Best Used For | Main Output |
|---|---|---|
explore | Understanding a flow or relationships among several symbols | Relevant source, call paths, and impact summary |
query | Locating a function, class, or symbol | Matching symbols and locations |
node | Reading one symbol or file | Line-numbered source and relationships |
callers | Finding what calls a symbol | Reverse call relationships |
callees | Finding what a symbol calls | Forward call relationships |
04. Use CodeGraph in Codex and Other Agents
When an agent starts a project session, it launches codegraph serve --mcp automatically. You generally do not start the MCP server by hand. The default codegraph_explore tool becomes available to the agent when the project contains a .codegraph/ index.
How an Agent Queries CodeGraph
-
Receive a Structural Question
The agent identifies the function, file, business flow, or change target.
-
Call
codegraph_exploreThe MCP server searches relevant symbols in the current project’s local graph.
-
Return Source and Relationships
The result groups line-numbered source by file and includes call paths and a change-impact summary.
-
Modify and Verify
The agent uses the graph result to locate code, then runs the project’s own tests and checks.
You can ask an agent questions such as:
- “Which functions does a login request pass through from the route to the database?”
- “Before changing
AuthMiddleware, list callers, downstream calls, and tests that may be affected.” - “Explain the relationship between
UserServiceand the cache layer, with source locations.”
Including file names, symbol names, and the target flow usually produces more stable results. If a workspace contains several indexed projects, the agent can query another index through projectPath. A path without an index falls back to the agent’s built-in search tools.
05. Check Impact Before Changing Code
codegraph impact traces dependencies outward from a target symbol. codegraph affected starts from changed files and follows import dependencies to find tests that may be affected. Together they provide a workflow for checking impact before a change and selecting tests afterward.
Change-Impact Check
-
Understand the Target Symbol
Use
exploreto inspect the implementation, call paths, and existing impact summary. -
Increase the Impact Depth
Use
impact --depthto inspect farther dependency levels and identify high-risk callers. -
Select Relevant Tests
Pass changed files to
affected, then feed its output into the test command used by the project.
# Inspect the blast radius of a symbol
codegraph impact AuthMiddleware --depth 3
# Find tests affected by one changed source file
codegraph affected src/auth.ts
# Feed the current Git diff into affected-test discovery
git diff --name-only | codegraph affected --stdin
# Produce machine-readable impact data for a script
codegraph impact AuthMiddleware --depth 3 --jsonaffected traverses up to five dependency levels by default. It also supports a custom test-file glob and path-only output. It can narrow the test set, but it does not replace a complete test strategy.
A static dependency graph cannot cover every runtime relationship. Reflection, runtime dependency injection, dynamic string routes, and external service contracts still require integration tests, logs, or human review.
06. Keep the Index Fresh
The MCP server starts a file watcher by default. After a source file is created, changed, or deleted, CodeGraph coalesces nearby writes and updates the graph incrementally. A normal agent session does not require repeated manual codegraph sync commands.
# Check index health and statistics
codegraph status
# Manually apply changed files when the watcher is unavailable
codegraph sync
# Rebuild the complete graph when configuration or mappings changed
codegraph index --force
# Remove a confirmed stale indexing lock
codegraph unlock| Command | When to Use It | Cost |
|---|---|---|
status | Check nodes, edges, files, the SQLite backend, and journal mode | Read-only check |
sync | A sandbox disables the watcher, or a script needs a preflight update | Processes changed files only |
index --force | Extension mappings, inclusion scope, or index state requires a clean rebuild | Reprocesses the entire project |
unlock | No indexer is running, but a stale lock still blocks indexing | Removes the lock before rebuilding |
If an agent has just edited a file, query results identify pending files during the brief synchronization window. When a staleness notice appears, the agent should read that file directly or wait for incremental synchronization before querying again.
07. Control Index Scope and Local Data
The index scope is determined by supported file extensions, default exclusions, and .gitignore. Dependency and build directories such as node_modules, vendor, dist, build, and .next are excluded by default. Files larger than 1 MB are also skipped.
Use codegraph.json in the project root for committed directories that should not be indexed. The same file can map non-standard extensions to supported languages.
{
"exclude": ["static/", "**/vendor/**"],
"include": ["Tools/", "Local/typescript/"],
"extensions": {
".dota_lua": "lua",
".tpl": "php"
}
}Run codegraph index --force after changing codegraph.json so the new scope and extension mappings take full effect. exclude takes precedence over include, and built-in exclusions cannot be re-enabled through include.
The official documentation states that source code, paths, symbol names, and query contents are not included in anonymous telemetry. If organizational policy requires usage statistics to be disabled completely, run:
# Disable anonymous usage telemetry
codegraph telemetry off08. Troubleshoot and Remove CodeGraph
When troubleshooting CodeGraph, check codegraph status before diagnosing connection, lock, or scope problems. Do not keep serve --mcp running manually when MCP fails to connect; supported agents launch the service themselves.
| Symptom | First Check | Resolution |
|---|---|---|
CodeGraph not initialized | Whether the current directory is the target project | Run codegraph init at the project root |
| MCP server does not connect | Index status and the configured agent path | Run status, then rerun codegraph install if needed |
| New symbols are missing | Whether files are pending, supported, or excluded | Wait for auto-sync or run codegraph sync |
database is locked | Version, journal mode, network drive, or WSL path | Upgrade; move the project and index to a local filesystem |
| Indexing is unexpectedly slow | Whether dependency or generated directories are scanned | Fix .gitignore or codegraph.json, then rebuild |
# Check whether an upgrade is available
codegraph upgrade --check
# Upgrade the current installation
codegraph upgrade
# Remove only this project's .codegraph/ index
codegraph uninit
# Remove agent configurations but keep the CLI
codegraph uninstall --keep-cli
# Remove configured integrations and the CLI
codegraph uninstallREFERENCES