leanback 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- leanback-0.1.0/.gitignore +39 -0
- leanback-0.1.0/LICENSE +191 -0
- leanback-0.1.0/PKG-INFO +154 -0
- leanback-0.1.0/README.md +135 -0
- leanback-0.1.0/pyproject.toml +109 -0
- leanback-0.1.0/src/leanback/README.md +19 -0
- leanback-0.1.0/src/leanback/__init__.py +3 -0
- leanback-0.1.0/src/leanback/check.py +365 -0
- leanback-0.1.0/src/leanback/common/__init__.py +0 -0
- leanback-0.1.0/src/leanback/common/env.py +91 -0
- leanback-0.1.0/src/leanback/common/errors.py +517 -0
- leanback-0.1.0/src/leanback/common/path_manager.py +191 -0
- leanback-0.1.0/src/leanback/common/project_context.py +276 -0
- leanback-0.1.0/src/leanback/common/util.py +72 -0
- leanback-0.1.0/src/leanback/goals.py +167 -0
- leanback-0.1.0/src/leanback/mathlib_search.py +190 -0
- leanback-0.1.0/src/leanback/mathlib_search_simple.py +221 -0
- leanback-0.1.0/src/leanback/mcp_server.py +1179 -0
- leanback-0.1.0/src/leanback/mcp_test.py +496 -0
- leanback-0.1.0/src/leanback/prove.py +87 -0
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# Python
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[cod]
|
|
4
|
+
*.egg-info/
|
|
5
|
+
*.egg
|
|
6
|
+
dist/
|
|
7
|
+
build/
|
|
8
|
+
.eggs/
|
|
9
|
+
|
|
10
|
+
# Virtual environments
|
|
11
|
+
.venv/
|
|
12
|
+
venv/
|
|
13
|
+
|
|
14
|
+
# Lean
|
|
15
|
+
**/.lake/
|
|
16
|
+
**/lake-packages/
|
|
17
|
+
*.olean
|
|
18
|
+
*.ilean
|
|
19
|
+
*.trace
|
|
20
|
+
|
|
21
|
+
# IDE
|
|
22
|
+
.vscode/
|
|
23
|
+
.idea/
|
|
24
|
+
*.swp
|
|
25
|
+
*.swo
|
|
26
|
+
|
|
27
|
+
# System
|
|
28
|
+
.DS_Store
|
|
29
|
+
._*
|
|
30
|
+
|
|
31
|
+
# Temporary
|
|
32
|
+
*.tmp
|
|
33
|
+
*.log
|
|
34
|
+
*~
|
|
35
|
+
.#*
|
|
36
|
+
|
|
37
|
+
# Caches
|
|
38
|
+
.ruff_cache/
|
|
39
|
+
.pytest_cache/
|
leanback-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to the Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by the Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding any notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
Copyright 2025 Stefan Szeider
|
|
180
|
+
|
|
181
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
182
|
+
you may not use this file except in compliance with the License.
|
|
183
|
+
You may obtain a copy of the License at
|
|
184
|
+
|
|
185
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
186
|
+
|
|
187
|
+
Unless required by applicable law or agreed to in writing, software
|
|
188
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
189
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
190
|
+
See the License for the specific language governing permissions and
|
|
191
|
+
limitations under the License.
|
leanback-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: leanback
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: MCP server for Lean 4 theorem proving — check, prove, goals, eval, search
|
|
5
|
+
Author: Stefan Szeider
|
|
6
|
+
License: Apache-2.0
|
|
7
|
+
License-File: LICENSE
|
|
8
|
+
Classifier: Development Status :: 4 - Beta
|
|
9
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
14
|
+
Classifier: Topic :: Scientific/Engineering :: Mathematics
|
|
15
|
+
Requires-Python: >=3.11
|
|
16
|
+
Requires-Dist: mcp>=1.9.4
|
|
17
|
+
Requires-Dist: rich>=14.0.0
|
|
18
|
+
Description-Content-Type: text/markdown
|
|
19
|
+
|
|
20
|
+
# LeanBack
|
|
21
|
+
|
|
22
|
+
*Lean back and let the AI agent write the proof.*
|
|
23
|
+
|
|
24
|
+
Lightweight MCP server for Lean 4 theorem proving. Exposes six tools — **project_info**, **check**, **prove**, **goals**, **eval**, **search** — via the [Model Context Protocol](https://modelcontextprotocol.io), enabling AI assistants to verify proofs, inspect goal states, evaluate expressions, and search Mathlib.
|
|
25
|
+
|
|
26
|
+
## Prerequisites
|
|
27
|
+
|
|
28
|
+
- **Lean 4** with [elan](https://github.com/leanprover/elan) (the Lean version manager)
|
|
29
|
+
- At least one Lean project (see [Project Setup](#project-setup))
|
|
30
|
+
|
|
31
|
+
## Installation
|
|
32
|
+
|
|
33
|
+
### Claude Code
|
|
34
|
+
|
|
35
|
+
Simply run:
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
claude mcp add leanback -- uvx leanback
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
No installation required — `uvx` automatically downloads and runs LeanBack in an isolated environment.
|
|
42
|
+
|
|
43
|
+
### Other MCP Clients
|
|
44
|
+
|
|
45
|
+
Any MCP-compatible client can connect. Run `uvx leanback` as a stdio MCP server, or use `--transport sse --port 3000` for HTTP.
|
|
46
|
+
|
|
47
|
+
### Manual Install (optional)
|
|
48
|
+
|
|
49
|
+
If you prefer a permanent installation:
|
|
50
|
+
|
|
51
|
+
```bash
|
|
52
|
+
uv tool install leanback
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## Project Setup
|
|
56
|
+
|
|
57
|
+
LeanBack works with any Lean project on disk — just pass its absolute path. To create a new project with Mathlib:
|
|
58
|
+
|
|
59
|
+
```bash
|
|
60
|
+
cd /path/to/parent/directory
|
|
61
|
+
lake +leanprover-community/mathlib4:lean-toolchain new myproject math
|
|
62
|
+
cd myproject
|
|
63
|
+
lake exe cache get
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
This takes a few minutes (downloading precompiled Mathlib). Once done, the project is ready to use.
|
|
67
|
+
|
|
68
|
+
You can also call `project_info(project="/path/to/myproject")` to check if a project is properly set up, or to get the exact shell commands to create one.
|
|
69
|
+
|
|
70
|
+
## Tools
|
|
71
|
+
|
|
72
|
+
All tools require `project` as an absolute path (e.g., `"/home/user/lean/myproject"`).
|
|
73
|
+
|
|
74
|
+
### project_info
|
|
75
|
+
|
|
76
|
+
Check project status and get setup instructions. Call this first.
|
|
77
|
+
|
|
78
|
+
```
|
|
79
|
+
project_info(project="/home/user/lean/myproject")
|
|
80
|
+
→ { "valid": true, "lean_version": "...", "has_mathlib": true, "built": true }
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
If the path doesn't exist, returns setup commands to create the project.
|
|
84
|
+
|
|
85
|
+
### check
|
|
86
|
+
|
|
87
|
+
Verify Lean code correctness — entire projects, specific files, or expressions.
|
|
88
|
+
|
|
89
|
+
```
|
|
90
|
+
check(project="/home/user/lean/myproject")
|
|
91
|
+
check(project="/home/user/lean/myproject", file="MyProof.lean")
|
|
92
|
+
check(project="/home/user/lean/myproject", expr="#check Nat.add", imports=["Mathlib.Tactic"])
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
### prove
|
|
96
|
+
|
|
97
|
+
Verify theorem proofs with tactics, or check proof files.
|
|
98
|
+
|
|
99
|
+
```
|
|
100
|
+
prove(project="/home/user/lean/myproject", theorem="2 + 2 = 4", tactics=["rfl"])
|
|
101
|
+
prove(project="/home/user/lean/myproject", theorem="∀ n : Nat, n + 0 = n", tactics=["intro n", "rfl"])
|
|
102
|
+
prove(project="/home/user/lean/myproject", theorem="∃ x : Nat, x > 0", tactics=["use 1", "simp"], mathlib=True)
|
|
103
|
+
prove(project="/home/user/lean/myproject", file="MyTheorems.lean")
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
### goals
|
|
107
|
+
|
|
108
|
+
Inspect the proof state during proof construction. See what goals remain after applying tactics.
|
|
109
|
+
|
|
110
|
+
```
|
|
111
|
+
goals(project="/home/user/lean/myproject", theorem="∀ n : Nat, n + 0 = n")
|
|
112
|
+
goals(project="/home/user/lean/myproject", theorem="∀ n : Nat, n + 0 = n", tactics=["intro n"])
|
|
113
|
+
goals(project="/home/user/lean/myproject", theorem="True ∧ True", tactics=["constructor"])
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
### eval
|
|
117
|
+
|
|
118
|
+
Execute Lean expressions and see results.
|
|
119
|
+
|
|
120
|
+
```
|
|
121
|
+
eval(project="/home/user/lean/myproject", code="#eval 2 + 2")
|
|
122
|
+
eval(project="/home/user/lean/myproject", code="#check List.map")
|
|
123
|
+
eval(project="/home/user/lean/myproject", code="#eval Nat.gcd 12 18", mathlib=True)
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
### search
|
|
127
|
+
|
|
128
|
+
Find declarations in Mathlib by pattern.
|
|
129
|
+
|
|
130
|
+
```
|
|
131
|
+
search(project="/home/user/lean/myproject", pattern="add_comm")
|
|
132
|
+
search(project="/home/user/lean/myproject", pattern="reverse", filter_namespace="List")
|
|
133
|
+
search(project="/home/user/lean/myproject", batch="add_assoc,mul_assoc,add_comm")
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
## All Tools Are Read-Only
|
|
137
|
+
|
|
138
|
+
LeanBack never modifies files. It only reads project structure and runs Lean to check/evaluate code. File creation and editing is left to your agent's own tools.
|
|
139
|
+
|
|
140
|
+
## Response Format
|
|
141
|
+
|
|
142
|
+
All tools return JSON with a `success` (or `valid`) boolean. On failure, an `error_code` and `message` explain what went wrong. Detailed response schemas are documented in each tool's description (visible to your AI assistant via MCP).
|
|
143
|
+
|
|
144
|
+
## Testing
|
|
145
|
+
|
|
146
|
+
```bash
|
|
147
|
+
leanback-test --tool-descriptions
|
|
148
|
+
leanback-test --project /path/to/myproject
|
|
149
|
+
leanback-test --project /path/to/myproject --tool check
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
## License
|
|
153
|
+
|
|
154
|
+
Apache 2.0 — see [LICENSE](LICENSE).
|
leanback-0.1.0/README.md
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
# LeanBack
|
|
2
|
+
|
|
3
|
+
*Lean back and let the AI agent write the proof.*
|
|
4
|
+
|
|
5
|
+
Lightweight MCP server for Lean 4 theorem proving. Exposes six tools — **project_info**, **check**, **prove**, **goals**, **eval**, **search** — via the [Model Context Protocol](https://modelcontextprotocol.io), enabling AI assistants to verify proofs, inspect goal states, evaluate expressions, and search Mathlib.
|
|
6
|
+
|
|
7
|
+
## Prerequisites
|
|
8
|
+
|
|
9
|
+
- **Lean 4** with [elan](https://github.com/leanprover/elan) (the Lean version manager)
|
|
10
|
+
- At least one Lean project (see [Project Setup](#project-setup))
|
|
11
|
+
|
|
12
|
+
## Installation
|
|
13
|
+
|
|
14
|
+
### Claude Code
|
|
15
|
+
|
|
16
|
+
Simply run:
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
claude mcp add leanback -- uvx leanback
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
No installation required — `uvx` automatically downloads and runs LeanBack in an isolated environment.
|
|
23
|
+
|
|
24
|
+
### Other MCP Clients
|
|
25
|
+
|
|
26
|
+
Any MCP-compatible client can connect. Run `uvx leanback` as a stdio MCP server, or use `--transport sse --port 3000` for HTTP.
|
|
27
|
+
|
|
28
|
+
### Manual Install (optional)
|
|
29
|
+
|
|
30
|
+
If you prefer a permanent installation:
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
uv tool install leanback
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## Project Setup
|
|
37
|
+
|
|
38
|
+
LeanBack works with any Lean project on disk — just pass its absolute path. To create a new project with Mathlib:
|
|
39
|
+
|
|
40
|
+
```bash
|
|
41
|
+
cd /path/to/parent/directory
|
|
42
|
+
lake +leanprover-community/mathlib4:lean-toolchain new myproject math
|
|
43
|
+
cd myproject
|
|
44
|
+
lake exe cache get
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
This takes a few minutes (downloading precompiled Mathlib). Once done, the project is ready to use.
|
|
48
|
+
|
|
49
|
+
You can also call `project_info(project="/path/to/myproject")` to check if a project is properly set up, or to get the exact shell commands to create one.
|
|
50
|
+
|
|
51
|
+
## Tools
|
|
52
|
+
|
|
53
|
+
All tools require `project` as an absolute path (e.g., `"/home/user/lean/myproject"`).
|
|
54
|
+
|
|
55
|
+
### project_info
|
|
56
|
+
|
|
57
|
+
Check project status and get setup instructions. Call this first.
|
|
58
|
+
|
|
59
|
+
```
|
|
60
|
+
project_info(project="/home/user/lean/myproject")
|
|
61
|
+
→ { "valid": true, "lean_version": "...", "has_mathlib": true, "built": true }
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
If the path doesn't exist, returns setup commands to create the project.
|
|
65
|
+
|
|
66
|
+
### check
|
|
67
|
+
|
|
68
|
+
Verify Lean code correctness — entire projects, specific files, or expressions.
|
|
69
|
+
|
|
70
|
+
```
|
|
71
|
+
check(project="/home/user/lean/myproject")
|
|
72
|
+
check(project="/home/user/lean/myproject", file="MyProof.lean")
|
|
73
|
+
check(project="/home/user/lean/myproject", expr="#check Nat.add", imports=["Mathlib.Tactic"])
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
### prove
|
|
77
|
+
|
|
78
|
+
Verify theorem proofs with tactics, or check proof files.
|
|
79
|
+
|
|
80
|
+
```
|
|
81
|
+
prove(project="/home/user/lean/myproject", theorem="2 + 2 = 4", tactics=["rfl"])
|
|
82
|
+
prove(project="/home/user/lean/myproject", theorem="∀ n : Nat, n + 0 = n", tactics=["intro n", "rfl"])
|
|
83
|
+
prove(project="/home/user/lean/myproject", theorem="∃ x : Nat, x > 0", tactics=["use 1", "simp"], mathlib=True)
|
|
84
|
+
prove(project="/home/user/lean/myproject", file="MyTheorems.lean")
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
### goals
|
|
88
|
+
|
|
89
|
+
Inspect the proof state during proof construction. See what goals remain after applying tactics.
|
|
90
|
+
|
|
91
|
+
```
|
|
92
|
+
goals(project="/home/user/lean/myproject", theorem="∀ n : Nat, n + 0 = n")
|
|
93
|
+
goals(project="/home/user/lean/myproject", theorem="∀ n : Nat, n + 0 = n", tactics=["intro n"])
|
|
94
|
+
goals(project="/home/user/lean/myproject", theorem="True ∧ True", tactics=["constructor"])
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
### eval
|
|
98
|
+
|
|
99
|
+
Execute Lean expressions and see results.
|
|
100
|
+
|
|
101
|
+
```
|
|
102
|
+
eval(project="/home/user/lean/myproject", code="#eval 2 + 2")
|
|
103
|
+
eval(project="/home/user/lean/myproject", code="#check List.map")
|
|
104
|
+
eval(project="/home/user/lean/myproject", code="#eval Nat.gcd 12 18", mathlib=True)
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
### search
|
|
108
|
+
|
|
109
|
+
Find declarations in Mathlib by pattern.
|
|
110
|
+
|
|
111
|
+
```
|
|
112
|
+
search(project="/home/user/lean/myproject", pattern="add_comm")
|
|
113
|
+
search(project="/home/user/lean/myproject", pattern="reverse", filter_namespace="List")
|
|
114
|
+
search(project="/home/user/lean/myproject", batch="add_assoc,mul_assoc,add_comm")
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
## All Tools Are Read-Only
|
|
118
|
+
|
|
119
|
+
LeanBack never modifies files. It only reads project structure and runs Lean to check/evaluate code. File creation and editing is left to your agent's own tools.
|
|
120
|
+
|
|
121
|
+
## Response Format
|
|
122
|
+
|
|
123
|
+
All tools return JSON with a `success` (or `valid`) boolean. On failure, an `error_code` and `message` explain what went wrong. Detailed response schemas are documented in each tool's description (visible to your AI assistant via MCP).
|
|
124
|
+
|
|
125
|
+
## Testing
|
|
126
|
+
|
|
127
|
+
```bash
|
|
128
|
+
leanback-test --tool-descriptions
|
|
129
|
+
leanback-test --project /path/to/myproject
|
|
130
|
+
leanback-test --project /path/to/myproject --tool check
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
## License
|
|
134
|
+
|
|
135
|
+
Apache 2.0 — see [LICENSE](LICENSE).
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling>=1.25.0"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "leanback"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "MCP server for Lean 4 theorem proving — check, prove, goals, eval, search"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = { text = "Apache-2.0" }
|
|
11
|
+
authors = [{ name = "Stefan Szeider" }]
|
|
12
|
+
requires-python = ">=3.11"
|
|
13
|
+
classifiers = [
|
|
14
|
+
"Development Status :: 4 - Beta",
|
|
15
|
+
"License :: OSI Approved :: Apache Software License",
|
|
16
|
+
"Programming Language :: Python :: 3",
|
|
17
|
+
"Programming Language :: Python :: 3.11",
|
|
18
|
+
"Programming Language :: Python :: 3.12",
|
|
19
|
+
"Programming Language :: Python :: 3.13",
|
|
20
|
+
"Topic :: Scientific/Engineering :: Mathematics",
|
|
21
|
+
]
|
|
22
|
+
dependencies = [
|
|
23
|
+
"rich>=14.0.0",
|
|
24
|
+
"mcp>=1.9.4",
|
|
25
|
+
]
|
|
26
|
+
|
|
27
|
+
[dependency-groups]
|
|
28
|
+
dev = [
|
|
29
|
+
"pytest>=8.4.0",
|
|
30
|
+
"ruff>=0.11.0",
|
|
31
|
+
"mypy>=1.16.0",
|
|
32
|
+
"coverage>=7.8.0",
|
|
33
|
+
]
|
|
34
|
+
|
|
35
|
+
[tool.ruff]
|
|
36
|
+
# Usage:
|
|
37
|
+
# Format code: uv run ruff format .
|
|
38
|
+
# Check code: uv run ruff check .
|
|
39
|
+
# Fix issues: uv run ruff check --fix .
|
|
40
|
+
|
|
41
|
+
# Python 3.11+
|
|
42
|
+
target-version = "py311"
|
|
43
|
+
|
|
44
|
+
# Black-compatible
|
|
45
|
+
line-length = 88
|
|
46
|
+
|
|
47
|
+
# Auto-fix issues
|
|
48
|
+
fix = true
|
|
49
|
+
|
|
50
|
+
[tool.ruff.format]
|
|
51
|
+
# Black-compatible formatting
|
|
52
|
+
quote-style = "double"
|
|
53
|
+
indent-style = "space"
|
|
54
|
+
docstring-code-format = true
|
|
55
|
+
|
|
56
|
+
[tool.ruff.lint]
|
|
57
|
+
# Essential rules that catch real issues
|
|
58
|
+
select = [
|
|
59
|
+
"E", # pycodestyle errors
|
|
60
|
+
"F", # Pyflakes
|
|
61
|
+
"I", # isort (import sorting)
|
|
62
|
+
"UP", # pyupgrade (Python 3.11+ syntax)
|
|
63
|
+
"B", # flake8-bugbear (likely bugs)
|
|
64
|
+
"SIM", # flake8-simplify (code simplification)
|
|
65
|
+
"RUF", # Ruff-specific rules
|
|
66
|
+
"ARG", # flake8-unused-arguments
|
|
67
|
+
"LOG", # flake8-logging (logging best practices)
|
|
68
|
+
]
|
|
69
|
+
|
|
70
|
+
# Ignore annoying/controversial rules
|
|
71
|
+
ignore = [
|
|
72
|
+
"E501", # Line length (formatter handles this)
|
|
73
|
+
"B008", # Function calls in defaults
|
|
74
|
+
"B904", # Raise from err - existing error handling pattern
|
|
75
|
+
"SIM102", # Use single if statement - readability preference
|
|
76
|
+
"SIM105", # Use contextlib.suppress - explicit exception handling preferred
|
|
77
|
+
"SIM108", # Use ternary operator - if/else can be clearer
|
|
78
|
+
"SIM117", # Use single with statement - readability preference
|
|
79
|
+
"ARG001", # Unused function argument - protocol requirements
|
|
80
|
+
"ARG002", # Unused method argument - interface compatibility
|
|
81
|
+
]
|
|
82
|
+
|
|
83
|
+
[tool.ruff.lint.isort]
|
|
84
|
+
# Clean import grouping
|
|
85
|
+
combine-as-imports = true
|
|
86
|
+
lines-after-imports = 2
|
|
87
|
+
|
|
88
|
+
[tool.ruff.lint.per-file-ignores]
|
|
89
|
+
# Tests can use assert and have unused imports
|
|
90
|
+
"tests/*" = ["S101", "F401", "F841"]
|
|
91
|
+
"__init__.py" = ["F401"]
|
|
92
|
+
|
|
93
|
+
[tool.mypy]
|
|
94
|
+
python_version = "3.11"
|
|
95
|
+
warn_return_any = true
|
|
96
|
+
warn_unused_configs = true
|
|
97
|
+
|
|
98
|
+
[tool.pytest.ini_options]
|
|
99
|
+
testpaths = ["tests"]
|
|
100
|
+
markers = [
|
|
101
|
+
"integration: tests that require a working Lean installation",
|
|
102
|
+
]
|
|
103
|
+
|
|
104
|
+
[tool.hatch.build.targets.sdist]
|
|
105
|
+
include = ["src/"]
|
|
106
|
+
|
|
107
|
+
[project.scripts]
|
|
108
|
+
leanback = "leanback.mcp_server:main"
|
|
109
|
+
leanback-test = "leanback.mcp_test:main"
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# LeanBack Implementation
|
|
2
|
+
|
|
3
|
+
MCP server exposing Lean 4 development tools via Model Context Protocol.
|
|
4
|
+
|
|
5
|
+
## Module Structure
|
|
6
|
+
|
|
7
|
+
- `mcp_server.py`: MCP server — defines project_info, check, prove, goals, eval, search tools
|
|
8
|
+
- `check.py`: Check tool implementation
|
|
9
|
+
- `prove.py`: Prove tool implementation
|
|
10
|
+
- `goals.py`: Goals tool implementation (proof state inspection)
|
|
11
|
+
- `mathlib_search.py`: Search tool (Mathlib declaration search)
|
|
12
|
+
- `mathlib_search_simple.py`: Regex-based Mathlib search backend
|
|
13
|
+
- `mcp_test.py`: E2E test client
|
|
14
|
+
- `common/`: Shared utilities
|
|
15
|
+
- `env.py`: Lean environment handling (LeanRunner)
|
|
16
|
+
- `errors.py`: Error parsing and formatting
|
|
17
|
+
- `project_context.py`: Project validation and context
|
|
18
|
+
- `path_manager.py`: Path resolution and management
|
|
19
|
+
- `util.py`: Misc utilities (temp file creation)
|