MCP Code Executor
MCP代码执行器
MCP 代码执行器是一个 MCP 服务器,允许 LLM 在指定的 Python 环境中执行 Python 代码。这使得 LLM 能够运行代码并访问环境中定义的库和依赖项。它还支持增量代码生成,以处理可能超出令牌限制的大型代码块。
特征
从 LLM 提示执行 Python 代码
支持增量代码生成以克服令牌限制
在指定环境中运行代码(Conda、virtualenv 或 UV virtualenv)
在需要时安装依赖项
检查软件包是否已安装
在运行时动态配置环境
可配置代码存储目录
Related MCP server: MCP Python Interpreter
先决条件
Node.js 已安装
以下之一:
安装了 Conda 并创建了所需的 Conda 环境
Python虚拟环境
UV虚拟环境
设置
克隆此存储库:
git clone https://github.com/bazinga012/mcp_code_executor.git导航到项目目录:
cd mcp_code_executor安装 Node.js 依赖项:
npm install构建项目:
npm run build配置
要配置 MCP Code Executor 服务器,请将以下内容添加到 MCP 服务器配置文件中:
使用 Node.js
{
"mcpServers": {
"mcp-code-executor": {
"command": "node",
"args": [
"/path/to/mcp_code_executor/build/index.js"
],
"env": {
"CODE_STORAGE_DIR": "/path/to/code/storage",
"ENV_TYPE": "conda",
"CONDA_ENV_NAME": "your-conda-env"
}
}
}
}使用 Docker
{
"mcpServers": {
"mcp-code-executor": {
"command": "docker",
"args": [
"run",
"-i",
"--rm",
"mcp-code-executor"
]
}
}
}注意: Dockerfile 仅针对 venv-uv 环境类型进行了测试。其他环境类型可能需要额外配置。
环境变量
必需变量
CODE_STORAGE_DIR:存储生成的代码的目录
环境类型(选择一种设置)
对于 Conda:
ENV_TYPE:设置为condaCONDA_ENV_NAME:要使用的 Conda 环境的名称
对于标准虚拟环境:
ENV_TYPE:设置为venvVENV_PATH:virtualenv 目录的路径
对于 UV Virtualenv:
ENV_TYPE:设置为venv-uvUV_VENV_PATH:UV 虚拟环境目录的路径
可用工具
MCP 代码执行器为 LLM 提供以下工具:
1. execute_code
在配置的环境中执行 Python 代码。最适合短代码片段。
{
"name": "execute_code",
"arguments": {
"code": "import numpy as np\nprint(np.random.rand(3,3))",
"filename": "matrix_gen"
}
}2. install_dependencies
在环境中安装 Python 包。
{
"name": "install_dependencies",
"arguments": {
"packages": ["numpy", "pandas", "matplotlib"]
}
}3. check_installed_packages
检查环境中是否已安装包。
{
"name": "check_installed_packages",
"arguments": {
"packages": ["numpy", "pandas", "non_existent_package"]
}
}4. configure_environment
动态改变环境配置。
{
"name": "configure_environment",
"arguments": {
"type": "conda",
"conda_name": "new_env_name"
}
}5. get_environment_config
获取当前环境配置。
{
"name": "get_environment_config",
"arguments": {}
}6. initialize_code_file
创建一个新的 Python 文件,其中包含初始内容。对于可能超出 token 限制的较长代码,请将此作为第一步。
{
"name": "initialize_code_file",
"arguments": {
"content": "def main():\n print('Hello, world!')\n\nif __name__ == '__main__':\n main()",
"filename": "my_script"
}
}7. append_to_code_file
将内容附加到现有的 Python 代码文件。使用此功能可以向使用 initialise_code_file 创建的文件添加更多代码。
{
"name": "append_to_code_file",
"arguments": {
"file_path": "/path/to/code/storage/my_script_abc123.py",
"content": "\ndef another_function():\n print('This was appended to the file')\n"
}
}8. execute_code_file
执行现有的 Python 文件。此操作是使用 initialize_code_file 和 append_to_code_file 构建代码后的最后一步。
{
"name": "execute_code_file",
"arguments": {
"file_path": "/path/to/code/storage/my_script_abc123.py"
}
}9. read_code_file
读取现有 Python 代码文件的内容。在附加更多内容或执行文件之前,使用此方法验证文件的当前状态。
{
"name": "read_code_file",
"arguments": {
"file_path": "/path/to/code/storage/my_script_abc123.py"
}
}用法
配置完成后,MCP 代码执行器将允许 LLM 通过在指定的CODE_STORAGE_DIR中生成文件并在配置的环境中运行它来执行 Python 代码。
LLM 可以通过在其提示中引用此 MCP 服务器来生成和执行代码。
处理大型代码块
对于可能超出 LLM 令牌限制的较大代码块,请使用增量代码生成方法:
使用
initialize_code_file初始化具有基本结构的文件使用
append_to_code_file在后续调用中添加更多代码如果需要,使用
read_code_file验证文件内容使用
execute_code_file执行完整代码
这种方法允许 LLM 编写复杂的、多部分的代码,而不会遇到令牌限制。
向后兼容性
此软件包保持与早期版本的向后兼容性。使用旧版本且仅指定了 Conda 环境的用户无需更改配置即可继续使用。
贡献
欢迎贡献!请打开一个问题或提交一个拉取请求。
执照
该项目已获得 MIT 许可。
Available Tools
9 toolsappend_to_code_fileA
Append content to an existing Python code file. Use this to add more code to a file created with initialize_code_file, allowing you to build up larger code bases in parts.
Inputs: file_path (required), content (required).
check_installed_packagesC
Check if packages are installed in the conda environment
Inputs: packages (required).
configure_environmentC
Change the environment configuration settings
Inputs: type (required), conda_name, venv_path, uv_venv_path.
execute_codeA
Execute Python code in the conda environment. For short code snippets only. For longer code, use initialize_code_file and append_to_code_file instead.
Inputs: code (required), filename.
execute_code_fileA
Execute an existing Python file. Use this as the final step after building up code with initialize_code_file and append_to_code_file.
Inputs: file_path (required).
get_environment_configB
Get the current environment configuration
Inputs: no named parameters.
initialize_code_fileA
Create a new Python file with initial content. Use this as the first step for longer code that may exceed token limits. Follow with append_to_code_file for additional code.
Inputs: content (required), filename.
install_dependenciesC
Install Python dependencies in the conda environment
Inputs: packages (required).
read_code_fileA
Read the content of an existing Python code file. Use this to verify the current state of a file before appending more content or executing it.
Inputs: file_path (required).
Tool Schema Changelog
Recent tool additions, removals, and schema changes observed during successful MCP inspections.
Loading tool schema changelog…
TDQS
Scored across 9 tools
Each tool has a clearly distinct purpose with no ambiguity. For example, initialize_code_file, append_to_code_file, and read_code_file handle different file operations, while execute_code and execute_code_file target different execution methods. The descriptions explicitly differentiate tools like execute_code (for short snippets) versus the file-based workflow.
All tool names follow a consistent verb_noun pattern using snake_case, such as append_to_code_file, check_installed_packages, and configure_environment. There are no deviations in naming style or convention across the set, making them predictable and readable.
With 9 tools, the count is well-scoped for a code execution server. Each tool earns its place by covering distinct aspects like file management, environment configuration, dependency handling, and code execution, without being overly sparse or bloated.
The tool set provides complete coverage for the code execution domain, including CRUD-like operations for files (initialize, append, read), environment management (configure, get config, install dependencies), and execution (code snippets, files). There are no obvious gaps, and the workflow from file creation to execution is fully supported.
Maintenance
Related MCP Connectors
Operate Linux, macOS and Windows from your LLM. Every action runs through an auditable allowlist.
Run Python and install PyPI packages from natural language. Hosted at https://cloudish.ai.
Build Apps and run code in 30 languages — sandboxed, with persistent sessions for agent loops.
I do everything related to coding and execution tasks
Related MCP Servers
- FlicenseNot gradedqualityNot gradedmaintenanceEnables LLMs to interact with Python environments, execute code or scripts, manage packages, and read/write files within an isolated working directory.-
- FlicenseNot gradedqualityAmaintenanceEnables LLMs to interact with Python environments, execute code, manage packages, and perform file operations within a configurable working directory.-
- FlicenseNot gradedqualityAmaintenanceEnables LLMs to interact with Python environments, execute code or scripts, manage packages, and read/write files within an isolated working directory.-
- FlicenseNot gradedqualityAmaintenanceEnables LLMs to execute Python code and scripts across system and conda environments, manage installed packages, and read or write text and binary files within an isolated working directory.-