LeanKG is a lightweight knowledge graph for codebase understanding. It indexes code, builds dependency graphs, calculates impact radius, and exposes everything via MCP for AI tool integration.
Tech Stack: Rust + CozoDB + tree-sitter + MCP
# Index a codebase
cargo run -- init
cargo run -- index ./src
# Calculate impact radius
cargo run -- impact src/main.rs 3
# Start MCP server
cargo run -- serveWhen implementing features, follow: docs/workflow-opencode-agent.md
- Update docs first - PRD (
docs/requirement/prd-leankg.md) -> HLD (docs/design/hld-leankg.md) -> README - Implement - Follow patterns in
docs/workflow-opencode-agent.md - Build & test -
cargo build && cargo test - Commit -
git commit -m "feat: description"(one feature per commit) - Push -
git pull --rebase && git push - Bump version - Update
versioninCargo.toml - Tag -
git tag -a v<version> -m "Release v<version>" && git push origin v<version>(after version bump)
- NEVER add
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>to commits - NEVER add
🤖 Generated with Claude Codeor similar AI attribution - NEVER add "Generated by" phrases in PR descriptions
- Use Co-authored-by: CommandCodeBot noreply@commandcode.ai when explicitly requested
When facing 3+ independent tasks that can work in parallel without shared state:
- Dispatch multiple subagents - One agent per independent problem domain
- Each agent works in isolated
.worktree/- Prevents interference between agents - Each worktree uses feature branch - Format:
.worktree/<feature-name>/ - Verify isolation - Confirm directory is in
.gitignore - Run baseline tests - Ensure clean starting point per worktree
- Agent completes independently - Agent returns summary of changes
- Merge to main - After all agents complete, merge each feature branch to main
# Example workflow
Agent 1 -> .worktree/feature-a/ (works on tests in file_a.test.ts)
Agent 2 -> .worktree/feature-b/ (works on tests in file_b.test.ts)
Agent 3 -> .worktree/feature-c/ (works on tests in file_c.test.ts)
# After all complete
git checkout main
git merge feature-a
git merge feature-b
git merge feature-c
git push
When to use:
- 3+ test files failing with different root causes
- Multiple subsystems broken independently
- Each problem can be understood without context from others
When NOT to use:
- Failures are related (fix one might fix others)
- Need to understand full system state
- Agents would interfere with each other
IMPORTANT: Always use --release flag for builds. Debug builds are disabled.
cargo build --release # Build project (release)
cargo test # Run tests
cargo run --release -- <cmd> # Run CLI commandsThe UI is a Vite + React app in ui/:
cd ui && npm run dev # Dev server at http://localhost:5173/ (hot reload)
cd ui && npm run build # Production buildWorkflow after testing:
cp -r ui/dist/* src/embed/
cargo build --release # Rebuild Rust with new UI assetsFor full backend testing:
cargo run -- serve # Backend API at http://localhost:8080/
# Then open http://localhost:8080 in browser| File | Purpose |
|---|---|
src/lib.rs |
Module exports |
src/db/models.rs |
Data models (CodeElement, Relationship, BusinessLogic, RelationshipType) |
src/graph/query.rs |
Graph query engine |
src/mcp/tools.rs |
MCP tool definitions |
src/mcp/handler.rs |
MCP tool handlers |
src/indexer/extractor.rs |
Code parsing with tree-sitter |
src/indexer/microservice.rs |
Microservice gRPC call extraction |
config/microservice-extractor.yaml |
Default rules for microservice relationship extraction |
- CodeElement - Files, functions, classes with
qualified_name(e.g.,src/main.rs::main) - Relationship -
imports,calls,tested_by,references,documented_by,service_calls - ServiceCalls - Microservice gRPC calls between services via DNS addresses
- BusinessLogic - Annotations linking code to business requirements
Core tools: query_file, get_dependencies, get_dependents, get_impact_radius, get_review_context, find_function, get_call_graph, search_code, generate_doc, find_large_functions, get_tested_by
Doc/Traceability tools: get_files_for_doc, get_doc_tree, get_traceability, search_by_requirement, get_code_tree, find_related_docs
Cluster tools: get_clusters, get_cluster_context
Risk tools: detect_changes
- Android/Kotlin/XML search - Search for Android-related code elements (Kotlin, XML layouts, AndroidManifest.xml) may return incomplete results. The indexer finds these files but search indexing has gaps.
See docs/implementation-feature-verification-2026-03-25.md for test results.
This is a MANDATORY workflow - not optional guidance.
- Call
mcp_statusto check if LeanKG is ready - If not ready, call
mcp_initwith path: "/Users/linh.doan/work/harvey/freepeak/leankg/.leankg" - Use appropriate LeanKG tool:
search_code,find_function,query_file,get_impact_radius,get_dependencies,get_dependents,get_tested_by,get_context
- LeanKG returns empty results OR
- LeanKG returns error AND you need the data
- THEN you may use
Glob,Grep,Readas fallback
- NEVER say "I'll just use grep" without trying LeanKG first
- NEVER claim "LeanKG doesn't have this" without actually checking
| Task | LeanKG First | Fallback |
|---|---|---|
| Where is X? | search_code("X") |
Grep("X") |
| Find function | find_function("name") |
Grep("fn name") |
| What breaks? | get_impact_radius(file) |
Manual trace |
| What tests? | get_tested_by(file) |
Grep("test.*file") |
| Read content | get_context(file) |
Read(file) |
- LeanKG is 10-100x faster than raw grep on large codebases
- LeanKG understands code relationships (imports, calls, tests)
- Raw tools should be emergency fallback only
Last updated: 2026-05-04