Make AI agents faster and cheaper to use on your codebase.
CodeGraph indexes your PHP code structure (classes, methods, functions, and relationships) and stores it locally. AI agents query this index instead of reading files.
Inspired by colbymchenry/codegraph and kha333n/laravel-code-graph
Result:
- β‘ Faster β Queries in milliseconds instead of reading files
- π° Cheaper β Use fewer tokens, no need to pass file contents
- π Accurate β Structured data about code relationships, not guesses
Ask your AI agent questions directly:
- "Who calls this method?"
- "What breaks if I change this class?"
- "Show me all usages of this function"
- "What's the impact radius of this change?"
- Index your code β
php vendor/bin/codegraph index(one-time) - AI agent uses it β Register CodeGraph as an MCP server (works with Claude Code and any MCP-compatible AI)
- Faster context β AI queries the index instead of reading files
Works with Claude Code, Cursor, and any AI supporting MCP protocol.
composer require maarheeze/codegraph --dev
php vendor/bin/codegraph init
php vendor/bin/codegraph indexThat's it. CodeGraph will:
- Create a
.codegraph/directory with SQLite database (should be ignored by git!) - Scan your code (
app/andsrc/directories by default) - Extract symbols, relationships, and code structure
- Build the index
Optional: Auto-reindex on changes
For continuous development, keep CodeGraph in sync automatically:
php vendor/bin/codegraph watchThis watches your code for changes and reindexes automatically. Press Ctrl+C to stop. To automatically initialize and index your codebase after dependencies are installed or updated, add the following to your composer.json:
{
"scripts": {
"post-autoload-dump": [
"@php artisan codegraph:init || true",
"@php artisan codegraph:index || true"
]
}
}Verify it worked:
php vendor/bin/codegraph statusThis shows how many symbols, edges, and files were indexed.
php vendor/bin/codegraph search UserCodeGraph works with Claude Code via the MCP protocol.
Automatic Setup (Recommended)
Running php vendor/bin/codegraph init automatically:
- Creates
.codegraph/directory and SQLite database - Detects your environment (Sail, Docker, or plain PHP)
- Configures
.mcp.jsonwith the correct MCP command - Adds CodeGraph guidelines to your
CLAUDE.md
That's it! Claude Code will automatically discover and use CodeGraph's tools.
Manual Setup (if needed)
If you need to manually configure .mcp.json:
{
"mcpServers": {
"codegraph": {
"command": "vendor/bin/sail",
"args": ["php", "vendor/bin/codegraph", "mcp"]
}
}
}Replace vendor/bin/sail with:
phpfor plain PHP projectsdockerwith args["compose", "exec", "-it", "app", "php", "vendor/bin/codegraph", "mcp"]for Docker Compose (replaceappwith your actual service name)
Then ask Claude Code:
- "Search for the User model"
- "Who calls the create method on User?"
- "What breaks if I change this service?"
- "Find all usages of the payment provider"
Available tools:
codegraph_searchβ Find symbols by name or FQNcodegraph_callersβ Who calls a symbolcodegraph_calleesβ What a symbol callscodegraph_blast_radiusβ Impact of changing a symbolcodegraph_search_chunksβ Full-text search across code
init β Initialize CodeGraph in your project
php vendor/bin/codegraph initOptional: Explicitly set MCP configuration (auto-detected by default):
# Use Sail
php vendor/bin/codegraph init sail
# Use Docker Compose
php vendor/bin/codegraph init docker
# Use plain PHP (no Docker)
php vendor/bin/codegraph init phpIf not specified, CodeGraph auto-detects your environment:
- Checks for
vendor/bin/sailβ uses Sail - Checks for
docker-compose.ymlβ uses Docker - Falls back to plain PHP otherwise
index β Index your code (tracks changes, only re-parses what changed)
php vendor/bin/codegraph indexwatch β Auto-reindex when files change (useful during development)
php vendor/bin/codegraph watchstatus β See indexing statistics
php vendor/bin/codegraph statusmcp β Start MCP server (for Claude Code integration)
php vendor/bin/codegraph mcpsearch β CLI symbol search
php vendor/bin/codegraph search UserDefault: src/ and app/
php vendor/bin/codegraph index --path app --path libDefault: vendor/, node_modules/, storage/
php vendor/bin/codegraph index --path app --exclude tests --exclude migrationsCodeGraph tracks file hashes and only re-parses changed files:
php vendor/bin/codegraph indexTo force full re-index:
rm -rf .codegraph
php vendor/bin/codegraph init
php vendor/bin/codegraph indexSymbols: Classes, interfaces, traits, enums, methods, functions, properties
Relationships: Method calls, function calls, constructors, inheritance (extends/implements/uses), parent calls
Metadata: File paths, line numbers, visibility (public/protected/private), static/abstract markers, method signatures
- Dynamic class names (
new $className()) are not resolved - Variable method calls (
$obj->$method()) are not detected - Closures and callbacks are not indexed
- External/vendor code (Composer packages) is not indexed
- Template files (Blade, Twig, etc.) are not analyzed
- Service container resolution is not automatic
- 100% code coverage β Comprehensive unit and integration tests
- Index template files β Support for Blade, Twig or HTML templates (core package or plugin TBD)
- Extensive documentation β Architecture guides, API reference, plugin development docs
CodeGraph uses file hashing to detect changes:
php vendor/bin/codegraph indexOnly files that changed since last index are re-parsed. This makes indexing fast on large codebases.
To see what changed:
php vendor/bin/codegraph statususe Maarheeze\CodeGraph\CodeGraph;
// Initialize for current project
$codeGraph = CodeGraph::forProject();
// Get statistics
$stats = $codeGraph->stats();
echo "Symbols: " . $stats['symbols'];
// Index code
$indexStats = $codeGraph->index();
echo "Files indexed: " . $indexStats->getFilesChanged();
// Query the storage directly
$storage = $codeGraph->getStorage();
$symbols = $storage->findByName('User');
$callers = $storage->findEdgesTo('\App\Models\User::create');
$callees = $storage->findEdgesFrom('\App\Models\User::create');
$affected = $storage->blastRadius('\App\Models\User::create', depth: 3);CodeGraph uses SQLite with the following tables:
symbolsβ All indexed symbols (classes, methods, functions, etc.)edgesβ Relationships between symbols (calls, inheritance, etc.)chunksβ Code snippets indexed for full-text searchfilesβ File metadata (paths, hashes, timestamps)
Direct database queries are supported for custom analysis.
MIT