Lean-LSP-MCP: Bridging Lean and Agentic Reasoning
- Lean-LSP-MCP is an interface protocol that exposes Lean 4’s LSP functionalities via a standardized MCP layer for formal proofs and diagnostics.
- It integrates formal theorem proving with agentic reasoning architectures, using JSON-RPC messages to facilitate synchronous and asynchronous workflows.
- The protocol underpins autonomous agentic systems, supporting semantic search, proof orchestration, and plug-and-play tool extensibility in formal mathematics.
Lean-LSP-MCP is an interface protocol and tool suite designed to bridge formal Lean 4 theorem proving systems with agentic reasoning architectures and AI-driven workflows via the Model Context Protocol (MCP). By exposing Lean 4’s Language Server Protocol (LSP) functionalities through a standardized, extensible MCP layer, Lean-LSP-MCP enables autonomous agents and human users to orchestrate proof tasks, discover relevant declarations, and retrieve automated diagnostics using uniform, JSON-RPC 2.0 based message semantics. The protocol has become foundational in agentic mathematics systems, including Numina-Lean-Agent and LeanExplore, supporting plug-and-play extensibility and autonomous tool invocation across theorem proving, semantic search, and informal reasoning (Asher, 4 Jun 2025, Liu et al., 20 Jan 2026).
1. Architectural Role and System Context
Lean-LSP-MCP operates atop the Lean 4 Language Server Protocol and reifies its primitives into MCP methods consumable by external agents and assistants. MCP serves as a uniform RPC protocol that encodes tool invocations and responses as JSON messages over stdin/stdout or TCP, providing seamless integration for both synchronous and asynchronous workflows. In composite agentic systems such as Numina-Lean-Agent, Lean-LSP-MCP acts as the exclusive mediator for all formal interactions, abstracting away direct shell or binary invocations and enabling high-level applications (LLM agent cores, semantic search engines, informal provers) to interact with Lean and its library ecosystem via explicit MCP calls (Liu et al., 20 Jan 2026).
The protocol supports core messaging patterns—initialization, capability negotiation, structured requests (e.g., file outline, proof state, tactic execution), and robust error handling. MCP methods such as mcp/lean_goal, mcp/lean_run_code, mcp/lean_local_search, and mcp/lean_loogle abstract Lean’s internal commands into standardized, agent-consumable formats:
| MCP Method | Function | Primary Response Field |
|---|---|---|
| mcp/lean_goal | Query proof goals at a position | goals |
| mcp/lean_run_code | Execute Lean code/tactic snippet | diagnostics, result status |
| mcp/lean_local_search | Local theorem retrieval | declaration list |
| mcp/lean_loogle | Semantic search via LeanDex/Mathlib | declaration |
Through this abstraction, Lean-LSP-MCP enables orchestrated workflows where tool invocation, concurrent task attempts, and cross-tool composition (e.g., semantic search plus tactic execution) remain modular and discoverable at runtime (Liu et al., 20 Jan 2026).
2. Protocol Specification and Message Exchange
Lean-LSP-MCP strictly adheres to JSON-RPC 2.0 format for both handshake and payload transfer. The protocol initializes with a capability exchange, declaring supported MCP methods under the "experimental" section of the LSP capabilities object. An example handshake involves:
Initialization Request
1 2 3 4 5 6 7 8 9 |
{
"jsonrpc": "2.0",
"method": "initialize",
"params": {
"processId": 1234,
"rootUri": "file:///project-root",
"capabilities": { "experimental": { "mcp": true } }
}
} |
Initialization Response
1 2 3 4 5 6 7 8 9 10 |
{
"jsonrpc": "2.0",
"id": 1,
"result": {
"capabilities": {
"experimental": { "mcpMethods": ["lean_goal", "lean_run_code", ...] },
...
}
}
} |
Subsequent requests utilize method-specific parameter schemas. For instance, mcp/lean_goal requests the proof goal at a specific location:
Request
1 2 3 4 5 6 7 8 9 |
{
"jsonrpc": "2.0",
"id": 42,
"method": "mcp/lean_goal",
"params": {
"textDocument": { "uri": "file:///project/Foo.lean" },
"position": { "line": 41, "character": 4 }
}
} |
Response
1 2 3 4 5 6 7 |
{
"jsonrpc": "2.0",
"id": 42,
"result": {
"goals": ["⊢ ∑ i in finset.range (n+1), (i : ℚ)^2 = n*(n+1)*(2*n+1)/6"]
}
} |
All methods correspond one-to-one with internal Lean LSP commands, surfaced with uniform naming and standardized JSON return values, facilitating error recovery, message batching, and concurrent call handling (Liu et al., 20 Jan 2026).
3. Agentic Proof Workflow Orchestration
Lean-LSP-MCP underpins the agentic workflow loop in modern formal mathematics systems, where an LLM-based agent coordinates proof construction, search, and refinement via repeated MCP interactions. A prototypical session includes:
- File Outline Acquisition: The agent leverages
mcp/lean_file_outlineto enumerate available declarations and identify open proof holes. - Goal Inspection: Through
mcp/lean_goal, the agent analyzes the current theorem goal and extracts subgoals. - Automated Theorem Retrieval: Calls to
mcp/lean_local_searchormcp/lean_looglefetch relevant lemmas from both project-local contexts and Mathlib via LeanDex. - Proof Synthesis and Execution: Candidate tactic sequences are synthesized and trialed with
mcp/lean_run_code. Parallel strategies are attempted viamcp/lean_multi_attempt. - Informal Reasoning: Integration with external tools for informal proof steps via
mcp/informal_proof. - Iterative Diagnostics and Blueprint Refinement: Error and diagnostic feedback from Lean guides further refinement through additional MCP calls.
- Completion: Proof success marks closure of the session, encapsulating all agent-LSP interactions in MCP logs (Liu et al., 20 Jan 2026).
A plausible implication is that the agent's separation from direct Lean command invocation enhances reproducibility and traceability, as all activity is logged via structured MCP exchanges.
4. Integration with Semantic Search and MCP Tools
Lean-LSP-MCP serves as the key interface for consuming advanced retrieval and reasoning services such as LeanExplore and LeanDex. Via MCP, an agent or editor plugin can, for example, search for Lean 4 declarations semantically with a hybrid ranking engine, incorporating semantic embeddings (bge-base-en-v1.5 via FAISS), BM25+ lexical relevance, and network-centrality PageRank measures (Asher, 4 Jun 2025).
Tool calls such as leanexplore_search and leanexplore_get_decl follow the same message conventions:
leanexplore_search Example
1 2 3 4 5 6 7 8 9 |
{
"type": "tool",
"tool": "leanexplore_search",
"input": {
"query": "compactness in metric spaces",
"top_k": 5,
"filters": {"packages": ["Mathlib"]}
}
} |
1 2 3 4 5 6 7 8 9 10 11 12 |
{
"type": "tool_response",
"tool": "leanexplore_search",
"output": [
{
"id": "SG:12345",
"name": "MetricSpace.compact_iff_finite_subcover",
"docstring": "...",
"informal": "..."
}, ...
]
} |
Results may be injected into LLM prompts for in-context reasoning, supporting theorem-proving agents in autonomous lemma retrieval and application (Asher, 4 Jun 2025). The hybrid ranking scores are combined and normalized as described in the LeanExplore index pipeline, ensuring rigorous relevance.
5. Extensibility and Autonomous Tool Discovery
Lean-LSP-MCP operationalizes capability extensibility by abstracting all new functionalities as typed MCP methods. Novel tools—external theorem provers, decision procedures, or discussion partners—register as "mcp/..." methods on server initialization, and agents autonomously discover and invoke them as needed, referencing capability lists returned in the LSP handshake.
Adding a new tool does not require recompilation of Lean or retraining of the agent’s internal model. The agent recognizes new MCP methods, orchestration remains uniform, and all actions persist in reproducible JSON-RPC logs. This design sharply contrasts with monolithic or closed formal environments, where extending capability sets often necessitates refactoring deep parts of the core provers (Liu et al., 20 Jan 2026). A plausible implication is that Lean-LSP-MCP’s modularity supports future expansion into auxiliary mathematical domains with minimal engineering overhead.
6. Benchmark Performance and Empirical Evaluation
Empirical results for Lean-LSP-MCP-enabled systems demonstrate marked improvements in theorem search and proof automation. In a benchmark study over 300 AI-generated queries targeting Mathlib, LeanExplore ranked first in relevance (55.4% ± 0.7%) compared to LeanSearch (46.3%) and Moogle (12.0%). In direct agentic theorem-proving use cases, over 80% of proof obligations in a 30-theorem pilot were solved correctly, with average MCP tool-call latency around 120 ms and semantic filtering reducing candidate sets to approximately 150 items (Asher, 4 Jun 2025).
The Numina-Lean-Agent, orchestrated via MCP and incorporating Lean-LSP-MCP, solved 12/12 Putnam 2025 problems using Claude Opus 4.5 as its agent core, matching the best closed-source results (Liu et al., 20 Jan 2026). This suggests Lean-LSP-MCP facilitates competitive, reproducible, and scalable formal mathematical workflows in agentic settings.
References:
- "LeanExplore: A search engine for Lean 4 declarations" (Asher, 4 Jun 2025)
- "Numina-Lean-Agent: An Open and General Agentic Reasoning System for Formal Mathematics" (Liu et al., 20 Jan 2026)