maxim-saplin/mcp_safe_local_python_executor(一个安全的本地 Python 执行器 MCP 服务)

maxim-saplin/mcp_safe_local_python_executor
分类General
作者Community
星标881
定价Free

简介

这是一个基于 Hugging Face Smolagents 实现的 MCP 服务器,旨在为 LLM 提供一个安全的本地 Python 执行环境。它解决了开发者在让 AI 运行代码时最担心的安全与隔离问题,让模型能够直接在本地执行数据处理、数学计算或调用特定库,而无需手动复制粘贴代码。对于习惯使用 Cursor 或 Claude Desktop 的用户,它可以将 AI 从单纯的“代码生成器”升级为具备实时执行能力的“自动化助手”,上手门槛低,配置简单,非常适合需要本地数据分析或快速原型验证的场景。

核心亮点

  • 基于 Smolagents 实现,确保本地执行环境安全
  • 让 LLM 具备实时运行 Python 代码并获取结果的能力
  • 无缝集成至支持 MCP 的 IDE 或客户端
  • 极大地提升本地数据处理和复杂计算的自动化程度

完整文档

Safe Local Python Executor

一个 MCP server (stdio transport),封装了 Hugging Face 的 LocalPythonExecutor(来自 smolagents 框架)。它是一个自定义的 Python 运行时,在本地运行由 LLM 生成的 Python 代码时提供基础的隔离与安全保障,且无需 Docker 或 VM。
该包允许通过 MCP (Model Context Protocol) 将 Python 执行器作为工具暴露给 Claude Desktop、Cursor 或任何其他兼容 MCP 的客户端。
对于 Claude Desktop,该工具是添加缺失的 Code Interpreter(ChatGPT 早已提供的插件功能)的一种便捷方式。

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

Features

  • 暴露 run_python 工具
  • 相比直接使用 Python eval(),执行 Python 代码更安全
  • 通过 uv 在 Python venv 中运行
  • 不允许文件 I/O 操作
  • 限制 import 列表:
- collections - datetime - itertools - math - queue - random - re - stat - statistics - time - unicodedata

Security在本地机器上执行 LLM 生成的代码时请保持谨慎,远离通过命令行或使用 eval() 运行 Python 的 MCP server。最安全的选择是使用 VM 或 docker 容器,尽管这需要一定的配置成本,且消耗资源/速度较慢。此外还有提供 Python runtime 的第三方服务,但它们需要注册、API key 等。

LocalPythonExecutor 在直接使用本地 Python 环境(配置更简单)与在 Docker 容器、VM 或第三方服务中远程执行(更安全)之间取得了良好的平衡。Hugging Face 团队投入时间为其 code agent 创建了一个快速且安全的 LLM 生成代码运行方案。此 MCP server 基于该方案构建:

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

更多详情请参阅 此处

安装与执行

通过 Smithery 安装

若要通过 Smithery 为 Claude Desktop 自动安装 Safe Local Python Executor:

bash
npx -y @smithery/cli install @maxim-saplin/mcp_safe_local_python_executor --client claude
### 手动安装
1. 安装 uv(例如在 macOS 上使用 brew install uv 或参考 official docs
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. 添加以下配置:

json
{
"mcpServers": {
"safe-local-python-executor": {
"command": "uv",
"args": [
"--directory",
"/path/to/mcp_local_python_executor/",
"run",
"mcp_server.py"
]
}
}
}
4. 重启 Claude for Desktop
5. Python executor 工具现在将在 Claude 中可用(你将在消息输入框中看到锤子图标)

Example Prompts

配置完成后,你可以使用如下 prompt:

  • "使用 Python 计算 5 的阶乘"
  • "创建一个 100 以内的质数列表"
  • "解这个方程(使用 Python):x^2 + 5x + 6 = 0"

Development

Clone 该 repo。使用 uv 创建 venv,安装 dev 依赖并运行测试:

code
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>

![smithery badge](https://smithery.ai/server/@maxim-saplin/mcp_safe_local_python_executor)

![MseeP.ai Security Assessment Badge](https://mseep.ai/app/maxim-saplin-mcp-safe-local-python-executor)