Skip to main content

lean-lsp-mcp

Lean Theorem Prover MCP

PyPI version last update license

MCP server that allows agentic interaction with the Lean theorem prover via the Language Server Protocol using leanclient. This server provides a range of tools for LLM agents to understand, analyze and interact with Lean projects.

Currently beta testing: Please help us by submitting bug reports and feature requests!

Key Features

  • Rich Lean Interaction: Access diagnostics, goal states, term information, hover documentation and more.
  • External Search Tools: Use leansearch, loogle, and lean_state_search to find relevant theorems and definitions.
  • Easy Setup: Simple configuration for various clients, including VSCode, Cursor and Claude Code.

Setup

Overview

  1. Install uv, a Python package manager.
  2. Make sure your Lean project builds quickly by running lake build manually.
  3. Configure your IDE/Setup

1. Install uv

Install uv for your system.

E.g. on Linux/MacOS:

curl -LsSf https://astral.sh/uv/install.sh | sh

2. Run lake build

lean-lsp-mcp will run lake build in the project root to use the language server (for most tools). Some clients (e.g. Cursor) might timeout during this process. Therefore, it is recommended to run lake build manually before starting the MCP. This ensures a faster build time and avoids timeouts.

E.g. on Linux/MacOS:

cd /path/to/lean/project
lake build

Note: Your build does not necessarily need to be successful, some errors or warnings (e.g. declaration uses 'sorry') are OK.

3. a) VSCode Setup

VSCode and VSCode Insiders are supporting MCPs in agent mode. For VSCode you might have to enable Chat > Agent: Enable in the settings.

  1. One-click config setup:

Install in VS Code

Install in VS Code Insiders

OR manually add config to settings.json (global):

{
    "mcp": {
        "servers": {
            "lean-lsp": {
                "command": "uvx",
                "args": ["lean-lsp-mcp"],
                // OPTIONAL: Setting this env variable is not required.
                // It is recommended to try without it first and only set it if you run into issues.
                "env": {
                    "LEAN_PROJECT_PATH": "/path/to/lean/project"
                }
            }
        }
    }
}
  1. Click "Start" above server config, open a Lean file, change to agent mode in the chat and run e.g. "auto proof" to get started:

3. b) Cursor Setup

  1. Open MCP Settings (File > Preferences > Cursor Settings > MCP)

  2. "+ Add a new global MCP Server" > ("Create File")

  3. Paste the server config into mcp.json file:

{
    "mcpServers": {
        "lean-lsp": {
            "command": "uvx",
            "args": ["lean-lsp-mcp"],
            // OPTIONAL: Setting this env variable is not required.
            // It is recommended to try without it first and only set it if you run into issues.
            "env": {
                "LEAN_PROJECT_PATH": "/path/to/lean/project"
            }
        }
    }
}

3. c) Claude Code

Run one of these commands in the root directory of your Lean project (where lakefile.toml is located):

# Local-scoped MCP server
claude mcp add lean-lsp uvx lean-lsp-mcp

# OR project-scoped MCP server (creates or updates a .mcp.json file in the current directory)
claude mcp add lean-lsp -s project uvx lean-lsp-mcp

# OR If you run into issues with the project path (e.g. the language server directory cannot be found), you can also set it manually e.g.
claude mcp add lean-lsp uvx lean-lsp-mcp -e LEAN_PROJECT_PATH=$PWD

You can find more details about MCP server configuration for Claude Code here.

Other Setups

Other setups, such as Claude Desktop, OpenAI Agent SDK, Windsurf or Goose should work with similar configs.

Tools

Tools are currently the only way to interact with the MCP server.

File interactions (LSP)

lean_file_contents

Get the contents of a Lean file, optionally with line number annotations.

lean_diagnostic_messages

Get all diagnostic messages for a Lean file. This includes infos, warnings and errors.

Example output

l20c42-l20c46, severity: 1
simp made no progress

l21c11-l21c45, severity: 1
function expected at h_empty term has type T ∩ compl T = ∅

...

lean_goal

Get the proof goal at a specific location (line or line & column) in a Lean file.

Example output (line) Before:
S : Type u_1
inst✝¹ : Fintype S
inst✝ : Nonempty S
P : Finset (Set S)
hPP : ∀ T ∈ P, ∀ U ∈ P, T ∩ U ≠ ∅
hPS : ¬∃ T ∉ P, ∀ U ∈ P, T ∩ U ≠ ∅
compl : Set S → Set S := fun T ↦ univ \ T
hcompl : ∀ T ∈ P, compl T ∉ P
all_subsets : Finset (Set S) := Finset.univ
h_comp_in_P : ∀ T ∉ P, compl T ∈ P
h_partition : ∀ (T : Set S), T ∈ P ∨ compl T ∈ P
⊢ P.card = 2 ^ (Fintype.card S - 1)
After:
no goals

lean_term_goal

Get the term goal at a specific position (line & column) in a Lean file.

lean_hover_info

Retrieve hover information (documentation) for symbols, terms, and expressions in a Lean file (at a specific line & column).

Example output (hover info on a `sorry`) The `sorry` tactic is a temporary placeholder for an incomplete tactic proof,
closing the main goal using `exact sorry`.

