Executes Python code in the configured environment. Best for short code snippets.
Installs Python packages in the environment.
Checks if packages are already installed in the environment.
Dynamically changes the environment configuration.
Gets the current environment configuration.
Creates a new Python file with initial content. Use this as the first step for longer code that may exceed token limits.
Appends content to an existing Python code file. Use this to add more code to a file created with initialize_code_file.
Executes an existing Python file. Use this as the final step after building up code with initialize_code_file and append_to_code_file.
Reads 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.
The Python Code Executor allows LLMs to execute Python code within a specified environment (Conda, virtualenv, or UV virtualenv). This enables AI assistants to run code with access to libraries and dependencies defined in the environment. With support for incremental code generation, this tool overcomes token limitations by allowing code to be built up in stages. It provides a complete workflow for code execution, from environment configuration and dependency management to code generation and execution.
The Python Code Executor provides a secure and flexible way for AI assistants to execute Python code. It supports running code within different Python environments (Conda, virtualenv, or UV virtualenv), allowing access to specific libraries and dependencies.
One of the key features is support for incremental code generation, which helps overcome token limitations when working with large code blocks. The executor also provides tools for managing dependencies, checking installed packages, and configuring the environment dynamically.
git clone https://github.com/bazinga012/mcp_code_executor.git
cd mcp_code_executor
npm install
npm run build
git clone https://github.com/bazinga012/mcp_code_executor.git
docker build -t mcp-code-executor .
The server requires configuration through environment variables:
Required:
CODE_STORAGE_DIR
: Directory where generated code will be storedEnvironment Type (choose one setup):
ENV_TYPE
: Set to conda
CONDA_ENV_NAME
: Name of the Conda environment to useENV_TYPE
: Set to venv
VENV_PATH
: Path to the virtualenv directoryENV_TYPE
: Set to venv-uv
UV_VENV_PATH
: Path to the UV virtualenv directoryOnce configured, the Python Code Executor allows AI assistants to execute Python code by generating files in the specified storage directory and running them within the configured environment.
For code that might exceed token limits:
initialize_code_file
to create a new file with initial contentappend_to_code_file
to add more code in subsequent messagesexecute_code_file
to run the complete codeThis approach allows building up complex code across multiple interactions.