Skip to main content

Dockerised Lean4 execution environment for AI agents

Project description

lean-docker-mcp

Dockerized Lean4 execution environment for AI agents.

Overview

This MCP server provides a safe, sandboxed Lean4 execution environment for LLM-powered agents. It allows agents to:

  • Execute Lean4 code in isolated Docker containers
  • Choose between transient or persistent execution environments
  • Maintain state between execution steps

Installation

Requirements

  • Docker must be installed and running on the host system
  • Python 3.11 or later
  • uv for package management (recommended)

Install from PyPI

# Using uv (recommended)
uv pip install lean-docker-mcp

# Using pip
pip install lean-docker-mcp

Install from Source

# Clone the repository
git clone https://github.com/artivus/lean-docker-mcp.git
cd lean-docker-mcp

# Install with uv
uv pip install -e .

# Or with pip
pip install -e .

Quick Start

Running the Server

The lean-docker-mcp server can be started directly using the module:

python -m lean_docker_mcp

This will start the MCP server and listen for JSONRPC requests on stdin/stdout.

Components

Docker Execution Environment

The server implements two types of execution environments:

  1. Transient Environment

    • Each execution is isolated in a fresh container
    • State isn't maintained between calls
    • Safer for one-off code execution
  2. Persistent Environment

    • Maintains state between executions
    • Variables and functions defined in one execution are available in subsequent executions
    • Suitable for interactive, stateful REPL-like sessions

Tools

The server provides the following tools:

  • execute-lean: Run Lean4 code in a transient Docker container

    • Takes code (required) parameter
    • Returns execution results
  • execute-lean-persistent: Run Lean4 code in a persistent Docker container

    • Takes code (required) and session_id (optional) parameters
    • Returns execution results
    • Maintains state between calls
  • cleanup-session: Clean up a persistent session

    • Takes session_id (required) parameter
    • Stops and removes the associated Docker container

Configuration

The server can be configured via a YAML configuration file. By default, it looks for a file at ~/.lean-docker-mcp/config.yaml.

Configuration File Structure

Example configuration:

docker:
  image: lean-docker-mcp:latest
  working_dir: /home/leanuser/project
  memory_limit: 256m
  cpu_limit: 0.5
  timeout: 30
  network_disabled: true
  read_only: false

lean:
  allowed_imports:
    - Lean
    - Init
    - Std
    - Mathlib
  blocked_imports:
    - System.IO.Process
    - System.FilePath

Docker Configuration Options

Option Description Default
image Docker image to use for execution lean-docker-mcp:latest
working_dir Working directory inside container /home/leanuser/project
memory_limit Memory limit for container 256m
cpu_limit CPU limit (0.0-1.0) 0.5
timeout Execution timeout in seconds 30
network_disabled Disable network access true
read_only Run container in read-only mode false

Integration with Claude and Anthropic Products

Claude Desktop

On MacOS: ~/Library/Application\ Support/Claude/claude_desktop_config.json On Windows: %APPDATA%/Claude/claude_desktop_config.json

Development/Unpublished Servers Configuration
"mcpServers": {
  "lean-docker-mcp": {
    "command": "uv",
    "args": [
      "--directory",
      "/path/to/lean-docker-mcp",
      "run",
      "lean-docker-mcp"
    ]
  }
}
Published Servers Configuration
"mcpServers": {
  "lean-docker-mcp": {
    "command": "uvx",
    "args": [
      "lean-docker-mcp"
    ]
  }
}

Example MCP Usage

Transient Execution

# Define and use a simple function
result = await call_tool("execute-lean", {
  "code": "def hello (name : String) : String := s!\"Hello, {name}\"\n\ndef main : IO Unit := IO.println (hello \"Lean4!\")"
})

Persistent Session

# Create a persistent session and define a function
result = await call_tool("execute-lean-persistent", {
  "code": "def add (a b : Nat) : Nat := a + b\n\ndef main : IO Unit := IO.println \"Function defined\""
})

# Use the function in a subsequent call with the same session
result = await call_tool("execute-lean-persistent", {
  "session_id": "previous_session_id",
  "code": "def main : IO Unit := IO.println (toString (add 10 20))"
})

Development

Development Setup

  1. Clone the repository:
git clone https://github.com/artivus/lean-docker-mcp.git
cd lean-docker-mcp
  1. Set up development environment:
uv venv
source .venv/bin/activate  # On Windows: .venv\Scripts\activate
uv pip install -e ".[dev]"
  1. Install pre-commit hooks:
pre-commit install

Running Tests

# Run all tests
pytest

# Run tests with coverage
pytest --cov=src/lean_docker_mcp

# Run specific test categories
pytest tests/unit/
pytest tests/integration/

Building and Publishing

To prepare the package for distribution:

  1. Sync dependencies and update lockfile:
uv sync
  1. Build package distributions:
uv build
  1. Publish to PyPI:
uv publish

Debugging

Since MCP servers run over stdio, debugging can be challenging. For the best debugging experience, we strongly recommend using the MCP Inspector.

You can launch the MCP Inspector via npm with this command:

npx @modelcontextprotocol/inspector uv --directory /path/to/lean-docker-mcp run lean-docker-mcp

License

[License information]

Contributing

Contributions are welcome! Please see CONTRIBUTING.md for guidelines.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

lean_docker_mcp-0.1.0.tar.gz (14.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

lean_docker_mcp-0.1.0-py3-none-any.whl (17.3 kB view details)

Uploaded Python 3

File details

Details for the file lean_docker_mcp-0.1.0.tar.gz.

File metadata

  • Download URL: lean_docker_mcp-0.1.0.tar.gz
  • Upload date:
  • Size: 14.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.23

File hashes

Hashes for lean_docker_mcp-0.1.0.tar.gz
Algorithm Hash digest
SHA256 182ff2492b9f1bf063afcb4084449af8bce1df10b2f7226364be966691b53e6b
MD5 9b56594f102d9df309612c7591646232
BLAKE2b-256 76183fd333418e351ebaf69f018cdc670048ceadb37ea46d3ebb9fbb3a13bbe2

See more details on using hashes here.

File details

Details for the file lean_docker_mcp-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for lean_docker_mcp-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9ceba392585d60bcf444a9ce88a6d64c3252a6a5f522a2e4b2f6bcc4b4a6b633
MD5 c0b8ff81f1c8b427876e42d78572483c
BLAKE2b-256 4862dd3735790d03a560336d6d211d765c9b71cf835096ddbcfec39358142668

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page