A Python tool for analyzing codebases using graph database techniques. Parse source code into a graph structure and query relationships between code elements. Includes an MCP server for AI-powered code analysis with Claude Desktop.
- MCP Server Integration: Connect to Claude Desktop for AI-powered code analysis through natural language
- Parse Python codebases into a graph structure
- Store code elements (files, classes, functions, imports) as graph nodes
- Track relationships (contains, imports, calls) as graph edges
- Query the graph to find dependencies, dependents, and paths between elements
- Detect circular dependencies
- Export and import graph databases for reuse
- Command-line interface and Python API
Install directly from GitHub using pip:
pip install git+https://github.com/bursonic/code-graph-db.gitThat's it! The code-graph-db command is now available.
If you want to contribute or modify the code:
# Clone the repository
git clone https://github.com/bursonic/code-graph-db.git
cd code-graph-db
# Create and activate virtual environment
python3 -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
# Install in development mode
pip install -e .-
Parse your codebase:
code-graph-db parse /path/to/your/project -o myproject.db
-
Query it:
code-graph-db query myproject.db --stats
-
Use with Claude Desktop (optional):
See MCP Server Setup below for AI-powered code analysis.
Parse a codebase:
code-graph-db parse /path/to/codebase -o codebase.dbQuery the database:
# Show statistics
code-graph-db query codebase.db --stats
# Find nodes by name
code-graph-db query codebase.db --find "MyClass"
# Filter by type
code-graph-db query codebase.db --find "MyClass" --type classfrom code_graph_db import CodeGraph, CodeParser, QueryEngine
# Create a graph and parse code
graph = CodeGraph()
parser = CodeParser()
parser.parse_directory("/path/to/codebase", graph)
# Query the graph
engine = QueryEngine(graph)
# Find all classes
classes = graph.get_nodes_by_type("class")
# Find dependencies
deps = engine.find_dependencies(node_id)
# Find dependents
dependents = engine.find_dependents(node_id)
# Detect circular dependencies
cycles = engine.find_cycles()
# Get statistics
stats = engine.get_statistics()
# Save the graph
graph.save("codebase.db")
# Load a saved graph
graph.load("codebase.db")The MCP (Model Context Protocol) server lets AI assistants like Claude analyze your codebase through natural language queries.
pip install git+https://github.com/bursonic/code-graph-db.gitcode-graph-db parse /path/to/your/project -o ~/myproject.dbAdd this to your Claude Desktop config file:
macOS/Linux: ~/Library/Application Support/Claude/claude_desktop_config.json
Windows: %APPDATA%\Claude\claude_desktop_config.json
{
"mcpServers": {
"code-graph-db": {
"command": "code-graph-db",
"args": ["mcp", "--database", "/absolute/path/to/your/myproject.db"]
}
}
}Important: Replace /absolute/path/to/your/myproject.db with the actual absolute path to your database file.
The MCP server will now be available in Claude Desktop. You can ask Claude to:
- "What classes are in my codebase?"
- "Show me all dependencies of MyClass"
- "Find circular dependencies"
- "What functions call calculate_total?"
The server exposes 13 tools for code analysis:
parse_codebase- Parse and create graph databaseload_database_tool- Load existing databaseget_statistics- Get node/edge countsfind_by_name- Search for code elementsfind_dependencies- Find what a node depends onfind_dependents- Find what depends on a nodefind_path- Find path between two nodesfind_cycles- Detect circular dependenciesget_node- Get detailed node informationget_nodes_by_type- Get all nodes of a typeget_neighbors- Get adjacent nodessearch_by_attribute- Search by any attributeget_all_nodes- List all nodes
Command not found:
- Make sure the tool is installed:
pip install git+https://github.com/bursonic/code-graph-db.git - Verify installation:
which code-graph-db(should show path to executable) - If using a virtual environment, use absolute path:
"command": "/path/to/venv/bin/code-graph-db"
Server not appearing in Claude Desktop:
- Check the config file JSON syntax is valid
- Ensure you've restarted Claude Desktop completely
- Look for errors in Claude Desktop's logs (Help → View Logs)
Database path issues:
- Always use absolute paths, not relative paths
- Expand
~to full path:/Users/yourname/myproject.dbnot~/myproject.db - Ensure the database file exists and is readable
The tool consists of three main components:
-
CodeGraph (
graph.py): Core graph database using NetworkX- Add/retrieve nodes and edges
- Save/load graph to disk
- Navigate relationships
-
CodeParser (
parser.py): Parse source code using Tree-sitter- Extract code structure (classes, functions, imports)
- Build graph representation
- Support for Python (extensible to other languages)
-
QueryEngine (
query.py): Query and analyze the graph- Find nodes by name or attributes
- Traverse dependencies
- Detect cycles
- Calculate statistics
All development should be done within the virtual environment:
# Activate virtual environment
source venv/bin/activate # On Windows: venv\Scripts\activate
# Install development dependencies
pip install -r requirements-dev.txtRun tests:
venv/bin/pytest # or just `pytest` if venv is activatedFormat code:
venv/bin/black code_graph_db testsLint code:
venv/bin/ruff check code_graph_db testsType check:
venv/bin/mypy code_graph_db- Support for more programming languages (JavaScript, Java, Go, etc.)
- Function call graph analysis
- Advanced query language
- Visualization tools
- Integration with IDEs
- Performance optimizations for large codebases
- Export to various graph formats (GraphML, DOT, etc.)
MIT License - see LICENSE file for details
Contributions are welcome! Please feel free to submit a Pull Request.