MCP AgentRun Server

MCP AgentRun Server

Enables safe Python code execution in isolated Docker containers through the AgentRun framework. Provides automatic container lifecycle management and comprehensive error handling for secure and reproducible code execution.

Category
访问服务器

README

MCP AgentRun Server

An MCP (Model Context Protocol) server that provides Python code execution capabilities using AgentRun. This server can execute Python code in isolated Docker containers for safe and reproducible code execution.

Features

  • Safe Code Execution: Execute Python code in isolated Docker containers
  • Container Management: Automatic container lifecycle management with docker-compose
  • Environment Isolation: Each execution runs in a clean, isolated environment
  • Error Handling: Comprehensive error handling and logging
  • Easy Integration: Simple MCP interface for AI assistants and tools

Installation

Prerequisites

  • Python 3.13 or higher
  • Docker and Docker Compose
  • UV package manager (recommended)

Setup

  1. Clone the repository:
git clone <repository-url>
cd mcp-agentrun
  1. Run the setup script:
chmod +x setup.sh
./setup.sh

Usage

From Cursor

Go to Cursor Settings > Tools and Integrations > [New MCP Server] In the mcp.json file add the following:

{
  "mcpServers": {
    "python-code-executor": {
      "command": "/path/to/mcp-agentrun/.venv/bin/python",
      "args": [
        "/path/to/mcp-agentrun/src/server.py"
      ],
      "env": {
        "PYTHONPATH": "/path/to/mcp-agentrun",
        "AGENTRUN_API_DIR": "/path/to/mcp-agentrun/agentrun/agentrun-api",
        "PYTHONUNBUFFERED": "1"
      }
    }
  }
}

Running the Server

python src/server.py

Available Tools

Execute Python Code

Execute Python code in a container:

result = execute_code(
    python_code="print('Hello, World!')\nprint(2 + 2)"
)

Returns the output of the code execution as a string.

Example Usage

See tests/test_build_container.py for a complete example:

import os
import subprocess
import dotenv
from agentrun import AgentRun

# Setup environment
rootdir = subprocess.run(["git", "rev-parse", "--show-toplevel"], 
                        capture_output=True, text=True).stdout.strip()
agentrun_api_dir = os.path.join(rootdir, "agentrun", "agentrun-api")

dotenv.load_dotenv(os.path.join(agentrun_api_dir, ".env.dev"))
container_name = os.getenv("CONTAINER_NAME")

# Execute code
with ComposeService(agentrun_api_dir):
    runner = AgentRun(container_name=container_name)
    code_from_llm = "print(1+2)"
    result = runner.execute_code_in_container(code_from_llm)
    print(result)  # Output: 3

Project Structure

mcp-agentrun/
├── agentrun/                 # AgentRun submodule
├── src/
│   ├── __init__.py
│   └── server.py            # MCP server implementation
├── tests/
│   ├── test_build_container.py  # Container testing
│   ├── test.py              # Unit tests
│   ├── test_integration.py  # Integration tests
│   └── dev.ipynb            # Development notebook
├── pyproject.toml           # Project configuration
├── pytest.ini              # Pytest configuration
├── run_tests.py             # Test runner script
├── setup.sh                 # Setup script
└── README.md               # This file

Configuration

The server requires a .env.dev file in the agentrun/agentrun-api/ directory with the following variables:

CONTAINER_NAME=your-container-name

Testing

Dependencies

  • agentrun>=0.2.5: Python code execution in containers
  • docker>=7.1.0: Docker API client
  • fastmcp>=2.10.5: MCP server framework
  • pydantic>=2.11.7: Data validation
  • dotenv>=0.9.9: Environment variable management
  • tenacity>=9.1.2: Retry logic

Development

Adding New Features

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests
  5. Submit a pull request

Running in Development Mode

# Install in development mode
uv pip install -e .

# Run the server
python src/server.py

License

This project is licensed under the MIT License - see the LICENSE file for details.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Related Projects

  • AgentRun: Python code execution in containers
  • FastMCP: Fast MCP server framework

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选