# math-ai-org/mathcode architecture

> Architecture diagram of the GitHub repository math-ai-org/mathcode, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-18). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/math-ai-org/mathcode
- Repository: https://github.com/math-ai-org/mathcode

## Overview

#### Purpose and entry points
MathCode is a distributable terminal AI assistant that turns natural-language math problems into Lean 4 theorems and attempted formal proofs. The primary invocation paths are the installed `mathcode` launcher created by setup, the bundle-local wrapper `./run`, and browser mode through `./run webui`. Outputs are intended for `LeanFormalizations/` (generated at runtime).

Grounding: `run`, `setup.sh`, `index.html`

#### Bootstrap, runtime repair, and launch boundary
`setup.sh` is the installation/control-plane entry point. It provisions `.env`, local extension directories, a user-local launcher, Lean tooling, and validates or downloads platform-specific release runtime assets. The checked-in repository is a bootstrap bundle: the principal CLI/WebUI executables and vendored runtime are expected to be restored from release archives rather than represented in this tree.

`run` is the runtime wrapper: it sources bundle configuration and establishes the intended local environment before invoking the packaged CLI or WebUI helper.

Grounding: `setup.sh`, `run`, `.env.example`

#### AI session and provider boundary
The packaged MathCode CLI owns interactive sessions, slash commands, scheduling, agent tasks, formalization/planning/proving orchestration, and optional WebUI daemon behavior. Its default model path uses Codex authentication; alternate provider routes are configured through environment variables. Provider SDKs and the core implementation are bundled into release binaries, not available as editable source here.

External boundaries include Codex/OpenAI authentication, Anthropic-compatible APIs, OpenRouter/Atlas-compatible Responses endpoints, and optional cloud-provider credential paths.

Grounding: `.env.example`, `run`, `setup.sh`

#### Formalization and proof pipeline
The central flow is: user prompt → LLM formalization into Lean theorem → strategy planning/lemma discovery → iterative Lean compilation and repair → persisted proof artifact. Optional modes expand this into multi-planner parallelism, tree-of-subgoals decomposition with stitching, and tool-using agent-mode proving.

Successful theorems may be persisted into a theorem library; conversational assumptions can be stored as compile-checked axioms; vault/graph output can feed later planning. These runtime-managed stores are outside the checked-in tree.

Grounding: `lean-workspace/MathCodeLean.lean`, `lean-workspace/lakefile.toml`, `run`

#### Lean execution environment
Lean 4 and Lake are material runtime dependencies. The project defines a dedicated workspace and toolchain pin, with setup preferring bundle-local elan tooling and only using system Lean/Lake via explicit opt-in. The workspace is the compilation boundary for generated theorem code and Mathlib-backed proof checking. Persistent REPL and LSP modes are optional accelerators around this boundary.

Grounding: `lean-workspace/lean-toolchain`, `lean-workspace/lakefile.toml`, `bin/lean-check`

#### Analysis-tool extension boundary
Python tools are auto-discoverable extensions, separate from the core bundled CLI. They support proof and library inspection—axiom checks, `sorry` analysis, proof statistics, and library search—and require Python 3.12+ only when used. The Lean-check launcher is a focused bridge for invoking Lean validation.

Grounding: `tools/axiom_checker.py`, `tools/sorry_analyzer.py`, `bin/lean-check`

#### Customization and deployment surfaces
Skills, Python tools, and manifest-based plugins are independently discoverable extension surfaces. Plugins can contribute commands, agents, hooks, MCP servers, and related behavior; therefore they form a trust and capability boundary around the core session runtime. Static browser-facing project content is separate from the local WebUI daemon.

Grounding: `skills/README.md`, `plugins/README.md`, `index.html`

