Lean定理证明语言服务器

一个通过语言服务器协议(LSP)与Lean定理证明器交互的工具,提供丰富的Lean项目分析、诊断和搜索功能。

  • by project-numina
  • Python
  • 本地部署
  • 21 星标
  • 更新于
在 GitHub 查看 project-numina/lean-lsp-mcp

使用说明

lean-lsp-mcp

Lean Theorem Prover MCP

允许与代理交互的MCP服务器 精益定理证明器 通过 语言服务器协议 使用 精益客户端此服务器为LLM代理提供了一系列工具,用于理解、分析精益项目并与之交互。

主要特点

  • 丰富的精益互动:访问诊断、目标状态、术语信息、悬停文档等。
  • 外部搜索工具:使用 LeanSearch, Loogle, Lean Finder, Lean Hammer 和 Lean State Search 找到相关的定理和定义。
  • 轻松设置:各种客户端的简单配置,包括VSCode、Cursor和Claude Code。

设置

概述

  1. 安装 紫外线,Python包管理器。
  2. 通过运行以下命令确保您的精益项目快速构建 lake build 手动。
  3. 配置IDE/设置
  4. (可选,强烈推荐)安装 ripgrep (rg)使用本地搜索来减少幻觉。

1.安装uv

安装uv 为您的系统。在Linux/MacOS上: curl -LsSf https://astral.sh/uv/install.sh | sh

2.跑步 lake build

lean-lsp-mcp 将运行 lake serve 在项目根目录中使用语言服务器(适用于大多数工具)。在此过程中,某些客户端(例如Cursor)可能会超时。因此,建议运行 lake build 在启动MCP之前手动操作。这确保了更快的构建时间并避免了超时。

3.配置IDE/设置

VSCode (Click to expand) One-click config setup:

或者使用安装向导:

Ctrl+Shift+P>“MCP:添加服务器…”>“命令(stdio)”>“uvx-lean-lsp-MCP”>“lean-lsp”(或您喜欢的任何名称)>全局或工作区

或者通过打开手动添加配置 mcp.json 与:

Ctrl+Shift+P>“MCP:打开用户配置”

并添加以下内容

{
    "servers": {
        "lean-lsp": {
            "type": "stdio",
            "command": "uvx",
            "args": [
                "lean-lsp-mcp"
            ]
        }
    }
}

如果您在Windows上安装了VSCode,并且使用WSL2作为开发环境,则可能需要使用此配置:

{
    "servers": {
        "lean-lsp": {
            "type": "stdio",
            "command": "wsl.exe",
            "args": [
                "uvx",
                "lean-lsp-mcp"
            ]
        }
    }
}

如果这不起作用,您可以尝试克隆此存储库并替换 "lean-lsp-mcp" 随着 "/path/to/cloned/lean-lsp-mcp".

Cursor (Click to expand) 1. Open MCP Settings (File > Preferences > Cursor Settings > MCP)
  1. “+添加新的全局MCP服务器”>(“创建文件”)

  2. 将服务器配置粘贴到 mcp.json 文件:

{
    "mcpServers": {
        "lean-lsp": {
            "command": "uvx",
            "args": ["lean-lsp-mcp"]
        }
    }
}
Claude Code (Click to expand) 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

您可以找到有关Claude Code的MCP服务器配置的更多详细信息 这里.

克劳德技巧:Lean4定理证明

如果您正在使用 克劳德桌面版 或 克劳德代码,您还可以安装 Lean4定理证明技巧。此技能为与Lean4项目交互提供了额外的提示和模板,并包括与 lean-lsp-mcp 服务器。

4.安装ripgrep(可选但推荐)

对于本地搜索工具 lean_local_search,安装 ripgrep (rg)并确保它在您的PATH中可用。

MCP工具

文件交互(LSP)

lean_file_outline

获取一个精益文件的简明大纲,显示带有类型签名(定理、定义、类、结构)的导入和声明。

