by oOo0oOo
Enables LLM agents to interact with the Lean theorem prover through the Language Server Protocol, exposing diagnostics, proof goals, hover documentation, and a suite of external search tools.
Lean Lsp Mcp provides a Model Context Protocol (MCP) server that bridges large language model agents with the Lean theorem prover. By leveraging the Language Server Protocol, it surfaces Lean's internal information—such as diagnostics, goal states, term details, and hover docs—to agents, allowing automated reasoning and proof assistance.
curl -LsSf https://astral.sh/uv/install.sh | sh
lake build
to ensure fast startup.uvx lean-lsp-mcp
stdio
transport (default) or streamable-http
/sse
if needed. Add a server entry in mcp.json
such as:
{
"servers": {
"lean-lsp": {
"type": "stdio",
"command": "uvx",
"args": ["lean-lsp-mcp"]
}
}
}
lean_goal
, lean_hover_info
, leansearch
, etc., to analyze and construct proofs.leansearch
, loogle
, lean_state_search
, and lean_hammer
for theorem and premise retrieval.stdio
(default), streamable-http
, and sse
with optional bearer‑token authentication.LEAN_PROJECT_PATH
, LEAN_STATE_SEARCH_URL
, and LEAN_HAMMER_URL
allow custom endpoints and project locations.leansearch
) or pattern searches (loogle
).lean_build
to rebuild projects and refresh the LSP server as part of automated pipelines.Q: Do I need a successful lake build
before running the server?
A: Not strictly; the build can contain warnings or sorry
placeholders. A successful build speeds up server startup.
Q: Which transport should I choose?
A: stdio
is simplest for local IDE integration. Use streamable-http
or sse
when remote or web‑based clients need HTTP access, and protect them with LEAN_LSP_MCP_TOKEN
.
Q: How are rate limits handled for external tools? A: All external search tools are limited to 3 requests per 30 seconds to respect provider quotas.
Q: Can I host my own premise‑search services?
A: Yes. Set LEAN_STATE_SEARCH_URL
or LEAN_HAMMER_URL
to point to self‑hosted instances.
Q: What IDEs are supported? A: VSCode (including Agent Mode), Cursor, Claude Code, Claude Desktop, OpenAI Agent SDK, Windsurf, Goose, and any MCP‑compatible client.
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!
leansearch
, loogle
, lean_hammer
and lean_state_search
to find relevant theorems and definitions.lake build
manually.Install uv for your system.
E.g. on Linux/MacOS:
curl -LsSf https://astral.sh/uv/install.sh | sh
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.
VSCode and VSCode Insiders are supporting MCPs in agent mode. For VSCode you might have to enable Chat > Agent: Enable
in the settings.
OR using the setup wizard:
Ctrl+Shift+P > "MCP: Add Server..." > "Command (stdio)" > "uvx lean-lsp-mcp" > "lean-lsp" (or any name you like) > Global or Workspace
OR manually add config to mcp.json
:
{
"servers": {
"lean-lsp": {
"type": "stdio",
"command": "uvx",
"args": [
"lean-lsp-mcp"
]
}
}
}
Open MCP Settings (File > Preferences > Cursor Settings > MCP)
"+ Add a new global MCP Server" > ("Create File")
Paste the server config into mcp.json
file:
{
"mcpServers": {
"lean-lsp": {
"command": "uvx",
"args": ["lean-lsp-mcp"]
}
}
}
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, such as Claude Desktop, OpenAI Agent SDK, Windsurf or Goose should work with similar configs.
The Lean LSP MCP server supports the following transport methods:
stdio
: Standard input/output (default)streamable-http
: HTTP streamingsse
: Server-sent events (MCP legacy, use streamable-http
if possible)You can specify the transport method using the --transport
argument when running the server:
uvx lean-lsp-mcp --transport stdio # Default transport
uvx lean-lsp-mcp --transport streamable-http # Available at http://127.0.0.1:8000/mcp
uvx lean-lsp-mcp --transport sse # Available at http://127.0.0.1:8000/sse
Transport via streamable-http
and sse
supports bearer token authentication. This allows publicly accessible MCP servers to restrict access to authorized clients.
Set the LEAN_LSP_MCP_TOKEN
environment variable (or see section 3 for setting env variables in MCP config) to a secret token before starting the server.
Example Linux/MacOS setup:
export LEAN_LSP_MCP_TOKEN="your_secret_token"
uvx lean-lsp-mcp --transport streamable-http
Clients should then include the token in the Authorization
header.
Some (optional) features and integrations of lean-lsp-mcp
are configured using environment variables. These must be set in your shell or process environment before running the server.
LEAN_PROJECT_PATH
: (optional) Path to your Lean project root. Set this if the server cannot automatically detect your project.LEAN_LSP_MCP_TOKEN
: (optional) Secret token for bearer authentication when using streamable-http
or sse
transport.LEAN_STATE_SEARCH_URL
: (optional) URL for a self-hosted premise-search.com instance.LEAN_HAMMER_URL
: (optional) URL for a self-hosted Lean Hammer Premise Search instance.You can also often set these environment variables in your MCP client configuration:
{
"servers": {
"lean-lsp": {
"type": "stdio",
"command": "uvx",
"args": [
"lean-lsp-mcp"
],
"env": {
"LEAN_PROJECT_PATH": "/path/to/your/lean/project",
}
}
}
}
Tools are currently the only way to interact with the MCP server.
Get the contents of a Lean file, optionally with line number annotations.
Get all diagnostic messages for a Lean file. This includes infos, warnings and errors.
l20c42-l20c46, severity: 1 simp made no progress
l21c11-l21c45, severity: 1 function expected at h_empty term has type T ∩ compl T = ∅
...
Get the proof goal at a specific location (line or line & column) in a Lean file.
Get the term goal at a specific position (line & column) in a Lean file.
Retrieve hover information (documentation) for symbols, terms, and expressions in a Lean file (at a specific line & column).
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
.
Get the file contents where a symbol or term is declared.
Code auto-completion: Find available identifiers or import suggestions at a specific position (line & column) in a Lean file.
Run/compile an independent Lean code snippet/file and return the result or error message.
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.
Currently all external tools are rate limited to 3 requests per 30 seconds. This will change based on provider feedback.
Search for theorems in Mathlib using leansearch.net (natural language search).
Github Repository | Arxiv Paper
bijective map from injective
, n + 1 <= m if n < m
, Cauchy Schwarz
, List.sum
, {f : A → B} (hf : Injective f) : ∃ h, Bijective h
{
"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."
},
...
Search for Lean definitions and theorems using loogle.lean-lang.org.
Real.sin
, "differ"
, _ * (_ ^ _)
, (?a -> ?b) -> List ?a -> List ?b
, |- tsum _ = _ * tsum _
[
{
"type": " (x : ℝ) : ℝ",
"name": "Real.sin",
"module": "Mathlib.Data.Complex.Trigonometric"
},
...
]
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
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.
[
{
"name": "Nat.mul_zero",
"formal_type": "∀ (n : Nat), n * 0 = 0",
"module": "Init.Data.Nat.Basic"
},
...
]
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
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.
[
"MulOpposite.unop_injective",
"MulOpposite.op_injective",
"WellFoundedLT.induction",
...
]
Rebuild the Lean project and restart the Lean LSP server.
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).
Here are a few example prompts and interactions to try. All examples use VSCode (Agent Mode) and Gemini 2.5 Pro (Preview).
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."
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.".
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."
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:
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.
npx @modelcontextprotocol/inspector uvx --with-editable path/to/lean-lsp-mcp python -m lean_lsp_mcp.server
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}
}
Please log in to share your review and rating for this MCP.
Explore related MCPs that share similar capabilities and solve comparable challenges
by zed-industries
A high‑performance, multiplayer code editor designed for speed and collaboration.
by modelcontextprotocol
Model Context Protocol Servers
by modelcontextprotocol
A Model Context Protocol server for Git repository interaction and automation.
by modelcontextprotocol
A Model Context Protocol server that provides time and timezone conversion capabilities.
by cline
An autonomous coding assistant that can create and edit files, execute terminal commands, and interact with a browser directly from your IDE, operating step‑by‑step with explicit user permission.
by continuedev
Enables faster shipping of code by integrating continuous AI agents across IDEs, terminals, and CI pipelines, offering chat, edit, autocomplete, and customizable agent workflows.
by upstash
Provides up-to-date, version‑specific library documentation and code examples directly inside LLM prompts, eliminating outdated information and hallucinated APIs.
by github
Connects AI tools directly to GitHub, enabling natural‑language interactions for repository browsing, issue and pull‑request management, CI/CD monitoring, code‑security analysis, and team collaboration.
by daytonaio
Provides a secure, elastic infrastructure that creates isolated sandboxes for running AI‑generated code with sub‑90 ms startup, unlimited persistence, and OCI/Docker compatibility.