This is intended for stubbing-out incomplete parts of a proof while still having a syntactically correct proof skeleton.
Lean will give a warning whenever a proof uses sorry, so you aren't likely to miss it,
but you can double check if a theorem depends on sorry by looking for sorryAx in the output
of the #print axioms my_thm command, the axiom used by the implementation of sorry.

lean_declaration_file

Get the file contents where a symbol or term is declared.

lean_completions

Code auto-completion: Find available identifiers or import suggestions at a specific position (line & column) in a Lean file.

lean_run_code

Run/compile an independent Lean code snippet/file and return the result or error message.

Example output (code snippet: `#eval 5 * 7 + 3`) l1c1-l1c6, severity: 3
38

lean_multi_attempt

Attempt multiple lean code snippets on a line and return goal state and diagnostics for each snippet. This tool is useful to screen different proof attempts before using the most promising one.

Example output (attempting `rw [Nat.pow_sub (Fintype.card_pos_of_nonempty S)]` and `by_contra h_neq`) rw [Nat.pow_sub (Fintype.card_pos_of_nonempty S)]:
S : Type u_1
inst✝¹ : Fintype S
inst✝ : Nonempty S
P : Finset (Set S)
hPP : ∀ T ∈ P, ∀ U ∈ P, T ∩ U ≠ ∅
hPS : ¬∃ T ∉ P, ∀ U ∈ P, T ∩ U ≠ ∅
⊢ P.card = 2 ^ (Fintype.card S - 1)

l14c7-l14c51, severity: 1
unknown constant 'Nat.pow_sub'

by_contra h_neq:
S : Type u_1
inst✝¹ : Fintype S
inst✝ : Nonempty S
P : Finset (Set S)
hPP : ∀ T ∈ P, ∀ U ∈ P, T ∩ U ≠ ∅
hPS : ¬∃ T ∉ P, ∀ U ∈ P, T ∩ U ≠ ∅
h_neq : ¬P.card = 2 ^ (Fintype.card S - 1)
⊢ False

...

External Search Tools

Currently all external tools are rate limited to 3 requests per 30 seconds. This will change based on provider feedback.

lean_leansearch

Search for theorems in Mathlib using leansearch.net (natural language search).

Github Repository | Arxiv Paper

  • Supports natural language, mixed queries, concepts, identifiers, and Lean terms.
  • Example: bijective map from injective, n + 1 <= m if n < m, Cauchy Schwarz, List.sum, {f : A → B} (hf : Injective f) : ∃ h, Bijective h
Example output (query by LLM: `bijective map from injective`)
  {
    "module_name": "Mathlib.Logic.Function.Basic",
    "kind": "theorem",
    "name": "Function.Bijective.injective",
    "signature": " {f : α → β} (hf : Bijective f) : Injective f",
    "type": "∀ {α : Sort u_1} {β : Sort u_2} {f : α → β}, Function.Bijective f → Function.Injective f",
    "value": ":= hf.1",
    "informal_name": "Bijectivity Implies Injectivity",
    "informal_description": "For any function $f \\colon \\alpha \\to \\beta$, if $f$ is bijective, then $f$ is injective."
  },
  ...

lean_loogle

Search for Lean definitions and theorems using loogle.lean-lang.org.

Github Repository

  • Supports queries by constant, lemma name, subexpression, type, or conclusion.
  • Example: Real.sin, "differ", _ * (_ ^ _), (?a -> ?b) -> List ?a -> List ?b, |- tsum _ = _ * tsum _
Example output (`Real.sin`)
[
  {
    "type": " (x : ℝ) : ℝ",
    "name": "Real.sin",
    "module": "Mathlib.Data.Complex.Trigonometric"
  },
  ...
]

lean_state_search

Search for applicable theorems for the current proof goal using premise-search.com.

Github Repository | Arxiv Paper

A self-hosted version is available and encouraged. You can set an environment variable LEAN_STATE_SEARCH_URL (see Setup 3. for an example) to point to your self-hosted instance. It defaults to https://premise-search.com.

Uses the first goal at a given line and column. Returns a list of relevant theorems.

Example output (line 24, column 3)
[
  {
    "name": "Nat.mul_zero",
    "formal_type": "∀ (n : Nat), n * 0 = 0",
    "module": "Init.Data.Nat.Basic"
  },
  ...
]

lean_hammer_premise

Search for relevant premises based on the current proof state using the Lean Hammer Premise Search.

Github Repository | Arxiv Paper

A self-hosted version is available and encouraged. You can set an environment variable LEAN_HAMMER_URL (see Setup 3. for an example) to point to your self-hosted instance. It defaults to http://leanpremise.net.

Uses the first goal at a given line and column. Returns a list of relevant premises (theorems) that can be used to prove the goal.

Note: We use a simplified version, LeanHammer might have better premise search results.

Example output (line 24, column 3)
[
  "MulOpposite.unop_injective",
  "MulOpposite.op_injective",
  "WellFoundedLT.induction",
  ...
]

Project-level tools

lean_build

Rebuild the Lean project and restart the Lean LSP server.

