MCP代码执行服务器
用于模型上下文协议(MCP)的强大、安全和有状态的代码执行服务器。
该服务器通过提供以下功能将您的AI助手变成一名有能力的数据分析师:
- 有状态的Python执行:定义变量并在后续调用中使用它们(REPL样式)
- 数据可视化:自动捕获
matplotlib绘制并返回图像 - 文件系统访问:在专用工作区内安全地读写文件
- 包管理:动态安装任何Python包
灵感来自: 使用MCP执行代码 -Anthropic关于用MCP实现代码执行的文章
快速开始
先决条件
- 紫外线 (推荐)或Python 3.10+
安装
# Clone the repository
git clone https://github.com/ozgureyilmaz/mcp-executor.git
cd mcp-executor
# Create virtual environment and install dependencies
uv venv
uv pip install -e .用法
运行服务器
服务器通过以下方式进行通信 stdio。要将其与MCP客户端(如Claude Desktop)一起使用,请按如下方式配置:
{
"mcpServers": {
"executor": {
"command": "/Users/yourusername/dev/mcp-executor/.venv/bin/python",
"args": ["-m", "mcp_executor.server"],
"cwd": "/Users/yourusername/dev/mcp-executor"
}
}
}重要:替换 /Users/yourusername/dev/mcp-executor 根据您的实际项目路径。
可用工具
| 工具 | 说明 |
|---|---|
execute_python | 执行Python代码。保留通话之间的状态。返回文本输出和绘图。 |
install_package | 安装Python包(例如。, pandas, numpy). |
read_file | 从中读取文件 ./workspace 目录。 |
write_file | 将内容写入到 ./workspace 目录。 |
list_files | 列出文件 ./workspace 目录。 |
示例
检查 examples/ 使用示例目录。
安全
- 工作区隔离:文件操作严格限于
./workspace目录 - 本地执行:代码在您的计算机上本地运行。确保您信任正在执行的代码
- 软件包安装:包安装在隔离的虚拟环境中
项目结构
mcp-executor/
├── src/
│ └── mcp_executor/
│ ├── __init__.py # Package initialization
│ ├── __main__.py # Direct module execution
│ ├── server.py # MCP server implementation
│ └── execution.py # Code execution layer
├── examples/ # Example scripts
├── pyproject.toml # Project configuration
└── README.md # This file资源
许可证
MIT许可证-有关详细信息,请参阅许可证文件
贡献
欢迎投稿!请随时提交拉取请求。
