This server enables AI agents to interact with Lean theorem prover projects through Language Server Protocol (LSP) tools and external search services.
Core Capabilities:
File Analysis & Navigation: Get file outlines with imports and declarations, retrieve diagnostic messages (errors, warnings, info) with optional filtering, find declaration source files, and access hover information with type signatures and documentation
Proof Development: View proof goals at specific positions (the most important tool), get expected types at positions, attempt multiple tactics without modifying files to compare results, and profile theorems to identify slow tactics with per-line timing data
Code Assistance: Get IDE-style autocompletions for identifiers and imports, and run independent Lean code snippets with diagnostic feedback
Search Capabilities:
Local Search: Fast verification of declarations in local project and stdlib using ripgrep (prevents hallucination)
External Search (rate limited): Natural language search (LeanSearch), type signature/pattern search (Loogle), semantic search by mathematical meaning (Lean Finder), goal-based lemma search (State Search), and premise suggestions for automation tactics (Hammer)
Project Management: Build projects with
lake buildand restart the LSP server
Key Features: REPL-based execution for ~5x faster multi-attempt, local Loogle option to avoid rate limits, multiple transport methods (stdio, streamable-http, SSE), and bearer token authentication for public servers.
Enables searching for theorems in Mathlib using natural language queries through leansearch.net, which is documented in an arXiv paper.
Provides integration with the Lean theorem prover and related tools including leanclient, LeanSearch, Loogle, Lean Finder, Lean Hammer, and Lean State Search hosted on GitHub repositories.
Distributes the lean-lsp-mcp package through PyPI for installation and management of the Lean theorem prover MCP server.
Click on "Install Server".
Wait a few minutes for the server to deploy. Once ready, it will show a "Started" state.
In the chat, type
@followed by the MCP server name and your instructions, e.g., "@Lean LSP MCPsearch for theorems about natural number addition"
That's it! The server will respond to your query, and you can continue using it as needed.
Here is a step-by-step guide with screenshots.
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.
Key Features
Rich Lean Interaction: Access diagnostics, goal states, term information, hover documentation and more.
External Search Tools: Use
LeanSearch,Loogle,Lean Finder,Lean HammerandLean State Searchto find relevant theorems and definitions.Easy Setup: Simple configuration for various clients, including VSCode, Cursor and Claude Code.
Setup
Overview
Install uv, a Python package manager.
Make sure your Lean project builds quickly by running
lake buildmanually.Configure your IDE/Setup
(Optional, highly recommended) Install ripgrep (
rg) to reduce hallucinations using local search.
1. Install uv
Install uv for your system. On Linux/MacOS: curl -LsSf https://astral.sh/uv/install.sh | sh
2. Run lake build
lean-lsp-mcp will run lake serve 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.
3. Configure your IDE/Setup
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 adding config by opening mcp.json with:
Ctrl+Shift+P > "MCP: Open User Configuration"
and adding the following
If you installed VSCode on Windows and are using WSL2 as your development environment, you may need to use this config instead:
If that doesn't work, you can try cloning this repository and replace "lean-lsp-mcp" with "/path/to/cloned/lean-lsp-mcp".
"+ Add a new global MCP Server" > ("Create File")
Paste the server config into
mcp.jsonfile:
You can find more details about MCP server configuration for Claude Code here.
Claude Skill: Lean4 Theorem Proving
If you are using Claude Desktop or Claude Code, you can also install the Lean4 Theorem Proving Skill. This skill provides additional prompts and templates for interacting with Lean4 projects and includes a section on interacting with the lean-lsp-mcp server.
4. Install ripgrep (optional but recommended)
For the local search tool lean_local_search, install ripgrep (rg) and make sure it is available in your PATH.
MCP Tools
File interactions (LSP)
lean_file_outline
Get a concise outline of a Lean file showing imports and declarations with type signatures (theorems, definitions, classes, structures).
lean_file_contents (DEPRECATED)
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.
lean_goal
Get the proof goal at a specific location (line or line & column) in a Lean file.
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).
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.
lean_multi_attempt
Attempt multiple tactics on a line and return goal state and diagnostics for each. Useful to screen different proof attempts before committing to one.
When LEAN_REPL=true, uses the REPL tactic mode for up to 5x faster execution (see Environment Variables).
lean_profile_proof
Profile a theorem to identify slow tactics. Runs lean --profile on an isolated copy of the theorem and returns per-line timing data.
Local Search Tools
lean_local_search
Search for Lean definitions and theorems in the local Lean project and stdlib. This is useful to confirm declarations actually exist and prevent hallucinating APIs.
This tool requires ripgrep (rg) to be installed and available in your PATH.
External Search Tools
Currently most external tools are separately rate limited to 3 requests per 30 seconds. Please don't ruin the fun for everyone by overusing these amazing free services!
Please cite the original authors of these tools if you use them!
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
lean_loogle
Search for Lean definitions and theorems using loogle.lean-lang.org.
Supports queries by constant, lemma name, subexpression, type, or conclusion.
Example:
Real.sin,"differ",_ * (_ ^ _),(?a -> ?b) -> List ?a -> List ?b,|- tsum _ = _ * tsum _Local mode available: Use
--loogle-localto run loogle locally (avoids rate limits, see Local Loogle section)
lean_leanfinder
Semantic search for Mathlib theorems using Lean Finder.
Supports informal descriptions, user questions, proof states, and statement fragments.
Examples:
algebraic elements x,y over K with same minimal polynomial,Does y being a root of minpoly(x) imply minpoly(x)=minpoly(y)?,⊢ |re z| ≤ ‖z‖+transform to squared norm inequality,theorem restrict Ioi: restrict Ioi e = restrict Ici e
Query: Does y being a root of minpoly(x) imply minpoly(x)=minpoly(y)?
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 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.
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 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.
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).
MCP Configuration
This MCP server works out-of-the-box without any configuration. However, a few optional settings are available.
Environment Variables
LEAN_LOG_LEVEL: Log level for the server. Options are "INFO", "WARNING", "ERROR", "NONE". Defaults to "INFO".LEAN_LOG_FILE_CONFIG: Config file path for logging, with priority overLEAN_LOG_LEVEL. If not set, logs are printed to stdout.LEAN_PROJECT_PATH: Path to your Lean project root. Set this if the server cannot automatically detect your project.LEAN_REPL: Set totrue,1, oryesto enable fast REPL-basedlean_multi_attempt(~5x faster, see REPL Setup).LEAN_REPL_PATH: Path to thereplbinary. Auto-detected from.lake/packages/repl/if not set.LEAN_REPL_TIMEOUT: Per-command timeout in seconds (default: 60).LEAN_REPL_MEM_MB: Max memory per REPL in MB (default: 8192). Only enforced on Linux/macOS.LEAN_LSP_MCP_TOKEN: Secret token for bearer authentication when usingstreamable-httporssetransport.LEAN_STATE_SEARCH_URL: URL for a self-hosted premise-search.com instance.LEAN_HAMMER_URL: URL for a self-hosted Lean Hammer Premise Search instance.LEAN_LOOGLE_LOCAL: Set totrue,1, oryesto enable local loogle (see Local Loogle section).LEAN_LOOGLE_CACHE_DIR: Override the cache directory for local loogle (default:~/.cache/lean-lsp-mcp/loogle).
You can also often set these environment variables in your MCP client configuration:
Transport Methods
The Lean LSP MCP server supports the following transport methods:
stdio: Standard input/output (default)streamable-http: HTTP streamingsse: Server-sent events (MCP legacy, usestreamable-httpif possible)
You can specify the transport method using the --transport argument when running the server. For sse and streamable-http you can also optionally specify the host and port:
Bearer Token Authentication
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:
Clients should then include the token in the Authorization header.
REPL Setup
Enable fast REPL-based lean_multi_attempt (~5x faster). Uses leanprover-community/repl tactic mode.
1. Add REPL to your Lean project's
2. Build it:
3. Enable via CLI or environment variable:
The REPL binary is auto-detected from .lake/packages/repl/. Falls back to LSP if not found.
Local Loogle
Run loogle locally to avoid the remote API's rate limit (3 req/30s). First run takes ~5-10 minutes to build; subsequent runs start in seconds.
Requirements: git, lake (elan), ~2GB disk space.
Note: Local loogle is currently only supported on Unix systems (Linux/macOS). Windows users should use WSL or the remote API.
Falls back to remote API if local loogle fails.
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.
Development
MCP Inspector
Run Tests
Publications and Formalization Projects using lean-lsp-mcp
Ax-Prover: A Deep Reasoning Agentic Framework for Theorem Proving in Mathematics and Quantum Physics arxiv
Numina-Lean-Agent: An Open and General Agentic Reasoning System for Formal Mathematics arxiv github
A Group-Theoretic Approach to Shannon Capacity of Graphs and a Limit Theorem from Lattice Packings github
Talks
lean-lsp-mcp: Tools for agentic interaction with Lean (Lean Together 2026) youtube
Related Projects
License & Citation
MIT licensed. See LICENSE for more information.
Citing this repository is highly appreciated but not required by the license.