leankg 0.19.3

Lightweight Knowledge Graph for AI-Assisted Development
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
<p align="center">
  <img src="https://www.leankg.com/icon.svg" alt="LeanKG" width="96" height="96">
</p>

<h1 align="center">LeanKG</h1>

<p align="center">
  <strong>Local-first code knowledge graph for AI coding agents</strong>
</p>

<p align="center">
  Surgical context · fewer tool calls · blast-radius awareness · 100% local
</p>

<p align="center">
  Pre-index your repo. Serve precise subgraphs over MCP to Cursor, Claude Code, OpenCode, and more — no cloud, no external database.
</p>

<p align="center">
  <a href="https://leankg.onrender.com"><strong>Live Demo →</strong></a>
  ·
  <a href="https://github.com/FreePeak/LeanKG/blob/main/docs/cli-reference.md">Docs</a>
  ·
  <a href="https://hub.docker.com/r/freepeak/leankg">Docker Hub</a>
</p>

<p align="center">
  <a href="https://github.com/FreePeak/LeanKG/blob/main/LICENSE"><img src="https://img.shields.io/badge/License-Apache_2.0-blue.svg" alt="License: Apache 2.0"></a>
  <a href="https://www.rust-lang.org/"><img src="https://img.shields.io/badge/Rust-1.75%2B-orange?logo=rust&logoColor=white" alt="Rust 1.75+"></a>
  <a href="https://crates.io/crates/leankg"><img src="https://img.shields.io/crates/v/leankg.svg" alt="crates.io"></a>
  <a href="https://hub.docker.com/r/freepeak/leankg"><img src="https://img.shields.io/docker/v/freepeak/leankg?label=docker&logo=docker" alt="Docker Hub"></a>
  <a href="https://github.com/FreePeak/LeanKG/actions"><img src="https://img.shields.io/github/actions/workflow/status/FreePeak/LeanKG/ci.yml?branch=main&label=CI" alt="CI"></a>
  <a href="https://safeskill.dev/scan/freepeak-leankg"><img src="https://img.shields.io/badge/SafeSkill-77%2F100-yellow" alt="SafeSkill"></a>
</p>

<p align="center">
  <img src="https://img.shields.io/badge/macOS-supported-blue.svg" alt="macOS">
  <img src="https://img.shields.io/badge/Linux-supported-blue.svg" alt="Linux">
  <img src="https://img.shields.io/badge/Docker-supported-blue.svg" alt="Docker">
</p>

<p align="center">
  <img src="https://img.shields.io/badge/Cursor-supported-blueviolet.svg" alt="Cursor">
  <img src="https://img.shields.io/badge/Claude_Code-supported-blueviolet.svg" alt="Claude Code">
  <img src="https://img.shields.io/badge/OpenCode-supported-blueviolet.svg" alt="OpenCode">
  <img src="https://img.shields.io/badge/Gemini-supported-blueviolet.svg" alt="Gemini">
  <img src="https://img.shields.io/badge/Codex-supported-blueviolet.svg" alt="Codex">
  <img src="https://img.shields.io/badge/Antigravity-supported-blueviolet.svg" alt="Antigravity">
  <img src="https://img.shields.io/badge/Kilo-supported-blueviolet.svg" alt="Kilo">
</p>

---

## Contents

- [Get Started]#get-started
- [Why LeanKG?]#why-leankg
- [Measured Results]#measured-results
- [Key Features]#key-features
- [Screenshots]#screenshots
- [How It Works]#how-it-works
- [MCP & Agents]#mcp--agents
- [Language Support]#language-support
- [CLI Quick Reference]#cli-quick-reference
- [Documentation]#documentation
- [Troubleshooting]#troubleshooting
- [Contributing]#contributing
- [License]#license
- [Star History]#star-history

---

## Get Started

### 1. Install the CLI

**One command** — binary, MCP wiring, and agent docs for your tool of choice:

```bash
# 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**) |

```bash
curl -fsSL https://raw.githubusercontent.com/FreePeak/LeanKG/main/scripts/install.sh | bash -s -- cursor
```

<details>
<summary>Prefer Cargo or build from source?</summary>

```bash
cargo install leankg
# or
git clone https://github.com/FreePeak/LeanKG.git && cd LeanKG && cargo build --release
```

</details>

<details>
<summary>Teams / Docker (no Rust toolchain)</summary>

```bash
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](AGENTS.md).

> Published Hub tags currently target `linux/arm64`. On `linux/amd64`, build with `docker compose -f docker-compose.rocksdb.yml up --build`.

</details>

### 2. Wire up your agent(s)

Installing the binary alone does **not** connect your agent. Run setup (or use an install target above) so MCP is registered:

```bash
leankg setup
```

This configures Cursor, Claude Code, OpenCode, Gemini, and other supported clients with LeanKG’s MCP server, skills, and hooks where available.

