Safe Local Python Executor/Interpreter

Safe Local Python Executor/Interpreter

Stdio MCP 服务器,封装了来自 Hugging Face 的 smolagents 框架的自定义 Python 运行时 (LocalPythonExecutor)。该运行时结合了易于设置的优点(与 Docker、VM、云运行时相比),同时提供了安全保障,并限制了运行时内部允许的操作/导入。

Category
访问服务器

Tools

run_python

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

README

安全本地 Python 执行器

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

<img width="1032" alt="image" src="https://github.com/user-attachments/assets/3b820bfc-970a-4315-8f2d-970591c6fdae" />

特性

  • 公开 run_python 工具
  • 与直接使用 Python eva()l 相比,更安全地执行 Python 代码
  • 通过 uv 在 Python venv 中运行
  • 不允许文件 I/O 操作
  • 受限制的导入列表
    • collections
    • datetime
    • itertools
    • math
    • queue
    • random
    • re
    • stat
    • statistics
    • time
    • unicodedata

安全性

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

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

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

阅读更多 here

安装和执行

  1. 安装 uv(例如,macOS 上使用 brew install uv 或使用 官方文档
  2. 克隆存储库,更改目录 cd mcp_safe_local_python_executor
  3. 可以通过命令行 uv run mcp_server.py 启动服务器,venv 将自动创建,依赖项(smollagents, mcp)将被安装

配置 Claude Desktop

  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 -> Settings -> Developer -> 点击 "Edit Config" 按钮
  3. 添加以下配置:

{
    "mcpServers": {
        "safe-local-python-executor": {
            "command": "uv",
            "args": [
                "--directory", 
                "/path/to/mcp_local_python_executor/",
                "run",
                "mcp_server.py"
            ]
        }
    }
}
  1. 重启 Claude for Desktop
  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/

<a href="https://glama.ai/mcp/servers/@maxim-saplin/mcp_safe_local_python_executor"> <img width="380" height="200" src="https://glama.ai/mcp/servers/@maxim-saplin/mcp_safe_local_python_executor/badge" /> </a>

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选