Back to MCP directory
publicPublicdnsLocal runtime

mcp-python-executor

一个用于执行Python代码和管理Python包的MCP服务器,提供安全执行环境、包管理、资源监控等功能

article

README

🚀 MCP Python 执行器

MCP Python 执行器是一个 Model Context Protocol (MCP) 服务器,可执行 Python 代码并管理 Python 包,为 Python 代码的执行和包管理提供了便捷、安全的解决方案。

✨ 主要特性

  • 带有安全约束的 Python 代码执行,保障代码执行安全。
  • 支持安装和管理 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 | 日志级别(debug|info|error,默认:info) | | LOG_FORMAT | 日志格式(json|text,默认:json) |

💻 使用示例

1. execute_python

执行 Python 代码并返回结果。

interface ExecutePythonArgs {
  code?: string;          // 要执行的 Python 代码(内联)
  scriptPath?: string;    // 现有 Python 脚本文件的路径(代码的替代方案)
  inputData?: string[];   // 可选输入数据
}

基础用法

// 带内联代码的例子
{
  "code": "print('Hello, World!!')\nfor i in range(3): print(i)",
  "inputData": ["optional", "input", "data"]
}

高级用法

// 带脚本路径的例子
{
  "scriptPath": "/path/to/your_script.py",
  "inputData": ["optional", "input", "data"]
}

2. install_packages

安装 Python 包。

interface InstallPackagesArgs {
  packages: string[];
}

基础用法

// 示例:安装单个软件包
{
  "packages": ["requests"]
}

高级用法

// 示例:安装多个软件包
{
  "packages": ["numpy", "pandas", "matplotlib", "scikit-learn"]
}
help

Runtime guide

cloud

Hosted runtime

Hosted servers run from a provider-managed environment. You usually connect the MCP client to the hosted endpoint or follow the provider's authorization flow, without keeping a local process alive

  1. Open provider connection page
  2. Authorize or copy endpoint
  3. Connect from your MCP client
terminal

Local runtime / other methods

Local servers run on your own machine or infrastructure. You normally copy the server_config into your MCP client, install the required package, and provide env variables from env_schema when needed

  1. Copy server_config
  2. Install required package
  3. Fill env variables and restart client