MCP server and client for Lean 4 + Mathlib formal verification
Project description
lean-mcp
MCP server and client for Lean 4 + Mathlib formal theorem verification.
Exposes the Lean 4 compiler as an MCP tool (verify_lean_theorem) via stdio transport, allowing any MCP-compatible AI client (OpenCode, Claude Desktop, Cursor, etc.) to verify mathematical proofs.
Prerequisites
- Lean 4 installed via Elan
- A Lean 4 project with Mathlib dependency (with
lake buildcompleted) - Python 3.10+
Installation
pip install lean-mcp
Or install from source:
git clone https://github.com/KrystianYCSilva/lean-mcp.git
cd lean-mcp
pip install -e .
Quick Start
Server
Run the MCP server (stdio transport):
LEAN_PROJECT_PATH=/path/to/your/lean4/project lean-mcp-server
Client (Programmatic)
import asyncio
from lean_mcp.client import LeanMCPClient
async def main():
async with LeanMCPClient() as client:
await client.connect(lean_project_path="/path/to/lean4/project")
result = await client.verify(
"theorem modus_ponens (P Q : Prop) (hp : P) (hpq : P -> Q) : Q := hpq hp"
)
print(result) # [QED] Compilacao bem-sucedida. Nenhum erro. Prova completa.
asyncio.run(main())
Client Configuration (Claude Desktop, OpenCode, etc.)
Add to your MCP client configuration:
{
"mcpServers": {
"lean-mcp": {
"command": "lean-mcp-server",
"env": {
"LEAN_PROJECT_PATH": "/absolute/path/to/lean4/project"
}
}
}
}
Tool: verify_lean_theorem
| Parameter | Type | Required | Description |
|---|---|---|---|
lean_code |
string | yes | Lean 4 code to compile. If no import is present, import Mathlib is prepended automatically. |
timeout |
int | no | Compilation timeout in seconds (default: 120). |
Return Values
| Prefix | Meaning |
|---|---|
[QED] |
Proof compiles successfully (returncode 0, no output). |
[FEEDBACK DO COMPILADOR] |
Compilation error — includes compiler output for debugging. |
[ERRO_SISTEMA] |
Infrastructure failure (timeout, lake not found, etc.). |
Architecture
MCP Client (any)
|
| stdio (JSON-RPC)
v
lean_mcp/server.py Tool: verify_lean_theorem
|
| subprocess (lake env lean)
v
Lean 4 + Mathlib Formal verifier
Environment Variables
| Variable | Required | Description |
|---|---|---|
LEAN_PROJECT_PATH |
yes | Absolute path to the Lean 4 project directory containing lakefile.toml. |
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file lean_mcp-0.1.1.tar.gz.
File metadata
- Download URL: lean_mcp-0.1.1.tar.gz
- Upload date:
- Size: 5.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3dc3a0507c41a6a49d8c96f3da53ef6cb99fbeba313c55bf8c1f82878b2bd668
|
|
| MD5 |
b5d29c7038b8a888f4076531843368dd
|
|
| BLAKE2b-256 |
490c3844d7644ad706b6afda401e5bb3d771d3f486822cdb0d3ce1082ac2620e
|
File details
Details for the file lean_mcp-0.1.1-py3-none-any.whl.
File metadata
- Download URL: lean_mcp-0.1.1-py3-none-any.whl
- Upload date:
- Size: 7.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1999cc9829d800f9f7ce9838556a15bf1bc42d0e53a70bd495d278b1ce5d419a
|
|
| MD5 |
af826aab43d5f66ad83083b4a7fc5064
|
|
| BLAKE2b-256 |
b8565d616645e05b4f6a48213275da0ee79bed1ff8ccbcc3c06c9ddd8e67e7e1
|