Skip to main content
Glama
Safe Local Python Executor/Interpreter

Safe Local Python Executor/Interpreter

by maxim-saplin

安全的本地 Python 执行器

一个 MCP 服务器(stdio 传输),它封装了 Hugging Face 的LocalPythonExecutor (来自smolagents框架)。它是一个自定义的 Python 运行时,在本地运行 LLM 生成的 Python 代码时提供基本的隔离/安全性。它不需要 Docker 或虚拟机。此包允许通过 MCP(模型上下文协议)将 Python 执行器公开为 LLM 应用(例如 Claude Desktop、Cursor 或任何其他兼容 MCP 的客户端)的工具。对于 Claude Desktop,此工具可以轻松添加缺少的代码解释器(ChatGPT 中已作为插件提供一段时间了)。

特征

  • 公开run_python工具

  • 与直接使用 Python eva()l相比,Python 代码的执行更安全

  • 通过 Python venv 中的 uv 运行

  • 不允许文件 I/O 操作

  • 限制进口商品清单

    • 收藏品

    • 日期时间

    • 迭代工具

    • 数学

    • 队列

    • 随机的

    • 关于

    • 统计

    • 统计数据

    • 时间

    • unicode数据

Related MCP server: MCP Code Mode

安全

在你的机器上执行 LLM 生成的代码时要小心,远离通过命令行或使用eval()运行 Python 的 MCP 服务器。最安全的选择是使用虚拟机或 Docker 容器,尽管这需要一些设置工作,消耗资源/速度较慢。有一些第三方服务提供 Python 运行时,但它们需要注册、API 密钥等。

LocalPythonExecutor在直接使用本地 Python 环境(更易于设置)和在 Dokcer 容器或虚拟机/第三方服务中远程执行(更安全)之间提供了良好的平衡。Hugginng Face 团队投入了大量时间,创建了一个快速安全的选项,用于运行其代码代理使用的 LLM 生成的代码。此 MCP 服务器以此为基础构建:

为了增加第一层安全性,smolagents 中的代码执行并非由原生 Python 解释器执行。我们从头开始重新构建了一个更安全的 LocalPythonExecutor。

点击此处了解更多信息。

安装和执行

  1. 安装uv (在 macOS 上brew install uv或者使用官方文档)

  2. 克隆 repo,更改目录cd mcp_safe_local_python_executor

  3. 可以通过命令行uv run mcp_server.py启动服务器,venv 将自动创建,依赖项(smollagents、mcp)将被安装

配置 Claude 桌面

  1. 确保已安装 Claude for Desktop(从claude.ai下载)

  2. 编辑 Claude for Desktop 配置文件:

    • macOS: ~/Library/Application Support/Claude/claude_desktop_config.json

    • Windows: %APPDATA%\Claude\claude_desktop_config.json

    • 或者打开 Claude Desktop -> 设置 -> 开发者 -> 点击“编辑配置”按钮

  3. 添加以下配置:

{
    "mcpServers": {
        "safe-local-python-executor": {
            "command": "uv",
            "args": [
                "--directory", 
                "/path/to/mcp_local_python_executor/",
                "run",
                "mcp_server.py"
            ]
        }
    }
}
  1. 重启 Claude 桌面版

  2. Python 执行器工具现在将在 Claude 中可用(您将在消息输入字段中看到锤子图标)

示例提示

配置完成后,您可以使用如下提示:

  • “使用 Python 计算 5 的阶乘”

  • “创建一个 100 以内的素数列表”

  • “解这个方程(使用 Python):x^2 + 5x + 6 = 0”

发展

克隆仓库。使用uv创建 venv,安装开发依赖项,运行测试:

uv venv .venv
uv sync --group dev
python -m pytest tests/

Available Tools

1 tool
run_pythonA

Execute Python code in a secure sandbox environment.

This tool allows running simple Python code for calculations and data manipulations.
The execution environment is restricted for security purposes. Make sure you create a single file
that can be executed in one go and it returns a result.

Default allowed imports:
- math
- random 
- datetime
- time
- json
- re
- string
- collections
- itertools
- functools
- operator

Args:
    code: The Python code to execute. Must be valid Python 3 code. The result must be stored in a variable called `result`. E.g.:
    ```python
    import math
    result = math.sqrt(16)
    ```
    
Returns:
    A dictionary with execution results containing:
    - result: The final value or None if no value is returned
    - logs: Any output from print statements

Inputs: code (required). Returns: result

Tool Schema Changelog

Recent tool additions, removals, and schema changes observed during successful MCP inspections.

Loading tool schema changelog…

    TDQS

    A4.1/5.0

    Scored across 1 tool

    Disambiguation5/5

    With only one tool, there is no possibility of confusion or overlap between tools. The single tool 'run_python' has a clearly defined and distinct purpose.

    Naming Consistency5/5

    The naming follows a consistent verb_noun pattern with 'run_python'. Since there's only one tool, consistency is inherently perfect with no deviations to assess.

    Tool Count2/5

    A single tool is too few for the server's purpose as a 'Safe Local Python Executor/Interpreter'. This suggests a thin surface that may lack essential operations like code validation, environment inspection, or error handling, limiting agent functionality.

    Completeness2/5

    The tool set is severely incomplete for the domain. While 'run_python' covers execution, there are obvious gaps such as tools for listing allowed imports, checking code safety, managing execution timeouts, or handling errors, which are critical for a secure interpreter.

    Maintenance

    ActivityInactive
    ResponsivenessLimited data

    Related MCP Connectors

    Related MCP Servers