lean_file_contents(已停用)

获取精益文件的内容,可选地添加行号注释。

lean_diagnostic_消息

获取精益文件的所有诊断消息。这包括信息、警告和错误。

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 = ∅

...

贫煤

在精益文件中的特定位置(行或行和列)获取证明目标。

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

精益_条款_煤炭

在精益文件中的特定位置(行和列)获取术语目标。

精益管理信息

检索精益文件(特定行和列)中符号、术语和表达式的悬停信息(文档)。

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_decaration_file

获取声明符号或术语的文件内容。

精益完成

代码自动补全:在精益文件中的特定位置(行和列)查找可用标识符或导入建议。

lean_run_code

运行/编译一个独立的精益代码片段/文件,并返回结果或错误消息。

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

精益_多尝试

在一行中尝试多个精益代码片段,并为每个片段返回目标状态和诊断。 此工具可用于在使用最有希望的验证尝试之前筛选不同的验证尝试。

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

...

本地搜索工具

lean_cal_search

在本地精益项目和stdlib中搜索精益定义和定理。 这有助于确认声明确实存在,并防止API产生幻觉。

此工具需要 ripgrep (rg)在您的PATH中安装并可用。

外部搜索工具

目前,大多数外部工具都是单独的 速率限制为每30秒3个请求请不要过度使用这些令人惊叹的免费服务,破坏每个人的乐趣!

如果你使用这些工具,请引用它们的原始作者!

精益_精益搜索

在Mathlib中搜索定理 leansearch.net (自然语言搜索)。

Github仓库 | Arxiv纸

  • 支持自然语言、混合查询、概念、标识符和精益术语。
  • 例子: 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."
  },
  ...

leansearch_leandex

Example output (query by LLM: `bijective map from injective`)
  {'id': 25217,
  'primary_declaration': {'lean_name': 'Nat.add'},
  'source_file': 'Init/Prelude.lean',
  'range_start_line': 1705,
  'range_end_line': 1715,
  'display_statement_text': '。。。',
  'docstring': '...',
  'informal_description': '...'},
  ...

lean_logle

使用搜索精益定义和定理 隐约可见。Lean-lang.org.

Github仓库

  • 支持按常量、引理名称、子表达式、类型或结论进行查询。
  • 例子: 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"
  },
  ...
]

精益查找器

Mathlib定理的语义搜索 精益查找器.

Arxiv纸

  • 支持非正式描述、用户问题、证明状态和语句片段。
  • 示例: 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
Example output

查询: Does y being a root of minpoly(x) imply minpoly(x)=minpoly(y)?

  [
    [
      "/-- If `y : L` is a root of `minpoly K x`, then `minpoly K y = minpoly K x`. -/\ntheorem eq_of_root {x y : L} (hx : IsAlgebraic K x)\n    (h_ev : Polynomial.aeval y (minpoly K x) = 0) : minpoly K y = minpoly K x :=\n  ((eq_iff_aeval_minpoly_eq_zero hx.isIntegral).mpr h_ev).symm",
      
      "Let $L/K$ be a field extension, and let $x, y \\in L$ be elements such that $y$ is a root of the minimal polynomial of $x$ over $K$. If $x$ is algebraic over $K$, then the minimal polynomial of $y$ over $K$ is equal to the minimal polynomial of $x$ over $K$, i.e., $\\text{minpoly}_K(y) = \\text{minpoly}_K(x)$. This means that if $y$ satisfies the polynomial equation defined by $x$, then $y$ shares the same minimal polynomial as $x$."
    ],
    ...
  ]

lean_state_search

使用以下公式搜索当前证明目标的适用定理 premise-search.com.

Github仓库 | Arxiv纸

自托管版本是 可用的 并受到鼓励。您可以设置环境变量 LEAN_STATE_SEARCH_URL 指向您的自托管实例。它默认为 https://premise-search.com.

