LeanKG
Lightweight Knowledge Graph for AI-Assisted Development
LeanKG is a local-first knowledge graph that gives AI coding tools accurate codebase context. It indexes your code, builds dependency graphs, generates documentation, and exposes an MCP server so tools like Cursor, OpenCode, and Claude Code can query the knowledge graph directly. No cloud services, no external databases -- everything runs on your machine with minimal resources.
Token Savings Example (Benchmarked)
Real benchmark results from the Go API Service example:
| Scenario | Without LeanKG | With LeanKG | Savings |
|---|---|---|---|
| Impact Analysis | 835 tokens | 13 tokens | 98.4% |
| Full Feature Testing | 9,601 tokens | 42 tokens | 99.6% |
# Run the benchmark yourself
Before LeanKG: AI must scan entire codebase to understand dependencies (~9,600 tokens)
After LeanKG: LeanKG provides targeted subgraph with relationships pre-computed (~42 tokens)
Why LeanKG?
AI coding tools waste tokens scanning entire codebases. LeanKG provides targeted context instead:
| Scenario | Without LeanKG | With LeanKG |
|---|---|---|
| File review | Full content of changed files + diff | Blast radius + structural summary |
| Impact analysis | Manually trace dependencies | get_impact_radius returns affected files |
| Token count | 9,600+ tokens for full scan | 13-42 tokens with graph |
Installation
One-Line Install (Recommended)
Install the LeanKG binary and configure MCP for your AI coding tool:
|
Supported targets:
| Target | AI Tool | Config Location |
|---|---|---|
opencode |
OpenCode AI | ~/.config/opencode/opencode.json |
cursor |
Cursor AI | ~/.config/cursor/mcp.json |
claude |
Claude Code/Desktop | ~/.config/claude/settings.json |
gemini |
Gemini CLI | ~/.config/gemini-cli/mcp.json |
antigravity |
Google Antigravity | ~/.gemini/antigravity/mcp_config.json |
Examples:
# Install for OpenCode
|
# Install for Cursor
|
# Install for Claude Code
|
# Install for Gemini CLI
|
# Install for Google Antigravity
|
Install via Cargo
Build from Source
Quick Start
# 1. Initialize LeanKG in your project
# 2. Index your codebase
# 3. Start the MCP server (for AI tools)
# 4. Compute impact radius for a file
# 5. Check index status
How It Works
sequenceDiagram
participant Dev as Developer
participant CLI as LeanKG CLI
participant Indexer as Code Indexer
participant DB as CozoDB
participant MCP as MCP Server
participant AI as AI Tool (Claude/Cursor)
Dev->>CLI: leankg init
CLI->>DB: Initialize graph database
Dev->>CLI: leankg index ./src
CLI->>Indexer: Parse source files
Indexer->>Indexer: Extract functions, imports, calls
Indexer->>DB: Store code elements & relationships
Dev->>CLI: leankg serve
CLI->>MCP: Start MCP server
AI->>MCP: "What's the impact of changing auth.rs?"
MCP->>DB: Query impact radius (N hops)
DB-->>MCP: Affected files list
MCP-->>AI: Targeted context (13 tokens vs 835)
Dev->>CLI: leankg watch
CLI->>Index: Watch for file changes
Index->>DB: Incremental update
- Index -- LeanKG parses your codebase and builds a graph of code elements (functions, classes, modules) and their relationships (imports, calls, tests).
- Query -- AI tools query the graph via MCP instead of scanning files.
- Optimize -- Get targeted context with ~99% token reduction.
MCP Server Setup
LeanKG exposes a Model Context Protocol (MCP) server that AI tools can connect to.
Automated Setup (Recommended)
Use the install script to install and configure MCP for your AI tool:
|
Manual Setup
OpenCode AI
Add to ~/.config/opencode/opencode.json:
Cursor AI
Add to ~/.config/cursor/mcp.json:
Claude Code / Claude Desktop
Add to ~/.config/claude/settings.json:
Gemini CLI
Add to ~/.config/gemini-cli/mcp.json:
Google Antigravity
Add to ~/.gemini/antigravity/mcp_config.json:
Starting the MCP Server
# Stdio mode with auto-indexing (for local AI tools)
# Stdio mode without auto-indexing
Making LeanKG the Default Search for AI Agents
LeanKG can instruct AI coding agents to use it first before falling back to naive search. This works similarly to the Morph plugin for OpenCode.
How It Works
- LeanKG embeds an instructions file that tells AI agents when to use LeanKG tools
- The instructions are written to
instructions/leankg-tools.mdduringleankg install - OpenCode reads these instructions and follows them automatically
Setup
# 1. Install LeanKG with MCP config
# 2. Copy instructions to OpenCode config directory
# 3. Verify OpenCode picks up the instructions
# You should see LeanKG tools listed first in available tools
What the Instructions Do
The instructions tell AI agents:
| Task | Use LeanKG Instead Of |
|---|---|
| Find "where is X?" | grep/ripgrep |
| "What tests cover this?" | glob + content search |
| "What would break if I change X?" | Manual dependency tracing |
| "How does X work?" | Reading entire files |
Decision flow for AI agents:
User asks about codebase →
First check LeanKG tools (mcp_status) →
If not initialized, run mcp_init first →
Use appropriate LeanKG tool →
NEVER fall back to naive search until LeanKG is exhausted
Tools AI Agents Learn to Use First
| Priority | Tool | Use Case |
|---|---|---|
| 1 | search_code |
Find code by name/type |
| 2 | get_impact_radius |
Blast radius before changes |
| 3 | get_call_graph |
Understand function flow |
| 4 | get_tested_by |
Find related tests |
| 5 | get_dependencies |
Understand imports |
Highlights
- Code Indexing -- Parse and index Go, TypeScript, Python, and Rust codebases with tree-sitter.
- Dependency Graph -- Build call graphs with
IMPORTS,CALLS, andTESTED_BYedges. - Impact Radius -- Compute blast radius for any file to see downstream impact.
- Auto Documentation -- Generate markdown docs from code structure automatically.
- MCP Server -- Expose the graph via MCP protocol for AI tool integration.
- File Watching -- Watch for changes and incrementally update the index.
- CLI -- Single binary with init, index, serve, impact, and status commands.
- Business Logic Mapping -- Annotate code elements with business logic descriptions and link to features.
- Traceability -- Show feature-to-code and requirement-to-code traceability chains.
- Documentation Mapping -- Index docs/ directory, map doc references to code elements.
Auto-Indexing
LeanKG watches your codebase and automatically keeps the knowledge graph up-to-date.
# Start file watcher -- indexes changes automatically in background
# Incremental indexing -- only re-index changed files (git-based)
# Filter by language
# Exclude patterns
graph LR
subgraph "File Watcher"
FS[File System Events]
Git[Git Status]
Parse[Parser]
DB[(CozoDB)]
end
FS -->|change detected| Git
Git -->|only changed files| Parse
Parse -->|update relationships| DB
- Watch Mode --
leankg watchmonitors your source directory for file changes. - Git-Based Delta -- Uses
git diffto detect only modified files. - Incremental Update -- Re-parses only changed files and updates affected relationships.
- Background Sync -- Runs in background while you code.
Architecture
graph TB
subgraph "AI Tools"
Claude[Claude Code]
Open[OpenCode]
Cursor[Cursor]
Antigravity[Google Antigravity]
end
subgraph "LeanKG"
CLI[CLI Interface]
MCP[MCP Server]
Watcher[File Watcher]
subgraph "Core"
Indexer[tree-sitter Parser]
Graph[Graph Engine]
Cache[Query Cache]
end
subgraph "Storage"
CozoDB[(CozoDB)]
end
Web[Web UI]
end
Claude --> MCP
Open --> MCP
Cursor --> MCP
Antigravity --> MCP
CLI --> Indexer
CLI --> Graph
Watcher --> Indexer
Indexer --> CozoDB
Graph --> CozoDB
Graph --> Cache
Web --> Graph
CLI Commands
| Command | Description |
|---|---|
leankg init |
Initialize LeanKG in the current directory |
leankg index [path] |
Index source files at the given path |
leankg index --incremental |
Only index changed files (git-based) |
leankg index --lang go,ts,py,rs |
Filter by language |
leankg index --exclude vendor,node_modules |
Exclude patterns |
leankg serve |
Start the MCP server (WebSocket) |
leankg serve --mcp-port 3000 |
Custom MCP server port |
leankg mcp-stdio |
Start MCP server with stdio transport |
leankg impact <file> --depth N |
Compute blast radius for a file |
leankg status |
Show index statistics and status |
leankg generate |
Generate documentation from the graph |
leankg install |
Auto-install MCP config for AI tools |
leankg watch |
Start file watcher for auto-indexing |
leankg quality --min-lines N |
Find oversized functions by line count |
leankg query <text> --kind name |
Query the knowledge graph |
leankg annotate <element> -d <desc> |
Add business logic annotation |
leankg link <element> <id> |
Link element to feature |
leankg search-annotations <query> |
Search business logic annotations |
leankg show-annotations <element> |
Show annotations for a specific element |
leankg trace --feature <id> |
Show feature-to-code traceability |
leankg find-by-domain <domain> |
Find code by business domain |
leankg export |
Export graph data as JSON |
leankg docs --tree |
Show documentation directory structure |
leankg docs --for <file> |
Show docs referencing a code file |
leankg docs --link <doc> <element> |
Link documentation to code element |
leankg trace <element> |
Show traceability chain for element |
leankg trace --requirement <id> |
Trace code for a requirement |
MCP Tools
| Tool | Description |
|---|---|
mcp_init |
Initialize LeanKG project (creates .leankg/, leankg.yaml) |
mcp_index |
Index codebase (path, incremental, lang, exclude options) |
mcp_install |
Create .mcp.json for MCP client configuration |
mcp_status |
Show index statistics and status |
mcp_impact |
Calculate blast radius for a file |
query_file |
Find file by name or pattern |
get_dependencies |
Get file dependencies (direct imports) |
get_dependents |
Get files depending on target |
get_impact_radius |
Get all files affected by change within N hops |
get_review_context |
Generate focused subgraph + structured review prompt |
get_context |
Get AI context for file (minimal, token-optimized) |
find_function |
Locate function definition |
get_call_graph |
Get function call chain (full depth) |
search_code |
Search code elements by name/type |
generate_doc |
Generate documentation for file |
find_large_functions |
Find oversized functions by line count |
get_tested_by |
Get test coverage for a function/file |
get_doc_for_file |
Get documentation files referencing a code element |
get_files_for_doc |
Get code elements referenced in a documentation file |
get_doc_structure |
Get documentation directory structure |
get_traceability |
Get full traceability chain for a code element |
search_by_requirement |
Find code elements related to a requirement |
get_doc_tree |
Get documentation tree structure |
get_code_tree |
Get codebase structure |
find_related_docs |
Find documentation related to a code change |
Auto-Initialization: When the MCP server starts without an existing LeanKG project, it automatically initializes and indexes the current directory. This provides a "plug and play" experience for AI tools.
Auto-Indexing: When the MCP server starts with an existing LeanKG project, it checks if the index is stale (by comparing git HEAD commit time vs database file modification time). If stale, it automatically runs incremental indexing to ensure AI tools have up-to-date context.
Supported AI Tools
| Tool | Integration | Status |
|---|---|---|
| Claude Code | MCP | Supported |
| OpenCode | MCP | Supported |
| Cursor | MCP | Supported |
| Google Antigravity | MCP | Supported |
| Windsurf | MCP | Supported |
| Codex | MCP | Supported |
Roadmap
Phase 2 -- Pipeline Integration
| Feature | Status | Description |
|---|---|---|
| Pipeline Parsing | Planned | Parse CI/CD config files (GitHub Actions, GitLab CI, Jenkins, Azure) |
| Pipeline Graph | Planned | Build pipeline, stage, step nodes |
| Trigger Links | Planned | Link source file changes to triggered pipelines |
| Pipeline Impact | Planned | Include pipelines in blast radius analysis |
| Deployment Targets | Planned | Track which stages deploy to which environments |
Supported CI/CD Platforms (Coming Soon):
- GitHub Actions (
.github/workflows/*.yml) - GitLab CI (
.gitlab-ci.yml) - Jenkins (
Jenkinsfile) - Azure Pipelines (
azure-pipelines.yml)
Future Features
| Feature | Description |
|---|---|
| Semantic Search | AI-powered code search using embeddings |
| Security Analysis | Detect vulnerable dependencies and patterns |
| Cost Estimation | Cloud resource cost tracking via pipeline data |
| Multi-Project | Index and query across multiple repositories |
Requirements
For npm installation (recommended):
- Node.js 18+
- npm 8+
For building from source:
- Rust 1.70+
- macOS or Linux
Tech Stack
| Component | Technology |
|---|---|
| Language | Rust |
| Database | CozoDB (embedded relational-graph, Datalog queries) |
| Parsing | tree-sitter |
| CLI | Clap |
| Web Server | Axum |
| Installer | Node.js (npm package for binary distribution) |
Project Structure
src/
cli/ - CLI commands (Clap)
config/ - Project configuration
db/ - CozoDB persistence layer
doc/ - Documentation generator
graph/ - Graph query engine
indexer/ - Code parser (tree-sitter)
doc_indexer/ - Documentation indexer
mcp/ - MCP protocol handler
watcher/ - File change watcher
web/ - Web server (Axum)
docs/
planning/ - Planning documents
requirement/ - Requirements documents (PRD)
analysis/ - Analysis documents
design/ - Design documents (HLD)
business/ - Business logic documents
License
MIT