A Model Context Protocol (MCP) server that provides deep codebase analysis through AST (Abstract Syntax Tree) parsing and dependency graph generation for Python, JavaScript, PHP, and HTML projects.
This MCP server enables Claude Code and Codex CLI to understand your codebase structure by:
- Extracting functions, classes, and imports from source files
- Building dependency graphs showing import relationships
- Analyzing module architecture and detecting circular dependencies
- Searching for symbols across the entire codebase
✨ Features a custom lightweight graph implementation using only Python built-ins - no NetworkX or external graph libraries required!
The server is already configured and ready to use! When you start a Claude Code or Codex CLI session, the MCP tools will be automatically available.
Start Claude Code or Codex in any of your projects and you should see:
# Codebase Context Loaded
You now have access to MCP tools for deep codebase analysis...
Then ask your assistant to analyze your project:
User: "Give me a summary of this project"
Assistant: [Automatically uses `get_project_summary`]
Note: Throughout this document, any mention of "Claude" refers to whichever MCP-enabled client you're running (Claude Code or Codex CLI).
Codex CLI now loads the exact same MCP tooling:
~/.codex/settings.jsonmirrors the Claude hooks so SessionStart/PostToolUse run/home/tchang/MCP/rules/*.shfor Codex sessions.~/.codex/mcp.jsonregisters thecodebase-contextserver with the same command/args, using the Codex project directory environment variable when available.~/.config/mcp/servers.jsonprovides a global registry entry so other MCP-aware tools can reuse the server without extra setup.rules/session_start_hook.shwas updated to detect whether Claude or Codex launched the session and prints the correct client label.
To use it: restart the Codex CLI, open any project, and run list_mcp_resources (or just start coding). You should immediately see the codebase-context server in the tool list and receive the same automatic project summary workflow that Claude Code uses.
/home/tchang/MCP/
├── codebase-context-server/
│ ├── server.py # Main MCP server
│ ├── ast_tools.py # AST parsing for Python/JS/PHP/HTML
│ ├── graph_tools.py # Dependency graph generation
│ ├── requirements.txt # Python dependencies
│ └── venv/ # Python 3.13 virtual environment
├── rules/
│ ├── codebase_instructions.md # Instructions for Claude/Codex
│ └── session_start_hook.sh # SessionStart hook script
└── git-hooks/ # Git hook templates (for future use)
Get high-level statistics about the codebase.
Usage:
Claude will call this automatically at session start
Returns:
- Total files, functions, classes, lines
- Language breakdown (Python, JS, PHP, HTML)
Extract detailed AST information from a single file.
Parameters:
file_path: Path to file (relative to project root)deep: Boolean, include full AST structure (default: false)
Returns:
- Functions with signatures and locations
- Classes with inheritance info
- Import statements
- File statistics
Analyze all supported files in a directory.
Parameters:
directory: Path to directoryextensions: Optional filter (e.g., [".py", ".js"])deep: Include full AST structure
Returns:
- Complete AST data for all files
- Summary statistics by language
Build dependency graph showing import relationships.
Parameters:
directory: Project root (defaults to current)output_format: "summary" (default) or "json"
Returns:
- Total files and dependencies
- Circular dependencies (if any)
- Most imported files
High-level view of module organization.
Returns:
- Entry points (files not imported by others)
- Leaf modules (files that don't import others)
- Directory breakdown
- Circular dependencies
Show dependencies for a specific file.
Parameters:
file_path: File to analyzedirectory: Project root
Returns:
- What the file imports
- What imports the file
Find functions or classes by name across the codebase.
Parameters:
pattern: Search string (substring match)symbol_type: "function", "class", or "both"
Returns:
- Matching symbols with file locations and line numbers
| Language | Extensions | Functions | Classes | Imports | Full AST | Graphs |
|---|---|---|---|---|---|---|
| Python | .py | ✓ | ✓ | ✓ | ✓ | ✓ |
| JavaScript/TypeScript | .js, .jsx, .ts, .tsx | ✓ | ✓ | ✓ | ✓ | ✓ |
| PHP | .php | ✓ | ✓ | ✓ | ✓ | ✓ |
| HTML | .html, .htm | ✗ | ✗ | ✗ | ✓ | ✗ |
Note: Dependency graph generation uses a custom lightweight implementation with zero external dependencies!
When you start a Claude Code or Codex session:
- SessionStart hook runs (
~/.claude/settings.json) - Hook displays available tools
- Claude automatically calls
get_project_summary - You get an overview of your codebase
Claude intelligently uses tools based on context:
- Before editing: Analyzes file AST and dependencies
- Architecture questions: Generates dependency graphs
- Code search: Uses symbol search
- Impact analysis: Checks file dependencies
MCP Server Config (~/.claude/mcp.json):
{
"mcpServers": {
"codebase-context": {
"command": "/usr/local/bin/python3.13",
"args": ["/home/tchang/MCP/codebase-context-server/server.py"],
"env": {
"PROJECT_DIR": "${CLAUDE_PROJECT_DIR}"
}
}
}
}SessionStart Hook (~/.claude/settings.json):
{
"hooks": {
"SessionStart": [
{
"matcher": "startup",
"hooks": [
{
"type": "command",
"command": "/home/tchang/MCP/rules/session_start_hook.sh"
}
]
}
]
}
}Codex MCP Config (~/.codex/mcp.json):
{
"mcpServers": {
"codebase-context": {
"command": "/home/tchang/MCP/codebase-context-server/venv/bin/python",
"args": [
"/home/tchang/MCP/codebase-context-server/server.py"
],
"env": {
"PROJECT_DIR": "${CODEX_PROJECT_DIR}",
"PYTHONPATH": "/home/tchang/MCP/codebase-context-server"
}
}
}
}Codex Hooks + MCP Config (~/.codex/settings.json):
{
"hooks": {
"SessionStart": [
{
"matcher": "startup",
"hooks": [
{
"type": "command",
"command": "/home/tchang/MCP/rules/session_start_hook.sh"
}
]
}
],
"PostToolUse": [
{
"matcher": "Write|Edit",
"hooks": [
{
"type": "command",
"command": "/home/tchang/MCP/rules/post_edit_hook.sh"
}
]
}
]
},
"mcpServers": {
"codebase-context": {
"command": "/home/tchang/MCP/codebase-context-server/venv/bin/python",
"args": [
"/home/tchang/MCP/codebase-context-server/server.py"
],
"env": {
"PROJECT_DIR": "${CODEX_PROJECT_DIR}",
"PYTHONPATH": "/home/tchang/MCP/codebase-context-server"
},
"enabled": true
}
}
}cd /home/tchang/python/projects/your-project
claude
# Claude should automatically show project summaryAsk Claude:
"Analyze the structure of main.py"
Claude will use analyze_file_ast to show functions, classes, and imports.
Ask Claude:
"Show me the dependency structure of this project"
Claude will use get_module_structure and get_dependency_graph.
Ask Claude:
"Where is the login function defined?"
Claude will use search_symbols to find it.
Check MCP configuration (Claude):
cat ~/.claude/mcp.jsonCheck MCP configuration (Codex):
cat ~/.codex/mcp.jsonTest server manually:
cd /home/tchang/MCP/codebase-context-server
source venv/bin/activate
python server.py
# Should wait for input (MCP server running)
# Press Ctrl+C to exitCheck settings (Claude):
cat ~/.claude/settings.jsonCheck settings (Codex):
cat ~/.codex/settings.jsonTest hook manually:
/home/tchang/MCP/rules/session_start_hook.sh
# Should display instructionsReinstall dependencies:
cd /home/tchang/MCP/codebase-context-server
source venv/bin/activate
pip install -r requirements.txtCheck Claude Code logs:
tail -f ~/.claude/debug/*.logRestart Claude Code:
# Exit current session and start freshFor projects with 100k+ lines:
- Tools are optimized with caching
- Use targeted queries instead of full analysis
deep=false(default) for faster analysis
The following directories are automatically skipped:
.git,__pycache__,node_modulesvenv,.venv,vendordist,build
Goal: Use Phase 1 tools to analyze your actual projects
Process:
- Claude analyzes your codebases using current tools
- Discovers patterns:
- How routes/endpoints are defined
- Where database queries are located
- Configuration file formats
- Documentation structure
Deliverable: Design document for specialized tools
Goal: Build tools tailored to your codebase
Planned Tools:
-
API Route Discovery
- Auto-detect framework (Flask, Slim, Express, etc.)
- Extract all endpoints with methods and handlers
-
Database Schema Extraction
- Connect to MySQL database
- Parse migration files
- Extract from ORM models
-
Configuration Parser
- Parse .env files
- Extract from config.php, settings.py
- Redact sensitive data
-
Documentation Indexer
- Index docstrings and comments
- Parse README and docs/
- Searchable documentation context
The server works across all your projects:
/home/tchang/python/projects/(16,524 files, ~365k lines)/home/tchang/python/Working/(651 files, ~464k lines)/var/www/html/samplelogin/(1,116 files, ~1.4M lines)
Edit ast_tools.py to add more languages:
EXT_TO_LANG = {
'.py': 'python',
'.js': 'javascript',
# Add your custom extensions here
}Graphs are automatically exported to .context/ in each project:
dependency-graph.json- Machine-readable formatdependency-graph.dot- Graphviz format for visualization
# Install Graphviz
sudo apt-get install graphviz
# Generate PNG from DOT file
dot -Tpng .context/dependency-graph.dot -o dependency-graph.pngWhen you start using these tools, keep notes on:
- What patterns you discover in your code
- What additional context would be helpful
- Specific pain points or repetitive queries
This will inform the design of Phase 3 specialized tools.
For issues or questions:
- Check the troubleshooting section above
- Review
/home/tchang/MCP/rules/codebase_instructions.md - Test components manually (server, hooks, tools)
- MCP Server:
/home/tchang/MCP/codebase-context-server/ - Instructions:
/home/tchang/MCP/rules/codebase_instructions.md - MCP Config:
~/.claude/mcp.json - Settings:
~/.claude/settings.json - Logs:
~/.claude/debug/
Phase 1 - Complete
- AST analysis for Python, JS, PHP, HTML
- Dependency graph generation
- Module structure analysis
- Symbol search
- Automatic session initialization
Phase 2 - Planned
- Codebase pattern discovery
- Framework detection
Phase 3 - Planned
- API route discovery
- Database schema extraction
- Configuration parsing
- Documentation indexing