在给定的行和列中使用第一个目标。 返回一系列相关定理。

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

精益管理_前提

使用当前证明状态搜索相关前提 Lean Hammer房屋搜索.

Github仓库 | Arxiv纸

自托管版本是 可用的 并受到鼓励。您可以设置环境变量 LEAN_HAMMER_URL 指向您的自托管实例。它默认为 http://leanpremise.net.

在给定的行和列中使用第一个目标。 返回可用于证明目标的相关前提(定理)列表。

注:我们使用简化版本, LeanHammer 可能会有更好的前提搜索结果。

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

项目级工具

精益建筑

重建精益项目并重新启动精益LSP服务器。

禁用工具

许多客户端允许用户手动禁用特定工具(例如lean_build)。

VSCode:点击聊天中的扳手/螺丝刀图标。

光标:在“光标设置”>“MCP”中,单击工具的名称将其禁用(删除线)。

MCP配置

此MCP服务器无需任何配置即可开箱即用。但是,有一些可选设置可用。

环境变量

  • LEAN_LOG_LEVEL:服务器的日志级别。选项有“信息”、“警告”、“错误”、“无”。默认为“INFO”。
  • LEAN_PROJECT_PATH:通往精益项目根的路径。如果服务器无法自动检测到您的项目,请设置此选项。
  • LEAN_LSP_MCP_TOKEN:使用时用于承载身份验证的秘密令牌 streamable-http 或 sse 运输。
  • LEAN_STATE_SEARCH_URL:自托管的URL premise-search.com 例子
  • LEAN_HAMMER_URL:自托管的URL Lean Hammer房屋搜索 例子

您还可以在MCP客户端配置中设置这些环境变量:

VSCode mcp.json Example
{
    "servers": {
        "lean-lsp": {
            "type": "stdio",
            "command": "uvx",
            "args": [
                "lean-lsp-mcp"
            ],
            "env": {
                "LEAN_PROJECT_PATH": "/path/to/your/lean/project",
                "LEAN_LOG_LEVEL": "NONE"
            }
        }
    }
}

运输方式

精益LSP MCP服务器支持以下传输方式:

  • stdio:标准输入/输出(默认)
  • streamable-http:HTTP流媒体
  • sse:服务器发送的事件(MCP旧版,使用 streamable-http 如果可能的话)

您可以使用指定传输方法 --transport 运行服务器时的参数。对于 sse 和 streamable-http 您还可以选择指定主机和端口:

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 --host localhost --port 12345 # Available at http://localhost:12345/sse

承载令牌身份验证

运输方式 streamable-http 和 sse 支持承载令牌身份验证。这允许公开访问的MCP服务器限制对授权客户端的访问。

设置 LEAN_LSP_MCP_TOKEN 在启动服务器之前,将环境变量(或参阅第3节在MCP配置中设置环境变量)设置为秘密令牌。

Linux/MacOS设置示例:

export LEAN_LSP_MCP_TOKEN="your_secret_token"
uvx lean-lsp-mcp --transport streamable-http

然后,客户端应将令牌包含在 Authorization 头球

MCP安全注意事项

一般来说,模型上下文协议(MCP)存在许多有效的安全问题!

此MCP服务器旨在作为研究工具,目前处于测试阶段。 虽然它不处理任何敏感数据,如密码或API密钥,但它仍然包括各种安全风险:

  • 访问本地文件系统。
  • 无输入或输出验证。

请注意这些风险。请随时审核代码并报告安全问题!

有关更多信息,您可以使用 卓越的MCP安全 作为一个起点。

发展

MCP检查员

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

运行测试

uv sync --all-extras
uv run pytest tests

使用精益lsp-mcp的出版物

  • Ax证明器:数学和量子物理中定理证明的深度推理代理框架 arXiv

相关项目

许可证和引用

麻省理工学院 得到许可的。看 许可证 了解更多信息。

非常感谢引用此存储库,但许可证并不要求引用。

@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}
}