Main entry points: [`setup.sh`](https://github.com/math-ai-org/mathcode/blob/main/setup.sh) (Setup controller), [`run`](https://github.com/math-ai-org/mathcode/blob/main/run) (Runtime wrapper), [`lean-workspace/lakefile.toml`](https://github.com/math-ai-org/mathcode/blob/main/lean-workspace/lakefile.toml) (Lean workspace), [`.env.example`](https://github.com/math-ai-org/mathcode/blob/main/.env.example) (Environment template)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_bootstrap["Bootstrap &amp; Launch"]
  node_setup["Setup controller<br/>shell setup<br/>[setup.sh]"]
  node_env["Environment template<br/>environment config<br/>[.env.example]"]
  node_run["Runtime wrapper<br/>shell launcher"]
  node_launcher["Installed mathcode<br/>user launcher"]
end

subgraph group_runtime["Bundled Runtime"]
  node_release_runtime["Release runtime assets<br/>packaged binaries"]
  node_session{{"MathCode session runtime<br/>interactive orchestrator"}}
  node_providers(("AI providers<br/>external AI APIs"))
  node_formalizer["Formalization &amp; planning<br/>proof orchestration"]
  node_repair["Compile &amp; repair loop<br/>iterative prover"]
  node_artifacts[("Runtime theorem stores<br/>generated artifacts")]
end

subgraph group_lean["Lean Proof Environment"]
  node_workspace["Lean workspace<br/>Lean project<br/>[lakefile.toml]"]
  node_seed_lean["Lean project module<br/>Lean source<br/>[MathCodeLean.lean]"]
  node_toolchain["Lean toolchain pin<br/>toolchain config"]
  node_lean_check["Lean-check bridge<br/>validation launcher"]
end

subgraph group_extensions["Extensions &amp; UI"]
  node_python_tools["Python analysis tools<br/>auto-discovered tools<br/>[axiom_checker.py]"]
  node_skills["Skills<br/>discovered extensions<br/>[README.md]"]
  node_plugins["Plugin manifests<br/>capability extensions<br/>[README.md]"]
  node_web_content["Browser project content<br/>static UI<br/>[index.html]"]
end

node_setup -->|"provisions"| node_env
node_setup -->|"installs"| node_launcher
node_setup -->|"validates or downloads"| node_release_runtime
node_setup -->|"provisions tooling for"| node_toolchain
node_launcher -->|"invokes"| node_run
node_run -->|"sources"| node_env
node_run -->|"launches"| node_release_runtime
node_release_runtime -->|"contains"| node_session
node_session -->|"uses"| node_providers
node_session -->|"orchestrates"| node_formalizer
node_formalizer -->|"submits theorem and proof"| node_repair
node_repair -->|"compiles in"| node_workspace
node_workspace -->|"includes"| node_seed_lean
node_workspace -->|"uses"| node_toolchain
node_lean_check -->|"validates against"| node_workspace
node_repair -->|"persists successful proofs"| node_artifacts
node_session -.->|"discovers"| node_skills
node_session -.->|"loads"| node_plugins
node_session -.->|"discovers optional tools"| node_python_tools
node_run -.->|"serves in web mode"| node_web_content

click node_setup "https://github.com/math-ai-org/mathcode/blob/main/setup.sh"
click node_env "https://github.com/math-ai-org/mathcode/blob/main/.env.example"
click node_run "https://github.com/math-ai-org/mathcode/blob/main/run"
click node_workspace "https://github.com/math-ai-org/mathcode/blob/main/lean-workspace/lakefile.toml"
click node_seed_lean "https://github.com/math-ai-org/mathcode/blob/main/lean-workspace/MathCodeLean.lean"
click node_toolchain "https://github.com/math-ai-org/mathcode/blob/main/lean-workspace/lean-toolchain"
click node_lean_check "https://github.com/math-ai-org/mathcode/blob/main/bin/lean-check"
click node_python_tools "https://github.com/math-ai-org/mathcode/blob/main/tools/axiom_checker.py"
click node_skills "https://github.com/math-ai-org/mathcode/blob/main/skills/README.md"
click node_plugins "https://github.com/math-ai-org/mathcode/blob/main/plugins/README.md"
click node_web_content "https://github.com/math-ai-org/mathcode/blob/main/index.html"

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_setup,node_env,node_run,node_launcher toneBlue
class node_release_runtime,node_session,node_providers,node_formalizer,node_repair,node_artifacts toneAmber
class node_workspace,node_seed_lean,node_toolchain,node_lean_check toneMint
class node_python_tools,node_skills,node_plugins,node_web_content toneRose
```

## Components

### Bootstrap & Launch

- [Setup controller](https://github.com/math-ai-org/mathcode/blob/main/setup.sh): shell setup · Installs launcher, tooling, configuration, and release runtime assets. · `setup.sh`
- [Environment template](https://github.com/math-ai-org/mathcode/blob/main/.env.example): environment config · Configures providers and runtime options. · `.env.example`
- [Runtime wrapper](https://github.com/math-ai-org/mathcode/blob/main/run): shell launcher · Loads the bundle environment and starts CLI or WebUI. · `run`
- **Installed mathcode**: user launcher · User-local command installed by setup.

### Bundled Runtime

Release-restored executables own the core session behavior.

- **Release runtime assets**: packaged binaries · Platform-specific CLI, WebUI, and vendored dependencies restored from releases.
- **MathCode session runtime**: interactive orchestrator · Handles commands, scheduling, agents, and formalization workflows.
- **AI providers**: external AI APIs · Codex auth is default; alternate compatible providers are environment-configured.
- **Formalization & planning**: proof orchestration · Produces Lean statements and plans, lemmas, subgoals, or agent tasks.
- **Compile & repair loop**: iterative prover · Checks generated Lean and revises attempted proofs.
- **Runtime theorem stores**: generated artifacts · Persists proofs, theorem library entries, axioms, and vault/graph data.

### Lean Proof Environment

- [Lean workspace](https://github.com/math-ai-org/mathcode/blob/main/lean-workspace/lakefile.toml): Lean project · Mathlib-backed compilation boundary for generated theorem code. · `lean-workspace/lakefile.toml`
- [Lean project module](https://github.com/math-ai-org/mathcode/blob/main/lean-workspace/MathCodeLean.lean): Lean source · `lean-workspace/MathCodeLean.lean`
- [Lean toolchain pin](https://github.com/math-ai-org/mathcode/blob/main/lean-workspace/lean-toolchain): toolchain config · `lean-workspace/lean-toolchain`
- [Lean-check bridge](https://github.com/math-ai-org/mathcode/blob/main/bin/lean-check): validation launcher · Focused launcher for Lean validation. · `bin/lean-check`

### Extensions & UI

- [Python analysis tools](https://github.com/math-ai-org/mathcode/blob/main/tools/axiom_checker.py): auto-discovered tools · Optional axiom, sorry, proof-statistics, and library-inspection extensions. · `tools/axiom_checker.py`
- [Skills](https://github.com/math-ai-org/mathcode/blob/main/skills/README.md): discovered extensions · `skills/README.md`
- [Plugin manifests](https://github.com/math-ai-org/mathcode/blob/main/plugins/README.md): capability extensions · Can contribute commands, agents, hooks, and MCP servers. · `plugins/README.md`
- [Browser project content](https://github.com/math-ai-org/mathcode/blob/main/index.html): static UI · Separate static content for browser-facing mode. · `index.html`

## Connections

- Setup controller → Environment template: provisions
- Setup controller → Installed mathcode: installs
- Setup controller → Release runtime assets: validates or downloads
- Setup controller → Lean toolchain pin: provisions tooling for
- Installed mathcode → Runtime wrapper: invokes
- Runtime wrapper → Environment template: sources
- Runtime wrapper → Release runtime assets: launches
- Release runtime assets → MathCode session runtime: contains
- MathCode session runtime → AI providers: uses
- MathCode session runtime → Formalization & planning: orchestrates
- Formalization & planning → Compile & repair loop: submits theorem and proof
- Compile & repair loop → Lean workspace: compiles in
- Lean workspace → Lean project module: includes
- Lean workspace → Lean toolchain pin: uses
- Lean-check bridge → Lean workspace: validates against
- Compile & repair loop → Runtime theorem stores: persists successful proofs
- MathCode session runtime → Skills: discovers
- MathCode session runtime → Plugin manifests: loads
- MathCode session runtime → Python analysis tools: discovers optional tools
- Runtime wrapper → Browser project content: serves in web mode
