Are you the author? Sign in to claim
LeanKG: Stop Burning Tokens. Start Coding Lean.
Local-first code knowledge graph for AI coding agents
Surgical context · fewer tool calls · blast-radius awareness · 100% local
Pre-index your repo. Serve precise subgraphs over MCP to Cursor, Claude Code, OpenCode, and more — no cloud, no external database.
Live Demo → · Docs · Docker Hub
One command — binary, MCP wiring, and agent docs for your tool of choice:
# macOS / Linux
curl -fsSL https://raw.githubusercontent.com/FreePeak/LeanKG/main/scripts/install.sh | bash -s -- <target>
| Target | What you get |
|---|---|
cursor | Binary + MCP + skill + AGENTS.md + session hook |
claude | Binary + MCP + plugin + skill + CLAUDE.md + hooks |
opencode | Binary + MCP + plugin + skill + AGENTS.md |
gemini / kilo / antigravity | Binary + MCP + skill + agent docs |
docker | Hub image + index + embed + MCP HTTP (no Rust) |
curl -fsSL https://raw.githubusercontent.com/FreePeak/LeanKG/main/scripts/install.sh | bash -s -- cursor
cargo install leankg
# or
git clone https://github.com/FreePeak/LeanKG.git && cd LeanKG && cargo build --release
curl -fsSL https://raw.githubusercontent.com/FreePeak/LeanKG/main/scripts/docker-up.sh | bash
curl http://localhost:9699/health
Point your MCP client at http://localhost:9699/mcp. Multi-project RocksDB mounts: AGENTS.md.
Published Hub tags currently target
linux/arm64. Onlinux/amd64, build withdocker compose -f docker-compose.rocksdb.yml up --build.
Installing the binary alone does not connect your agent. Run setup (or use an install target above) so MCP is registered:
leankg setup
This configures Cursor, Claude Code, OpenCode, Gemini, and other supported clients with LeanKG’s MCP server, skills, and hooks where available.
cd your-project
leankg init
leankg index ./src
leankg status
Optional: enable watch mode so the graph stays fresh while you and your agent edit code:
leankg mcp-stdio --watch
leankg impact src/main.rs --depth 3
leankg path "Handler" "Repository"
leankg explain "APIRouter"
leankg graph-query "what connects auth to the database?"
leankg web # UI at http://localhost:8080
Upgrade anytime:
leankg update
When an AI agent needs to understand code, it usually discovers structure the slow way: grep, glob, and Read — one file at a time — rebuilding call paths and dependencies by hand. That is a pile of tool calls and round-trips before the real work starts.
LeanKG hands the agent the exact subgraph it needs. It indexes symbols, edges, tests, docs, and (optionally) embeddings into a local knowledge graph, then exposes them over MCP. Instead of crawling the tree, the agent asks one question and gets back callers, dependents, blast radius, and targeted source — surgical context, not a file-by-file search.
graph LR
A[AI Agent] -->|intent| B[LeanKG MCP]
B --> C[Graph + Embeddings]
C -->|targeted context| A
| Without LeanKG | With LeanKG |
|---|---|
| Grep → open many files → large context | Query the graph → minimal, relevant subgraph |
| No blast-radius awareness | Impact radius with confidence + severity |
| Keyword-only search | Keyword + semantic (HNSW) + ontology |
| Stale mental model of the repo | Index + optional --watch incremental updates |
On cost: LeanKG’s win on every codebase is precision and speed — fewer tool calls, faster answers. Token savings are real and scale-dependent: modest on small repos, material on large monorepos multiplied by team-wide agent usage.
For engineering managers choosing a team-wide stack: LeanKG vs Graphify — Company ROI Brief (token/tool-call floors, multi-repo Docker TCO, mega-graph safety, ops/traceability). The primary adoption lever is always-on graph-first install (curl …/install.sh | bash -s -- cursor or claude) so agents query the graph before grep.
Vector-engine A/B gate (100 tasks, synthetic agent workload vs grep/cat-style baseline) — see docs/benchmarks/vector_engine_gate_results.json:
| Metric | Result | Floor |
|---|---|---|
| Token reduction | −65.0% | ≥ 60% |
| Tool-call reduction | −84.6% | ≥ 80% |
| Speedup | 2.50× | ≥ 2× |
| 1M SQ8 ANN P95 | ~0.055 ms | < 50 ms |
Unified agent A/B (19 cases vs grep baseline): ~30% input token savings, ~3× tokens/result efficiency.
Load test (~100K nodes):
| Operation | Throughput |
|---|---|
| Insert elements | ~57k / sec |
| Insert relationships | ~67k / sec |
| Retrieve elements | ~419k / sec |
| Cache speedup (cold → warm) | 345–461× |
cargo build --release
target/release/leankg benchmark-unified --project .
cargo bench --bench vector_engine_ab
Full methodology: docs/benchmark.md
ontology/workflows.yaml while serving; watcher re-syncs so kg_trace_workflow returns corrected steps without restartimports, calls, tested_by, http_calls, service_calls, tunnels, and more--features embeddings; included in Docker)gitnexus-web (Force / Tree / Circles, filters, search, code panel); data plane is LeanKG /api/* (ui-v2/ + leankg serve)Architecture: docs/architecture.md · MCP catalog: docs/mcp-tools.md · UI v2: ui-v2/README.md
UI v2 uses the GitNexus web exploring shell (layout modes, 3-pane chrome, Sigma) with LeanKG’s REST graph API.
UI v2 — Force layout (Sigma), filters, and status bar against leankg serve.
Tree and Circles layouts on the same subgraph.
Node select opens syntax-highlighted source via /api/file.
Header search (/api/search) and Query FAB (/api/query).
Mega-graph skip gate with “Load graph anyway”.
CodeElement nodes and typed relationships.get_impact_radius, search_code, semantic_search, get_architecture.--watch and incremental index keep code edges fresh; ontology YAML watch keeps procedural workflows aligned.Repo ──► Indexer ──► Knowledge Graph ──► MCP Tools ──► AI Agent
│ │
└─ embeddings ─┘ (optional)
| Agent | Auto-setup | Notes |
|---|---|---|
| Cursor | Yes | Per-project install; always-on graph-first rule + session hook; skill using-leankg |
| Claude Code | Yes | Plugin + full lifecycle hooks (PreToolUse nudge) |
| OpenCode | Yes | Plugin + skill |
| Gemini CLI | Yes | MCP + skill / agent docs |
| Codex / Antigravity / Kilo | Yes | MCP + skill / agent docs |
| Docker MCP HTTP | Yes | Shared RocksDB; multi-repo mounts |
curl -fsSL https://raw.githubusercontent.com/FreePeak/LeanKG/main/scripts/install.sh | bash -s -- cursor
leankg mcp-stdio --watch # local AI tools
leankg mcp-http --port 9699 # HTTP/SSE for Docker / remote
When :9699 health is OK, for fuzzy / NL / “where is X?” questions discover first — do not open with query_graph:
get_overview_context → mcp_status → concept_search → semantic_search → search_code / find_function → then connection verbs → get_context / impact / deps.
| Question type | First tools |
|---|---|
| Fuzzy / meaning / domain NL | concept_search → semantic_search → search_code |
| Exact symbol / file name | find_function / search_code / query_file |
| How A↔B? (known endpoints) | shortest_path |
| What is this known symbol? | explain_node |
| Expand subgraph after seeds | query_graph (after semantic/concept hits) |
Docker MCP: pass container project= (/workspace); override with LEANKG_MCP_PROJECT.
While mcp-http / mcp-stdio / leankg serve runs, LeanKG watches ontology/concepts.yaml and ontology/workflows.yaml, debounces (≥1s), and replaces the ontology layer in the served DB so kg_trace_workflow stays fresh without a restart.
Typical agent loop:
kg_trace_workflow returns themOntology also refreshes after index, and Docker boot re-syncs when .leankg/ontology_synced is older than either YAML file. Prefer kg_trace_workflow after edits; use ontology_control(action=sync|status) when you need an explicit refresh.
| Knob | Default | Purpose |
|---|---|---|
LEANKG_ONTOLOGY_DIR | <project>/ontology | Override ontology YAML directory |
LEANKG_ONTOLOGY_WATCH_DEBOUNCE_MS | 1500 (min 1000) | Debounce for in-process YAML watch |
LEANKG_ONTOLOGY_SYNC_ON_BOOT | timeout | Docker: skip / force / timeout |
MCP ontology_control | — | action=sync|status (Admin) |
Details: docs/mcp-tools.md · Smoke: docs/reports/ontology-proc-auto-smoke-2026-07-21.md
Setup details: docs/agentic-instructions.md · Skill: instructions/using-leankg/SKILL.md · Tool catalog: docs/mcp-tools.md
Structural extraction and cross-file edges into one graph (no per-language product setup):
| Family | Languages / formats |
|---|---|
| Systems | Rust, Go, C / C++* |
| JVM | Java, Kotlin |
| Web | TypeScript, JavaScript |
| Scripting | Python, Ruby*, PHP* |
| Mobile | Dart, Android XML |
| Infra | Terraform, CI YAML |
*Depth varies by extractor maturity — see the PRD / roadmap for parity status.
leankg init
leankg index ./src
leankg status
leankg impact <file> --depth 3
leankg path <from> <to>
leankg explain <symbol>
leankg graph-query "<question>"
leankg detect-clusters
leankg embed --init && leankg embed # needs --features embeddings
leankg web
leankg mcp-stdio --watch
leankg mcp-http --port 9699
leankg ontology sync # concepts + workflows → DB
leankg ontology trace <workflow> # ordered procedural steps
leankg update
Full CLI: docs/cli-reference.md
| Doc | Description |
|---|---|
| docs/cli-reference.md | All CLI commands |
| docs/mcp-tools.md | MCP tool reference |
| docs/agentic-instructions.md | AI tool setup & auto-trigger |
| docs/architecture.md | System design & data model |
| docs/web-ui.md | Web UI |
| docs/benchmark.md | Benchmark methodology |
| src/embeddings/EMBEDDINGS.md | Embeddings / HNSW internals |
| INSTRUCTION.md | Memory tuning & ops playbook |
| docs/roadmap.md | Roadmap |
| AGENTS.md | Agent / Docker deployment notes |
| Issue | Fix |
|---|---|
| High RAM on macOS | export LEANKG_MMAP_SIZE=134217728 and LEANKG_CACHE_MAX_TOKENS=100000 — see INSTRUCTION.md |
database is locked | leankg proc kill (stop web/MCP before re-index) |
| Embeddings / cold embed | src/embeddings/EMBEDDINGS.md |
| MCP “not initialized” in Docker | Pass container project= paths (e.g. /workspace), not the host Mac path — see AGENTS.md |
Issues and PRs are welcome. For larger changes, open an issue first so we can align on design.
docs/prd.md / task tracker as needed)cargo build --release && cargo testRun Claude Code as an MCP server so any agent can delegate coding tasks to it
Browser automation using accessibility snapshots instead of screenshots
Google's universal MCP server supporting PostgreSQL, MySQL, MongoDB, Redis, and 10+ databases
Official GitHub integration for repos, issues, PRs, and CI/CD workflows