MCP安全本地Python执行器
标准I/O MCP服务器包装了来自Hugging Face的`smolagents`框架的自定义Python运行时(LocalPythonExecutor)。该运行时结合了易于设置的优点(与docker、虚拟机、云运行时相比),同时提供了保护措施,并限制了运行时内允许的操作和导入。
可用工具 (1 个)
该服务在 MCP 协议中暴露的工具,AI 可按需调用
run_python 1 个参数 需填 1 项
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
必填参数:code
服务介绍
安全本地 Python 执行器
一个 MCP 服务器(stdio 传输)封装了 Hugging Face 的 LocalPythonExecutor(来自 smolagents 框架)。它是一个自定义的 Python 运行时,提供了在本地运行由 LLM 生成的 Python 代码时的基本隔离/安全性。它不需要 Docker 或 VM。这个包允许通过 MCP(模型上下文协议)暴露 Python 执行器,作为 Claude Desktop、Cursor 或任何其他 MCP 兼容客户端的工具。对于 Claude Desktop 来说,这个工具是添加缺失的代码解释器(在 ChatGPT 中已经有一段时间作为插件可用)的一种简便方法。
特性
- 暴露
run_python工具 - 相比直接使用 Python
eval()更安全地执行 Python 代码 - 通过 uv 在 Python 虚拟环境中运行
- 不允许文件 I/O 操作
- 限制导入列表
- collections
- datetime
- itertools
- math
- queue
- random
- re
- stat
- statistics
- time
- unicodedata
安全性
小心在您的机器上执行 LLM 生成的代码,远离通过命令行或使用 eval() 运行 Python 的 MCP 服务器。最安全的选择是使用虚拟机或 Docker 容器,尽管这需要一些设置工作,会消耗资源/速度较慢。有一些第三方服务提供 Python 运行时,但它们需要注册、API 密钥等。
LocalPythonExecutor 在直接使用本地 Python 环境(设置更简单)与在 Docker 容器或 VM/第三方服务中远程执行(更安全)之间提供了良好的平衡。Hugging Face 团队投入时间创建了一个快速且安全的选项来运行其代码代理使用的 LLM 生成的代码。此 MCP 服务器基于它构建:
为了增加第一层安全性,smolagents 中的代码执行不是由原生 Python 解释器完成的。我们从头开始重建了一个更安全的 LocalPythonExecutor。
更多信息请参阅这里。
安装和执行
- 安装
uv(例如,在 macOS 上使用brew install uv或参考官方文档) - 克隆仓库,切换目录
cd mcp_safe_local_python_executor - 可以通过命令行
uv run mcp_server.py启动服务器,虚拟环境将自动创建,并安装依赖项(smolagents, mcp)
配置 Claude Desktop
-
确保已安装 Claude for Desktop(从 claude.ai 下载)
-
编辑您的 Claude for Desktop 配置文件:
- macOS:
~/Library/Application Support/Claude/claude_desktop_config.json - Windows:
%APPDATA%\Claude\claude_desktop_config.json - 或者打开 Claude Desktop -> 设置 -> 开发者 -> 点击“编辑配置”按钮
- macOS:
-
添加以下配置:
{
"mcpServers": {
"safe-local-python-executor": {
"command": "uv",
"args": [
"--directory",
"/path/to/mcp_local_python_executor/",
"run",
"mcp_server.py"
]
}
}
}
- 重启 Claude for Desktop
- Python 执行器工具现在将在 Claude 中可用(你会在消息输入字段中看到锤子图标)
示例提示
配置完成后,你可以使用如下提示:
- "使用 Python 计算5的阶乘"
- "创建一个直到100的质数列表"
- "解这个方程(使用 Python):x^2 + 5x + 6 = 0"
开发
克隆仓库。使用 uv 创建虚拟环境,安装开发依赖项,运行测试:
uv venv .venv
uv sync --group dev
python -m pytest tests/