Everything you need to know about LeanKG. From installation to advanced usage with your AI coding tools.
Ready to get started?
LeanKG is a local-first knowledge graph that gives AI coding tools accurate codebase context. It indexes your code, builds dependency graphs, and exposes an MCP server so tools like Claude, Cursor, and OpenCode can query the knowledge graph directly.
curl -fsSL https://raw.githubusercontent.com/FreePeak/LeanKG/main/scripts/install.sh | bash -s -- <target># Install for OpenCode
curl -fsSL https://raw.githubusercontent.com/FreePeak/LeanKG/main/scripts/install.sh | bash -s -- opencode
# Install for Cursor
curl -fsSL https://raw.githubusercontent.com/FreePeak/LeanKG/main/scripts/install.sh | bash -s -- cursor
# Install for Claude Code
curl -fsSL https://raw.githubusercontent.com/FreePeak/LeanKG/main/scripts/install.sh | bash -s -- claudecargo install leankg
leankg --versiongit clone https://github.com/FreePeak/LeanKG.git
cd LeanKG
cargo build --releaseCreates .leankg/ directory and leankg.yaml configuration.
# Initialize LeanKG in your project directory
leankg initParses code with tree-sitter, extracts functions, classes, imports, and call relationships.
# Index all source files
leankg index ./src
# With language filter
leankg index ./src --lang go,ts,py,rs
# Incremental index (only changed files)
leankg index --incrementalExposes MCP tools for AI tools to query your knowledge graph.
# Start MCP server for AI tools
leankg serve
# Or with stdio transport (for local AI tools)
leankg mcp-stdio --watchVisualize your codebase's dependency graph in real-time.
# Start web UI for graph visualization
leankg web
# Open http://localhost:8080Shows database statistics, indexed files count, and last update time.
# View index statistics
leankg status// Add to ~/.config/opencode/opencode.json
{
"mcp": {
"leankg": {
"type": "local",
"command": ["leankg", "mcp-stdio", "--watch"],
"enabled": true
}
}
}// Add to ~/.cursor/mcp.json
{
"mcpServers": {
"leankg": {
"command": "leankg",
"args": ["mcp-stdio", "--watch"]
}
}
}// Add to ~/.config/claude/settings.json
{
"mcpServers": {
"leankg": {
"command": "leankg",
"args": ["mcp-stdio", "--watch"]
}
}
}// Add to ~/.config/gemini-cli/mcp.json
{
"mcpServers": {
"leankg": {
"command": "leankg",
"args": ["mcp-stdio", "--watch"]
}
}
}// Add to ~/.config/kilo/kilo.json
{
"mcp": {
"leankg": {
"type": "local",
"command": ["leankg", "mcp-stdio", "--watch"],
"enabled": true
}
}
}When the MCP server starts without an existing LeanKG project, it automatically initializes and indexes the current directory. Provides a 'plug and play' experience for AI tools.
When the MCP server starts with an existing LeanKG project, it checks if the index is stale. If stale, it automatically runs incremental indexing to ensure AI tools have up-to-date context.
# Index with language filter
leankg index ./src --lang go,ts,py,rs,java,kotlin
# Exclude patterns
leankg index ./src --exclude vendor,node_modules,dist
# Incremental index (git-based, only changed files)
leankg index --incremental# Compute blast radius for a file
leankg impact src/main.rs --depth 3
# Find oversized functions
leankg quality --min-lines 50
# Query the knowledge graph
leankg query "authentication" --kind name# Generate documentation from the graph
leankg generate
# Show documentation structure
leankg docs --tree
# Show docs for a file
leankg docs --for src/auth.rs
# Show feature-to-code traceability
leankg trace --feature AUTH-001LeanKG provides agentic instructions that tell AI coding tools to use LeanKG FIRST for codebase queries before scanning the entire codebase.
## MANDATORY: Use LeanKG First
Before ANY codebase search/navigation, use LeanKG tools:
1. mcp_status - check if ready
2. Use tool: search_code, find_function, query_file, get_impact_radius
3. Only fallback to grep/read if LeanKG fails
| Task | Use |
|-------------------|-----|
| Where is X? | search_code or find_function |
| What breaks if I change Y? | get_impact_radius |
| What tests cover Y? | get_tested_by |
| How does X work? | get_context |Token savings vary by task complexity. Complex impact/debugging queries may timeout. LeanKG excels at targeted subgraph retrieval vs full codebase scanning.
If the MCP server reports 'LeanKG not initialized', manually run leankg init in your project directory, then restart the AI tool.
Run leankg index --incremental to update the index with the latest changes, or leankg watch to enable automatic background indexing.
# Check if LeanKG is running
leankg status
# Restart MCP server
leankg mcp-stdio --watchVisit the GitHub repository for issues, discussions, and support.
Need help?
Check our GitHub for issues and discussions