Python执行服务

MCP Python Executor是一个用于执行Python代码和管理Python包的服务器,提供安全约束、资源监控和预配置常用包等功能。

  • by bsmi021
  • JavaScript
  • 本地部署
  • 3 星标
  • 更新于
在 GitHub 查看 bsmi021/mcp-python-executor

使用说明

MCP Python执行器

用于执行Python代码和管理Python包的模型上下文协议(MCP)服务器。

特性

  • 执行带有安全约束的Python代码
  • 安装和管理Python包
  • 预配置常用软件包
  • 资源监控和限制
  • 健康检查和指标
  • 结构化日志记录

配置

可以通过MCP设置中的环境变量配置服务器:

{
  "mcpServers": {
    "mcp-python-executor": {
      "command": "node",
      "args": ["path/to/python-executor/build/index.js"],
      "env": {
        "PREINSTALLED_PACKAGES": "numpy pandas matplotlib scikit-learn",
        "MAX_MEMORY_MB": "512",
        "EXECUTION_TIMEOUT_MS": "30000",
        "MAX_CONCURRENT_EXECUTIONS": "5",
        "LOG_LEVEL": "info",
        "LOG_FORMAT": "json"
      }
    }
  }
}

环境变量

  • PREINSTALLED_PACKAGES:启动时要安装的Python包列表,以空格分隔
  • MAX_MEMORY_MB:每次执行的最大内存限制(默认值:512)
  • EXECUTION_TIMEOUT_MS:最大执行时间(毫秒)(默认值:30000)
  • MAX_CONCURRENT_EXECUTIONS:最大并发执行数(默认值:5)
  • LOG_LEVEL:日志记录级别(调试|信息|错误,默认值:信息)
  • LOG_FORMAT:日志格式(json|text,默认为json)

可用工具

1.execute_python

执行Python代码并返回结果。

interface ExecutePythonArgs {
  code?: string;          // Python code to execute (inline)
  scriptPath?: string;    // Path to existing Python script file (alternative to code)
  inputData?: string[];   // Optional input data
}

示例:

// Example with inline code
{
  "code": "print('Hello, World!!')\nfor i in range(3): print(i)",
  "inputData": ["optional", "input", "data"]
}

// Example with script path
{
  "scriptPath": "/path/to/your_script.py",
  "inputData": ["optional", "input", "data"]
}

2.安装包

安装Python包。

interface InstallPackagesArgs {
  packages: string[];
}

例子: