与 Daytona 的安全 Python 执行环境集成,以实现用于测试和教育目的的隔离代码运行和工作区管理。
Daytona MCP Interpreter
一个提供 Python 代码执行能力的 Model Context Protocol 服务器,运行在临时的 Daytona 沙箱中。

概述
Daytona MCP Interpreter 使像 Claude 这样的 AI 助手能够在安全、隔离的环境中执行 Python 代码和 Shell 命令。它实现了 Model Context Protocol (MCP) 标准,提供了以下工具:
- 在沙箱环境中执行 Python 代码
- 执行 Shell 命令
- 文件管理(上传/下载)
- Git 仓库克隆
- 运行服务器的 Web 预览生成
所有执行都在临时的 Daytona 工作区中进行,并在使用后自动清理。
安装
- 如果尚未安装 uv,请安装:
bash
curl -LsSf https://astral.sh/uv/install.sh | sh
- 创建并激活虚拟环境。
如果有现有的环境,请先停用并删除:
bash
deactivate
rm -rf .venv
创建并激活新的虚拟环境:
bash
uv venv
source .venv/bin/activate
(在 Windows 上: .venv\Scripts\activate
)
- 安装依赖项:
bash
uv add "mcp[cli]" pydantic python-dotenv "daytona-sdk>=0.10.5"
注意:此项目需要 daytona-sdk 版本 0.10.5 或更高版本。早期版本具有不兼容的 FileSystem API。
环境变量
为确保正常运行,请配置以下环境变量:
MCP_DAYTONA_API_KEY
: Daytona 认证所需的 API 密钥MCP_DAYTONA_SERVER_URL
: 服务器 URL (默认: https://app.daytona.io/api)MCP_DAYTONA_TIMEOUT
: 请求超时时间(秒)(默认: 180.0)MCP_DAYTONA_TARGET
: 目标区域 (默认: eu)MCP_VERIFY_SSL
: 启用 SSL 验证 (默认: false)
开发
直接运行服务器:
bash
uv run src/daytona_mcp_interpreter/server.py
如果 uv 不在您的路径中:
/Users/USER/.local/bin/uv run ~LOCATION/daytona-mcp-interpreter/src/daytona_mcp_interpreter/server.py
使用 MCP Inspector 测试服务器:
bash
npx @modelcontextprotocol/inspector \
uv \
--directory . \
run \
src/daytona_mcp_interpreter/server.py
查看日志:
tail -f /tmp/daytona-interpreter.log
与 Claude Desktop 的集成

- 在 Claude Desktop 中配置(或其他支持 MCP 的客户端):
在 MacOS 上,编辑: ~/Library/Application Support/Claude/claude_desktop_config.json
在 Windows 上,编辑: %APPDATA%\Claude\claude_desktop_config.json
json
{
"mcpServers": {
"daytona-interpreter": {
"command": "/Users/USER/.local/bin/uv",
"args": [
"--directory",
"/Users/USER/dev/daytona-mcp-interpreter",
"run",
"src/daytona_mcp_interpreter/server.py"
],
"env": {
"PYTHONUNBUFFERED": "1",
"MCP_DAYTONA_API_KEY": "api_key",
"MCP_DAYTONA_SERVER_URL": "api_server_url",
"MCP_DAYTONA_TIMEOUT": "30.0",
"MCP_VERIFY_SSL": "false",
"PATH": "/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin"
}
}
}
}
- 重启 Claude Desktop
- Daytona Python 解释器工具将在 Claude 中可用
可用工具
Shell Exec
在 Daytona 工作区中执行 Shell 命令。
bash
# 示例:列出文件
ls -la
# 示例:安装包
pip install pandas
File Download
从 Daytona 工作区下载文件,智能处理大文件。
基本用法:
file_download(file_path="/path/to/file.txt")
高级用法:
# 设置自定义文件大小限制
file_download(file_path="/path/to/large_file.csv", max_size_mb=10.0)
# 下载大文件的部分内容
file_download(file_path="/path/to/large_file.csv", download_option="download_partial", chunk_size_kb=200)
# 将大文件转换为文本
file_download(file_path="/path/to/large_file.pdf", download_option="convert_to_text")
# 压缩文件后再下载
file_download(file_path="/path/to/large_file.bin", download_option="compress_file")
# 强制下载,即使文件过大
file_download(file_path="/path/to/large_file.zip", download_option="force_download")
File Upload
将文件上传到 Daytona 工作区。支持文本和二进制文件。
基本用法:
# 上传文本文件
file_upload(file_path="/workspace/example.txt", content="Hello, World!")
高级用法:
# 使用特定路径上传文本文件
file_upload(
file_path="/workspace/data/config.json",
content='{"setting": "value", "enabled": true}'
)
# 使用 base64 编码上传二进制文件
import base64
with open("local_image.png", "rb") as f:
base64_content = base64.b64encode(f.read()).decode('utf-8')
file_upload(
file_path="/workspace/images/uploaded.png",
content=base64_content,
encoding="base64"
)
# 上传时不覆盖现有文件
file_upload(
file_path="/workspace/important.txt",
content="New content",
overwrite=False
)
Git Clone
将 Git 仓库克隆到 Daytona 工作区以进行分析和代码执行。
基本用法:
git_clone(repo_url="https://github.com/username/repository.git")
高级用法:
# 克隆特定分支
git_clone(
repo_url="https://github.com/username/repository.git",
branch="develop"
)
# 克隆到特定目录并保留完整历史记录
git_clone(
repo_url="https://github.com/username/repository.git",
target_path="my_project",
depth=0 # 0 表示完整历史记录
)
# 支持 Git LFS 克隆包含大文件的仓库
git_clone(
repo_url="https://github.com/username/large-files-repo.git",
lfs=True
)
Web Preview
为在 Daytona 工作区中运行的 Web 服务器生成预览 URL。
基本用法:
# 为运行在端口 3000 上的 Web 服务器生成预览链接
web_preview(port=3000)
高级用法:
# 生成带有描述性名称的预览链接
web_preview(
port=8080,
description="React Development Server"
)
# 生成链接时不检查服务器是否正在运行
web_preview(
port=5000,
check_server=False
)
示例:
bash
# 首先通过 Shell 使用 Python 启动一个简单的 Web 服务器
shell_exec(command="python -m http.server 8000 &")
# 然后为服务器生成预览链接
web_preview(port=8000, description="Python HTTP Server")