Code Execution MCP Server
Enables AI agents to execute terminal commands and Python code on the host system with session management and smart output handling, leveraging Agent Zero's proven implementation.
README
Code Execution MCP Server
A Model Context Protocol (MCP) server that exposes Agent Zero's battle-tested code execution capabilities.
This MCP server allows any AI agent (Claude, GPT-4, etc.) to execute terminal commands and Python code on the host system using Agent Zero's proven implementation.
Features
- Execute Terminal Commands: Run shell commands with full session persistence
- Execute Python Code: Run Python code via IPython with session management
- Multiple Sessions: Maintain separate execution contexts
- Smart Output Handling: Automatic prompt detection, timeout management, and dialog detection
- Cross-Platform: Works on Linux, macOS, and Windows (experimental)
Installation
# Clone or download this package
cd code-execution-mcp
# Install dependencies
pip install -e .
# For Windows support
pip install -e ".[windows]"
Configuration
The MCP server can be configured via environment variables:
# Shell executable (default: /bin/bash on Unix, cmd.exe on Windows)
export CODE_EXEC_EXECUTABLE=/bin/bash
# Init commands (semicolon-separated, run when creating new sessions)
export CODE_EXEC_INIT_COMMANDS="source /path/to/venv/bin/activate;export PATH=\$PATH:/custom/bin"
# Timeout configuration (in seconds)
export CODE_EXEC_FIRST_OUTPUT_TIMEOUT=30 # Wait for first output
export CODE_EXEC_BETWEEN_OUTPUT_TIMEOUT=15 # Wait between output chunks
export CODE_EXEC_DIALOG_TIMEOUT=5 # Detect dialog prompts
export CODE_EXEC_MAX_EXEC_TIMEOUT=180 # Maximum execution time
MCP Client Configuration
Add to your Claude Desktop config (~/Library/Application Support/Claude/claude_desktop_config.json on macOS):
{
"mcpServers": {
"code-execution": {
"command": "python",
"args": ["/Users/lazy/Projects/A0-code-tool-MCP/code-execution-mcp/main.py"],
"env": {
"CODE_EXEC_EXECUTABLE": "/bin/bash",
"CODE_EXEC_INIT_COMMANDS": "source /Users/lazy/Projects/A0-code-tool-MCP/code-execution-mcp/.venv/bin/activate"
}
}
}
}
Available Tools
execute_terminal
Execute a terminal command in the specified session.
Parameters:
command(string, required): The shell command to executesession(integer, optional): Session number (default: 0)
Example:
await execute_terminal(command="ls -la", session=0)
execute_python
Execute Python code via IPython in the specified session.
Parameters:
code(string, required): The Python code to executesession(integer, optional): Session number (default: 0)
Example:
await execute_python(code="import sys; print(sys.version)", session=0)
get_output
Get accumulated output from a terminal session.
Parameters:
session(integer, optional): Session number (default: 0)
Example:
await get_output(session=0)
reset_terminal
Reset a terminal session, closing and reopening it.
Parameters:
session(integer, optional): Session number (default: 0)reason(string, optional): Reason for the reset
Example:
await reset_terminal(session=0, reason="Environment corrupted")
Session Management
Sessions allow maintaining separate execution contexts:
- Session 0: Default session for general commands
- Session 1+: Additional sessions for isolated environments
Each session maintains:
- Shell state (environment variables, working directory)
- Command history
- Output buffer
Virtual Environment Considerations
Important: When the MCP server is launched from a virtual environment, shell sessions may NOT automatically inherit the venv activation.
Solution: Use init commands to explicitly activate your virtual environment:
{
"env": {
"CODE_EXEC_INIT_COMMANDS": "source /path/to/venv/bin/activate"
}
}
Platform Support
- Linux: Fully tested and supported
- macOS: Fully tested and supported
- Windows: Experimental support via pywinpty
- Install with:
pip install -e ".[windows]" - Use
cmd.exeorpowershell.exeas executable - Some features may behave differently
- Install with:
Architecture
This MCP server is a minimal wrapper around Agent Zero's code execution tool:
- Preserves Agent Zero's battle-tested logic
- No rewrites or reimplementations
- Uses Agent Zero's helper modules unchanged:
tty_session.py- TTY session managementshell_local.py- Local shell interfaceprint_style.py- Output styling and loggingstrings.py- String manipulation utilities
Security
WARNING: This MCP server allows full code execution on the host system. Security is the responsibility of the MCP client.
Only use with trusted AI agents and in controlled environments.
License
MIT License
This project wraps and reuses code from Agent Zero (Copyright (c) 2025 Agent Zero, s.r.o), which is licensed under the MIT License.
See the LICENSE file for full license text and attribution details.
Credits
Built on Agent Zero's proven code execution implementation.
This MCP server preserves and reuses Agent Zero's battle-tested code:
- Core execution logic from
code_execution_tool.py - Helper modules:
tty_session.py,shell_local.py,print_style.py,strings.py - All system message prompts
All credit for the robust code execution implementation goes to the Agent Zero team.
Uses FastMCP for MCP protocol handling.
推荐服务器
Baidu Map
百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
Playwright MCP Server
一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。
Magic Component Platform (MCP)
一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。
Audiense Insights MCP Server
通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。
VeyraX
一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。
graphlit-mcp-server
模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。
Kagi MCP Server
一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。
e2b-mcp-server
使用 MCP 通过 e2b 运行代码。
Neon MCP Server
用于与 Neon 管理 API 和数据库交互的 MCP 服务器
Exa MCP Server
模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。