Disabling Tools

Many clients allow the user to disable specific tools manually (e.g. lean_build).

VSCode: Click on the Wrench/Screwdriver icon in the chat.

Cursor: In "Cursor Settings" > "MCP" click on the name of a tool to disable it (strikethrough).

Example Uses

Here are a few example prompts and interactions to try. All examples use VSCode (Agent Mode) and Gemini 2.5 Pro (Preview).

Use tools to assist with a proof

After installing the MCP, tools are automatically available to the agent. E.g. Open a Lean file with a sorry and run the following prompt: "Solve this sorry" The agent should use various tools such as lean_goal to understand and create a proof. You can also ask the agent to use tools explicitly, e.g. "Help me write this proof using tools." or "Use tools to analyze the goal and hover information, then write a proof."

Analyze a theorem

Open Algebra/Lie/Abelian.lean. Example prompt:

"Analyze commutative_ring_iff_abelian_lie_ring thoroughly using various tools such as goal, term goal, hover info. Explain the key proof steps in english.".

Analyzing a theorem in chat

Design proof approaches

Open an incomplete proof such as putnam 1964 b2. Example prompt:

"First analyze the problem statement by checking the goal, hover info and looking up key declarations. Next use up to three queries to leansearch to design three different approaches to solve this problem. Very concisely present each approach and its key challenge."

Designing proof approaches

Notes on MCP Security

There are many valid security concerns with the Model Context Protocol (MCP) in general!

This MCP server is meant as a research tool and is currently in beta. While it does not handle any sensitive data such as passwords or API keys, it still includes various security risks:

  • Access to your local file system.
  • No input or output validation.

Please be aware of these risks. Feel free to audit the code and report security issues!

For more information, you can use Awesome MCP Security as a starting point.

MCP Inspector

npx @modelcontextprotocol/inspector uvx --with-editable path/to/lean-lsp-mcp python -m lean_lsp_mcp.server

Related Projects

License & Citation

MIT licensed. See LICENSE for more information.

Citing this repository is highly appreciated but not required by the license.

@software{lean-lsp-mcp,
  author = {Oliver Dressler},
  title = {{Lean LSP MCP: Tools for agentic interaction with the Lean theorem prover}},
  url = {https://github.com/oOo0oOo/lean-lsp-mcp},
  month = {3},
  year = {2025}
}

Metadata

Release files for lean-lsp-mcp 0.4.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for lean-lsp-mcp 0.4.1
File Size Uploaded
lean_lsp_mcp-0.4.1.tar.gz 23.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for lean-lsp-mcp 0.4.1
File Interpreter ABI Platform
lean_lsp_mcp-0.4.1-py3-none-any.whl Python 3 none any Details

Total release size: 43.3 kB

Release files / lean_lsp_mcp-0.4.1.tar.gz

Download URL lean_lsp_mcp-0.4.1.tar.gz
Size 23.5 kB
Tags Source
SHA-256 checksum
How to use checksums
c2e4d2164e782922ea2e5fb8e0d5a4338df83658d79eb94f688149c9d34554fa
BLAKE2b-256 checksum
How to use checksums
63fe9d6091b9f3e9b840732783c8dc5bb9ac4013787a588fb3566624074982d8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.7.15

Release files / lean_lsp_mcp-0.4.1-py3-none-any.whl

Download URL lean_lsp_mcp-0.4.1-py3-none-any.whl
Size 19.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
584cb8bc816aa138a639896c0b8fca0eb657cea5b4e3b354036e4eae33dd61ec
BLAKE2b-256 checksum
How to use checksums
17b7deaf79cfcab8d0de613b15d95545a2311ffdc874c0a3b8a93be0b2e6fbb2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.7.15

Release history Release notifications | RSS feed

0.31.0

2 release files

0.30.0

2 release files

0.29.0

2 release files

0.28.1

2 release files

0.26.1

2 release files

0.25.1

2 release files

0.25.0

2 release files

0.24.0

2 release files

0.22.2

2 release files

0.22.1

2 release files

0.22.0

2 release files

0.21.2

2 release files

0.21.1

2 release files

0.21.0

2 release files

0.19.2

2 release files

0.19.1

2 release files

0.19.0

2 release files

0.17.2

2 release files

0.17.1

2 release files

0.17.0

2 release files

0.16.2

2 release files

0.14.1

2 release files

0.14.0

2 release files

0.13.2

2 release files

0.13.1

2 release files

0.13.0

2 release files

0.11.0

2 release files

0.10.3

2 release files

0.10.2

2 release files

0.10.1

2 release files

0.10.0

2 release files

0.9.1

2 release files

0.9.0

2 release files

0.8.2

2 release files

0.8.1

2 release files

0.8.0

2 release files

0.7.2

2 release files

0.7.1

2 release files

0.7.0

2 release files

0.6.4

2 release files

0.6.3

2 release files

0.6.2

2 release files

0.6.1

2 release files

0.6.0

2 release files

0.5.1

2 release files

0.5.0

2 release files

This release

0.4.1 This release

2 release files

0.4.0

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.9

2 release files

0.1.8

2 release files

0.1.7

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page