This project contains runnable demos for a lecture on programmatic interaction with Lean:
- command-line Lean and Python subprocess
- Lean metaprogramming
- Lean LSP access from Python
- MCP concepts and Lean MCP-style tool calls
The Lean/mathlib project is in:
lean_interact_demo
Install Lean using elan:
curl https://raw.githubusercontent.com/leanprover/elan/master/elan-init.sh -sSf | shRestart your shell, then check:
lean --version
lake --versionPython 3 and Jupyter are needed for the notebooks:
python3 --version
python3 -m pip install notebookClone the repository and enter the Lean project:
cd lean_interact_demoThe Lean toolchain is pinned in:
lean-toolchain
Download dependencies and build:
lake update
lake exe cache get
lake buildlake exe cache get downloads prebuilt mathlib artifacts when available, which avoids building all of mathlib locally.
cd lean_interact_demo
bash demos/01_cli/run_bash.sh
jupyter notebook demos/01_cli/lean_cli_subprocess_demo.ipynbcd lean_interact_demo
bash demos/02_metaprogramming/run.shcd lean_interact_demo
jupyter notebook demos/03_lsp/lean_lsp_demo.ipynbThis notebook starts lake serve and queries Lean LSP for diagnostics, hover, proof goals, symbols, and definitions.
cd lean_interact_demo
jupyter notebook demos/04_mcp/lean_mcp_demo.ipynbThis notebook shows MCP-style Lean tool call results, including diagnostics, hover, get-goal, try-tactic, and a candidate tactic loop.
student.md
README.md
lean_interact_demo/
lean-toolchain
lakefile.toml
lake-manifest.json
demos/
01_cli/
02_metaprogramming/
03_lsp/
04_mcp/
teacher.md is intentionally not part of the public repository.
From lean_interact_demo:
lake build
lake env lean demos/03_lsp/LspDemo.lean
lake env lean demos/04_mcp/McpDemo.lean