# morluto/leantoken architecture

> Architecture diagram of the GitHub repository morluto/leantoken, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-19). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/morluto/leantoken
- Repository: https://github.com/morluto/leantoken

## Overview

LeanToken is a local code-intelligence and retrieval layer for coding agents, exposed through MCP and a CLI. The main flow is repository indexing into a local database, followed by bounded context retrieval: services assemble evidence, ranking selects fragments within a token budget, and the adapter returns results and receipts. README-described tools also support focused file/search/structure/history/JSON queries, token-savings accounting, cache management, and client setup. Several feature modules are unsampled; their presence and roles are README-documented, so their finer internal calls are not asserted.

Main entry points: [`src/services.rs`](https://github.com/morluto/leantoken/blob/main/src/services.rs) (Retrieval services), [`src/cli/mod.rs`](https://github.com/morluto/leantoken/blob/main/src/cli/mod.rs) (CLI), [`src/indexer/orchestrator.rs`](https://github.com/morluto/leantoken/blob/main/src/indexer/orchestrator.rs) (Indexing orchestration), [`src/mcp/mod.rs`](https://github.com/morluto/leantoken/blob/main/src/mcp/mod.rs) (MCP server)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_entry["Interfaces"]
  node_cli["CLI<br/>[mod.rs]"]
  node_mcp["MCP server<br/>[mod.rs]"]
  node_models["Request models<br/>[model.rs]"]
end

subgraph group_index["Indexing"]
  node_config["Configuration<br/>[config.rs]"]
  node_discovery["Repository discovery<br/>[discovery.rs]"]
  node_parser["Code parser<br/>[mod.rs]"]
  node_indexer["Indexing orchestration<br/>[orchestrator.rs]"]
end

subgraph group_retrieve["Retrieval"]
  node_services["Retrieval services<br/>[services.rs]"]
  node_context["Context pipeline<br/>[pipeline.rs]"]
  node_ranking["Evidence ranking<br/>[mod.rs]"]
  node_focused["Focused retrieval tools<br/>[execution.rs]"]
  node_history["Git history<br/>[history.rs]"]
  node_json["JSON queries<br/>[dispatch.rs]"]
  node_read["Source reading<br/>[mod.rs]"]
  node_receipts["Evidence receipts<br/>[receipt.rs]"]
  node_tokens["Token accounting<br/>[tokens.rs]"]
end

subgraph group_state["Local state"]
  node_storage[("Local index database<br/>[mod.rs]")]
  node_cache["Index cache<br/>[mod.rs]"]
end

subgraph group_admin["Setup and insight"]
  node_setup["Client setup<br/>[mod.rs]"]
  node_savings["Token savings<br/>[savings.rs]"]
end

node_agent(("Agent or user"))

node_agent -->|"requests tools"| node_mcp
node_agent -->|"runs commands"| node_cli
node_cli -->|"builds requests"| node_models
node_mcp -->|"accepts requests"| node_models
node_config -->|"sets limits"| node_discovery
node_indexer -->|"uses configuration"| node_config
node_indexer -->|"discovers files"| node_discovery
node_indexer -->|"parses source"| node_parser
node_indexer -->|"publishes index"| node_storage
node_agent -->|"starts context flow"| node_mcp
node_mcp -->|"dispatches retrieval"| node_services
node_services -->|"reads index"| node_storage
node_services -.->|"runs context"| node_context
node_context -.->|"selects evidence"| node_ranking
node_services -.->|"serves focused queries"| node_focused
node_services -.->|"serves history"| node_history
node_services -.->|"serves JSON queries"| node_json
node_services -.->|"serves source reads"| node_read
node_services -->|"records evidence"| node_receipts
node_context -->|"budgets response"| node_tokens
node_cli -.->|"configures clients"| node_setup
node_cli -->|"manages cache"| node_cache
node_cli -->|"reports savings"| node_savings

click node_cli "https://github.com/morluto/leantoken/blob/main/src/cli/mod.rs"
click node_mcp "https://github.com/morluto/leantoken/blob/main/src/mcp/mod.rs"
click node_models "https://github.com/morluto/leantoken/blob/main/src/model.rs"
click node_config "https://github.com/morluto/leantoken/blob/main/src/config.rs"
click node_discovery "https://github.com/morluto/leantoken/blob/main/src/repository/discovery.rs"
click node_parser "https://github.com/morluto/leantoken/blob/main/src/parser/mod.rs"
click node_indexer "https://github.com/morluto/leantoken/blob/main/src/indexer/orchestrator.rs"
click node_storage "https://github.com/morluto/leantoken/blob/main/src/storage/mod.rs"
click node_services "https://github.com/morluto/leantoken/blob/main/src/services.rs"
click node_context "https://github.com/morluto/leantoken/blob/main/src/services/context/pipeline.rs"
click node_ranking "https://github.com/morluto/leantoken/blob/main/src/ranking/mod.rs"
click node_focused "https://github.com/morluto/leantoken/blob/main/src/services/search/execution.rs"
click node_history "https://github.com/morluto/leantoken/blob/main/src/services/history.rs"
click node_json "https://github.com/morluto/leantoken/blob/main/src/services/json/dispatch.rs"
click node_read "https://github.com/morluto/leantoken/blob/main/src/services/read/mod.rs"
click node_receipts "https://github.com/morluto/leantoken/blob/main/src/receipt.rs"
click node_tokens "https://github.com/morluto/leantoken/blob/main/src/tokens.rs"
click node_cache "https://github.com/morluto/leantoken/blob/main/src/cache/mod.rs"
click node_setup "https://github.com/morluto/leantoken/blob/main/src/setup/mod.rs"
click node_savings "https://github.com/morluto/leantoken/blob/main/src/savings.rs"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_cli,node_mcp,node_models,node_agent toneBlue
class node_config,node_discovery,node_parser,node_indexer toneAmber
class node_services,node_context,node_ranking,node_focused,node_history,node_json,node_read,node_receipts,node_tokens toneMint
class node_storage,node_cache toneRose
class node_setup,node_savings toneIndigo
```

## Components

### Interfaces

- [CLI](https://github.com/morluto/leantoken/blob/main/src/cli/mod.rs): `src/cli/mod.rs`
- [MCP server](https://github.com/morluto/leantoken/blob/main/src/mcp/mod.rs): `src/mcp/mod.rs`
- [Request models](https://github.com/morluto/leantoken/blob/main/src/model.rs): `src/model.rs`

### Indexing

- [Configuration](https://github.com/morluto/leantoken/blob/main/src/config.rs): `src/config.rs`
- [Repository discovery](https://github.com/morluto/leantoken/blob/main/src/repository/discovery.rs): `src/repository/discovery.rs`
- [Code parser](https://github.com/morluto/leantoken/blob/main/src/parser/mod.rs): `src/parser/mod.rs`
- [Indexing orchestration](https://github.com/morluto/leantoken/blob/main/src/indexer/orchestrator.rs): `src/indexer/orchestrator.rs`

### Retrieval

- [Retrieval services](https://github.com/morluto/leantoken/blob/main/src/services.rs): `src/services.rs`
- [Context pipeline](https://github.com/morluto/leantoken/blob/main/src/services/context/pipeline.rs): `src/services/context/pipeline.rs`
- [Evidence ranking](https://github.com/morluto/leantoken/blob/main/src/ranking/mod.rs): `src/ranking/mod.rs`
- [Focused retrieval tools](https://github.com/morluto/leantoken/blob/main/src/services/search/execution.rs): `src/services/search/execution.rs`
- [Git history](https://github.com/morluto/leantoken/blob/main/src/services/history.rs): `src/services/history.rs`
- [JSON queries](https://github.com/morluto/leantoken/blob/main/src/services/json/dispatch.rs): `src/services/json/dispatch.rs`
- [Source reading](https://github.com/morluto/leantoken/blob/main/src/services/read/mod.rs): `src/services/read/mod.rs`
- [Evidence receipts](https://github.com/morluto/leantoken/blob/main/src/receipt.rs): `src/receipt.rs`
- [Token accounting](https://github.com/morluto/leantoken/blob/main/src/tokens.rs): `src/tokens.rs`

### Local state

- [Local index database](https://github.com/morluto/leantoken/blob/main/src/storage/mod.rs): `src/storage/mod.rs`
- [Index cache](https://github.com/morluto/leantoken/blob/main/src/cache/mod.rs): `src/cache/mod.rs`

### Setup and insight

- [Client setup](https://github.com/morluto/leantoken/blob/main/src/setup/mod.rs): `src/setup/mod.rs`
- [Token savings](https://github.com/morluto/leantoken/blob/main/src/savings.rs): `src/savings.rs`

### Other components

- **Agent or user**

## Connections

- Agent or user → MCP server: requests tools (evidence: `README.md`)
- Agent or user → CLI: runs commands (evidence: `README.md`)
- CLI → Request models: builds requests (evidence: `src/cli/mod.rs`)
- MCP server → Request models: accepts requests (evidence: `src/mcp/mod.rs`)
- Configuration → Repository discovery: sets limits (evidence: `src/config.rs`)
- Indexing orchestration → Configuration: uses configuration (evidence: `src/indexer/mod.rs`)
- Indexing orchestration → Repository discovery: discovers files
- Indexing orchestration → Code parser: parses source (evidence: `src/indexer/mod.rs`)
- Indexing orchestration → Local index database: publishes index (evidence: `src/indexer/mod.rs`)
- Agent or user → MCP server: starts context flow (evidence: `README.md`)
- MCP server → Retrieval services: dispatches retrieval (evidence: `src/mcp/mod.rs`)
- Retrieval services → Local index database: reads index (evidence: `src/services.rs`)
- Retrieval services → Context pipeline: runs context
- Context pipeline → Evidence ranking: selects evidence
- Retrieval services → Focused retrieval tools: serves focused queries
- Retrieval services → Git history: serves history (evidence: `src/services.rs`)
- Retrieval services → JSON queries: serves JSON queries
- Retrieval services → Source reading: serves source reads (evidence: `src/services.rs`)
- Retrieval services → Evidence receipts: records evidence (evidence: `src/services.rs`)
- Context pipeline → Token accounting: budgets response (evidence: `src/services/context/pipeline.rs`)
- CLI → Client setup: configures clients (evidence: `src/cli/mod.rs`)
- CLI → Index cache: manages cache (evidence: `src/cli/mod.rs`)
- CLI → Token savings: reports savings
