Safe Local Python Executor/Interpreter

by maxim-saplin
Verified

local-only server

The server can only run on the client’s local machine because it depends on local resources.

Integrations

  • Integrates with Hugging Face's LocalPythonExecutor from the smolagents framework to provide secure Python code execution capabilities with basic isolation and security for running LLM-generated Python code.

安全的本地 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数据

安全

在你的机器上执行 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/

You must be authenticated.

A
security – no known vulnerabilities
A
license - permissive license
A
quality - confirmed to work

Stdio MCP 服务器包装了来自 Hugging Faces smolagents框架的自定义 Python 运行时 (LocalPythonExecutor)。该运行时易于设置(与 Docker、VM 和云运行时相比),同时提供了安全措施并限制了运行时内允许的操作/导入。

  1. Features
    1. Security
      1. Installation and Execution
        1. Configuring Claude Desktop
          1. Example Prompts
            1. Development
              ID: cj2uey3ghu