mcpField
P
Python Runner MCP Server

Python Runner MCP Server

本地服务
github.com/taiji1985开发工具174 周前更新

关于 Python Runner MCP Server

Python Runner MCP Server 为数据科学和机器学习工作流执行 Python 代码,适合需要隔离执行、输出捕获及常用科学计算库的 MCP 客户端用户。

主要功能

  1. 01在隔离命名空间中执行代码
  2. 02预装数据科学库
  3. 03捕获输出、错误和返回值
  4. 04通过 uvx 或本地运行

适用场景

  1. 01使用 pandas 和 numpy 分析表格数据
  2. 02运行 scikit-learn 机器学习示例
  3. 03通过 MCP 客户端执行 Python 代码

MCP 相关

该服务实现 MCP,并向兼容客户端(包括 Claude Desktop)提供 Python 代码执行能力。

配置

本地服务的配置在各客户端中通用,复制后粘贴到客户端的 MCP 配置文件即可。

mcp.json · 通用配置
{
"mcpServers": {
"python_runner_mcp": {
"command": "uvx",
"args": [
"python-runner"
]
}
}
}
启动命令uvx python-runner

本地服务需要在你的电脑上安装运行环境(如 Node.js 18+)。各客户端的配置文件位置不同,请参考客户端文档。

安装包
pypipython-runner

工具

还没有获取到工具列表

本地服务需要在你的电脑上运行后才能读取工具,维护者暂未提供工具列表。

详细介绍

Python Runner MCP 服务

English | 中文

基于 FastMCP 框架的 Python 代码执行服务,专为数据科学和机器学习工作流设计。

🚀 功能

  • 安全的 Python 代码执行:在隔离的命名空间中执行 Python 代码
  • 丰富的数据科学库:预装常用的数据科学和机器学习软件包
  • 实时输出捕获:捕获标准输出、错误输出和返回值
  • 支持 MCP 协议:完全兼容 Model Context Protocol
  • 易于使用:提供简单、整洁的 API 接口

⚡ 快速开始(推荐)

使用 uvx 直接运行服务,无需安装:[TODO ..... ]

# Run directly (no pre-installation required)
uvx python-runner
在 Claude Desktop 中配置

将以下配置添加到 Claude Desktop 配置文件中:

{
  "mcpServers": {
    "python-runner": {
      "command": "uvx",
      "args": ["python-runner"]
    }
  }
}

配置文件位置:

  • macOS:~/Library/Application Support/Claude/claude_desktop_config.json
  • Windows:%APPDATA%\Claude\claude_desktop_config.json
调试和测试
# Use MCP Inspector to debug the server
npx @modelcontextprotocol/inspector uvx python-runner

📦 预装库

本项目预装了以下常用的数据科学和机器学习库:

  • 数据处理:numpy、pandas、scipy
  • 机器学习:scikit-learn
  • 数据可视化:matplotlib、seaborn、plotly
  • 科学计算:sympy
  • 图像处理:pillow
  • 网络分析:networkx
  • 交互式开发:jupyter
  • HTTP 请求:requests

🛠️ 手动安装(用于开发)

前提条件
  • Python 3.12 或更高版本
  • uv 包管理器(推荐)或 pip
使用 uv 安装(推荐)
# Clone the repository
git clone <repository-url>
cd python_runner

# Install dependencies
uv sync
使用 pip 安装
# Clone the repository
git clone <repository-url>
cd python_runner

# Create virtual environment
python -m venv venv
source venv/bin/activate  # Windows: venv\Scripts\activate

# Install dependencies
pip install -e .

🚀 用法

在 Claude Desktop 中使用

完成配置后,即可直接在 Claude Desktop 中使用 Python 代码执行功能:

Please help me execute this Python code:

import pandas as pd
import numpy as np

# Create sample data
data = pd.DataFrame({
    'sales': [100, 150, 200, 120, 180],
    'profit': [20, 30, 50, 25, 40]
})

print("Sales data statistics:")
print(data.describe())
作为独立 MCP 服务运行
# Using uvx (recommended)
uvx python-runner

# Or run locally
python main.py
直接调用 API
from main import execute_python

# Execute simple Python code
result = execute_python("""
print("Hello, World!")
x = 1 + 2
print(f"Result: {x}")
""")

print(result)
# Output:
# {
#     'output': 'Hello, World!\nResult: 3\n',
#     'error': '',
#     'success': True
# }
数据科学示例
# Data analysis example
code = """
import pandas as pd
import numpy as np

# Create sample data
data = pd.DataFrame({
    'x': np.random.randn(100),
    'y': np.random.randn(100)
})

print(f"Data shape: {data.shape}")
print(f"Statistical summary:\n{data.describe()}")
"""

result = execute_python(code)
print(result['output'])
# Machine learning example
code = """
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score

# Generate sample data
X, y = make_classification(n_samples=1000, n_features=20, random_state=42)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Train model
clf = RandomForestClassifier(random_state=42)
clf.fit(X_train, y_train)

# Predict and evaluate
y_pred = clf.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
print(f"Model accuracy: {accuracy:.4f}")
"""

result = execute_python(code)
print(result['output'])

📋 API 参考

execute_python(code: str) -> dict

执行 Python 代码并返回结果。

参数:

  • code(str):要执行的 Python 代码字符串

返回值:

  • dict:包含以下键的字典:
    • output(str):标准输出内容
    • error(str):错误消息(如果有)
    • success(bool):执行是否成功

🔒 安全性

  • 在隔离的命名空间中执行代码
  • 捕获并重定向标准输出和错误输出
  • 提供详细的错误跟踪信息

⚠️ 警告:此工具会执行任意 Python 代码。请确保在可信环境中使用。

🤝 参与贡献

欢迎提交问题和 Pull Request!

  1. Fork 此仓库
  2. 创建功能分支(git checkout -b feature/AmazingFeature)
  3. 提交更改(git commit -m 'Add some AmazingFeature')
  4. 推送到分支(git push origin feature/AmazingFeature)
  5. 提交 Pull Request

📄 许可证

本项目采用 MIT License 授权,详情请参阅 LICENSE 文件。

🙏 致谢

  • FastMCP - 出色的 MCP 框架
  • 所有预装开源数据科学库的维护者

为数据科学社区倾心打造 ❤️

评论

支持 Markdown

第一次接触 MCP?

从协议是什么,到在客户端里装上第一个服务,这几条回答覆盖了大多数入门问题。

一个开放协议,规定了 AI 应用如何以统一方式连接外部工具与数据源。客户端负责与模型对话,MCP 服务负责提供工具、资源和提示模板,两者通过标准消息通信,同一个服务可以被不同客户端复用。