### 3. Index each project

```bash
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:

```bash
leankg mcp-stdio --watch
```

### 4. Ask better questions

```bash
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:

```bash
leankg update
```

---

## Why LeanKG?

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**.

```mermaid
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.

---

## Measured Results

Vector-engine A/B gate (100 tasks, synthetic agent workload vs grep/cat-style baseline) — see [`docs/benchmarks/vector_engine_gate_results.json`](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** | &lt; 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× |

```bash
cargo build --release
target/release/leankg benchmark-unified --project .
cargo bench --bench vector_engine_ab
```

Full methodology: [docs/benchmark.md](docs/benchmark.md)

---

## Key Features

- **MCP-native** — 85+ tools for search, impact, call graphs, ontology, architecture, and team knowledge
- **Procedural ontology auto-update** — edit `ontology/workflows.yaml` while serving; watcher re-syncs so `kg_trace_workflow` returns corrected steps without restart
- **Impact radius** — blast radius before you change code, with confidence and severity
- **Dependency graph**`imports`, `calls`, `tested_by`, `http_calls`, `service_calls`, tunnels, and more
- **Semantic search** — CozoDB HNSW over dense embeddings (`--features embeddings`; included in Docker)
- **Community detection** — Leiden clusters with per-cluster skill context
- **Local-first storage** — SQLite by default; RocksDB for multi-project / team deploy
- **Token-aware payloads** — targeted subgraphs + TOON responses (~40% smaller MCP payloads)
- **Team knowledge** — incidents, env conflicts, service topology, Obsidian vault sync
- **Graph export** — Mermaid, DOT, HTML, SVG, GraphML, Neo4j, portable snapshots
- **Web UI (v2)** — explorer shell adapted from [GitNexus]https://github.com/abhigyanpatwari/GitNexus `gitnexus-web` (Force / Tree / Circles, filters, search, code panel); data plane is LeanKG `/api/*` (`ui-v2/` + `leankg serve`)

Architecture: [docs/architecture.md](docs/architecture.md) · MCP catalog: [docs/mcp-tools.md](docs/mcp-tools.md) · UI v2: [ui-v2/README.md](ui-v2/README.md)

---

## Screenshots

<p align="center">
  <strong>UI v2</strong> uses the <a href="https://github.com/abhigyanpatwari/GitNexus">GitNexus</a> web exploring shell (layout modes, 3-pane chrome, Sigma) with LeanKG’s REST graph API.
</p>

<p align="center">
  <img src="docs/reports/screenshots/01-force-src.png" alt="LeanKG UI v2 Force layout" width="90%">
</p>

<p align="center">
  <em>UI v2 — Force layout (Sigma), filters, and status bar against <code>leankg serve</code>.</em>
</p>

<p align="center">
  <img src="docs/reports/screenshots/02-tree-src.png" alt="LeanKG UI v2 Tree layout" width="45%">
  &nbsp;
  <img src="docs/reports/screenshots/03-circles-src.png" alt="LeanKG UI v2 Circles layout" width="45%">
</p>

<p align="center">
  <em>Tree and Circles layouts on the same subgraph.</em>
</p>

<p align="center">
  <img src="docs/reports/screenshots/07-code-panel.png" alt="LeanKG UI v2 code panel" width="90%">
</p>

<p align="center">
  <em>Node select opens syntax-highlighted source via <code>/api/file</code>.</em>
</p>

<p align="center">
  <img src="docs/reports/screenshots/05-search.png" alt="LeanKG UI v2 header search" width="45%">
  &nbsp;
  <img src="docs/reports/screenshots/04-query-panel.png" alt="LeanKG UI v2 Query FAB" width="45%">
</p>

<p align="center">
  <em>Header search (<code>/api/search</code>) and Query FAB (<code>/api/query</code>).</em>
</p>

<p align="center">
  <img src="docs/reports/screenshots/06-mega-skip.png" alt="LeanKG UI v2 mega-graph skip gate" width="90%">
</p>

<p align="center">
  <em>Mega-graph skip gate with “Load graph anyway”.</em>
</p>

Full set: [docs/reports/ui-v2-screenshots-2026-07-20.md](docs/reports/ui-v2-screenshots-2026-07-20.md) · App notes: [ui-v2/README.md](ui-v2/README.md) · Live demo: **https://leankg.onrender.com** · Shell provenance: [GitNexus](https://github.com/abhigyanpatwari/GitNexus)
---

## How It Works

1. **Extract** — tree-sitter (and language-specific extractors) turn source into `CodeElement` nodes and typed relationships.
2. **Store** — CozoDB over SQLite (local) or RocksDB (multi-project / Docker) holds the graph + optional HNSW vectors.
3. **Serve** — MCP stdio (editor agents) or HTTP/SSE (Docker / remote) answers tools like `get_impact_radius`, `search_code`, `semantic_search`, `get_architecture`.
4. **Refresh**`--watch` and incremental index keep code edges fresh; ontology YAML watch keeps procedural workflows aligned.

```text
Repo ──► Indexer ──► Knowledge Graph ──► MCP Tools ──► AI Agent
              │              │
              └─ embeddings ─┘ (optional)
```

---

## MCP & Agents

| Agent | Auto-setup | Notes |
|-------|------------|--------|
| Cursor | Yes | Per-project install; session hook; skill `using-leankg` |
| Claude Code | Yes | Plugin + full lifecycle hooks |
| 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 |

```bash
leankg setup                 # configure MCP + hooks
leankg mcp-stdio --watch     # local AI tools
leankg mcp-http --port 9699  # HTTP/SSE for Docker / remote
```

**Agent search prefer-order** (when `:9699` healthy): `concept_search` → `semantic_search` → `search_code`, then exact tools (`get_context`, impact, deps). Docker MCP: pass container `project=` (`/workspace`); override with `LEANKG_MCP_PROJECT`.

### Procedural ontology (auto-update)

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:

1. Wrong workflow steps in YAML → `kg_trace_workflow` returns them
2. User corrects (rename / add / remove steps) and saves YAML
3. Watcher auto-syncs (YAML is source of truth — old steps disappear, no GID duplicates)
4. Next session query retrieves only the corrected ordered steps

Ontology 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](docs/mcp-tools.md) · Smoke: [docs/reports/ontology-proc-auto-smoke-2026-07-21.md](docs/reports/ontology-proc-auto-smoke-2026-07-21.md)

Setup details: [docs/agentic-instructions.md](docs/agentic-instructions.md) · Skill: [instructions/using-leankg/SKILL.md](instructions/using-leankg/SKILL.md) · Tool catalog: [docs/mcp-tools.md](docs/mcp-tools.md)

---

## Language Support

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.

---

## CLI Quick Reference

```bash
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](docs/cli-reference.md)

---

## Documentation

| Doc | Description |
|-----|-------------|
| [docs/cli-reference.md]docs/cli-reference.md | All CLI commands |
| [docs/mcp-tools.md]docs/mcp-tools.md | MCP tool reference |
| [docs/agentic-instructions.md]docs/agentic-instructions.md | AI tool setup & auto-trigger |
| [docs/architecture.md]docs/architecture.md | System design & data model |
| [docs/web-ui.md]docs/web-ui.md | Web UI |
| [docs/benchmark.md]docs/benchmark.md | Benchmark methodology |
| [src/embeddings/EMBEDDINGS.md]src/embeddings/EMBEDDINGS.md | Embeddings / HNSW internals |
| [INSTRUCTION.md]INSTRUCTION.md | Memory tuning & ops playbook |
| [docs/roadmap.md]docs/roadmap.md | Roadmap |
| [AGENTS.md]AGENTS.md | Agent / Docker deployment notes |

---

## Troubleshooting

| Issue | Fix |
|-------|-----|
| High RAM on macOS | `export LEANKG_MMAP_SIZE=134217728` and `LEANKG_CACHE_MAX_TOKENS=100000` — see [INSTRUCTION.md]INSTRUCTION.md |
| `database is locked` | `leankg proc kill` (stop web/MCP before re-index) |
| Embeddings / cold embed | [src/embeddings/EMBEDDINGS.md]src/embeddings/EMBEDDINGS.md |
| MCP “not initialized” in Docker | Pass **container** `project=` paths (e.g. `/workspace`), not the host Mac path — see [AGENTS.md]AGENTS.md |

---

## Requirements

- Rust **1.75+** (only when building from source)
- **macOS** or **Linux**
- Docker optional (recommended for teams / multi-repo)

---

## Contributing

Issues and PRs are welcome. For larger changes, open an issue first so we can align on design.

1. Fork and create a feature branch (prefer a git worktree for isolation)
2. Update docs when behavior changes (`docs/prd.md` / task tracker as needed)
3. `cargo build --release && cargo test`
4. Open a PR with a clear summary and test plan

---

## License

[Apache License 2.0](LICENSE)

---

## Star History

<a href="https://www.star-history.com/?repos=FreePeak%2FLeanKG&type=date&legend=top-left">
  <picture>
    <source media="(prefers-color-scheme: dark)" srcset="https://api.star-history.com/chart?repos=FreePeak/LeanKG&type=date&theme=dark&legend=top-left" />
    <source media="(prefers-color-scheme: light)" srcset="https://api.star-history.com/chart?repos=FreePeak/LeanKG&type=date&legend=top-left" />
    <img alt="Star History Chart" src="https://api.star-history.com/chart?repos=FreePeak/LeanKG&type=date&legend=top-left" />
  </picture>
</a>