CodeGraph Usage Guide

Learn how to install CodeGraph, index a project, connect it over MCP, query code relationships, assess change impact, select affected tests, keep the graph fresh, and troubleshoot common failures.

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?”

TEXT
Source files
     |
     v
Tree-sitter extraction
     |
     v
.codegraph/codegraph.db
     |
     v
MCP / CLI query
     |
     v
Source + call paths + blast radius
The path from source indexing to an agent query in CodeGraph

Code 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

  1. Install the CodeGraph CLI

    Use the official standalone installer or install the global npm package.

  2. 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.

  3. Initialize the Target Project

    Enter the repository and run codegraph init. The command creates .codegraph/ and builds the first graph in the same step.

POWERSHELL
# Install the CodeGraph CLI globally with npm
npm i -g @colbymchenry/codegraph

# One-shot alternative: download and run the interactive installer
npx @colbymchenry/codegraph

If 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.

POWERSHELL
# Connect CodeGraph to detected agents
codegraph install

# Confirm that the CLI resolves in the new terminal
codegraph version

The 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.

POWERSHELL
# 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 status

codegraph 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.

POWERSHELL
# 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
CommandBest Used ForMain Output
exploreUnderstanding a flow or relationships among several symbolsRelevant source, call paths, and impact summary
queryLocating a function, class, or symbolMatching symbols and locations
nodeReading one symbol or fileLine-numbered source and relationships
callersFinding what calls a symbolReverse call relationships
calleesFinding what a symbol callsForward 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

  1. Receive a Structural Question

    The agent identifies the function, file, business flow, or change target.

  2. Call codegraph_explore

    The MCP server searches relevant symbols in the current project’s local graph.

  3. Return Source and Relationships

    The result groups line-numbered source by file and includes call paths and a change-impact summary.

  4. 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 UserService and 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

  1. Understand the Target Symbol

    Use explore to inspect the implementation, call paths, and existing impact summary.

  2. Increase the Impact Depth

    Use impact --depth to inspect farther dependency levels and identify high-risk callers.

  3. Select Relevant Tests

    Pass changed files to affected, then feed its output into the test command used by the project.

POWERSHELL
# 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 --json

affected 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.

POWERSHELL
# 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
CommandWhen to Use ItCost
statusCheck nodes, edges, files, the SQLite backend, and journal modeRead-only check
syncA sandbox disables the watcher, or a script needs a preflight updateProcesses changed files only
index --forceExtension mappings, inclusion scope, or index state requires a clean rebuildReprocesses the entire project
unlockNo indexer is running, but a stale lock still blocks indexingRemoves 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.

JSON
{
  "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:

POWERSHELL
# Disable anonymous usage telemetry
codegraph telemetry off

08. 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.

SymptomFirst CheckResolution
CodeGraph not initializedWhether the current directory is the target projectRun codegraph init at the project root
MCP server does not connectIndex status and the configured agent pathRun status, then rerun codegraph install if needed
New symbols are missingWhether files are pending, supported, or excludedWait for auto-sync or run codegraph sync
database is lockedVersion, journal mode, network drive, or WSL pathUpgrade; move the project and index to a local filesystem
Indexing is unexpectedly slowWhether dependency or generated directories are scannedFix .gitignore or codegraph.json, then rebuild
POWERSHELL
# 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 uninstall

REFERENCES

References

  1. 01CodeGraph Official GitHub Repository
  2. 02CodeGraph: Your First Graph
  3. 03CodeGraph Installation
  4. 04CodeGraph: Indexing a Project
  5. 05CodeGraph MCP Server Reference
  6. 06CodeGraph CLI Reference
  7. 07CodeGraph: Affected Tests in CI
  8. 08CodeGraph Troubleshooting

Next step

Keep tracking CodeGraph

Continue along the same topic.

Open entity record