Safe Python interpreter based on HF Smolagents `LocalPythonExecutor`
This document details how to set up a secure local Python executor that seamlessly integrates with Large Language Model (LLM) applications using the Model Context Protocol (MCP). By leveraging Hugging Face's LocalPythonExecutor
from the smolagents
framework, this solution provides a sandboxed Python environment for executing code generated by LLMs, offering a balance between security and ease of use. This is particularly useful for applications like Claude Desktop, where it can function as a Code Interpreter, similar to the plugin available in ChatGPT.
run_python
Tool Exposure: Provides a readily accessible tool for executing Python code snippets.eval()
usage.uv
-Powered Virtual Environment: Operates within a dedicated Python virtual environment managed by uv
, ensuring dependency isolation.collections
datetime
itertools
math
queue
random
re
stat
statistics
time
unicodedata
Executing code generated by LLMs on your local machine requires careful consideration of security implications. While solutions involving command-line execution or direct eval()
should be avoided due to inherent risks, options like VMs or Docker containers offer robust isolation but can be resource-intensive and complex to set up. Third-party Python runtime services provide another alternative, but often require registration and API keys.
This MCP server, built upon Hugging Face's LocalPythonExecutor
, strikes a balance between security and convenience. It offers a more secure execution environment compared to direct Python interpretation while remaining easier to set up than containerized or remote solutions.
As highlighted by the Hugging Face team:
To add a first layer of security, code execution in smolagents is not performed by the vanilla Python interpreter. We have re-built a more secure LocalPythonExecutor from the ground up.
For a deeper understanding of the security measures implemented, refer to the official documentation.
uv
: Follow the official installation instructions for your operating system (e.g., brew install uv
on macOS).git clone <repository_url>
and cd mcp_safe_local_python_executor
.uv run mcp_server.py
. This command automatically creates a virtual environment, installs necessary dependencies (smolagents, mcp), and starts the MCP server.~/Library/Application Support/Claude/claude_desktop_config.json
%APPDATA%\Claude\claude_desktop_config.json
{ "mcpServers": { "safe-local-python-executor": { "command": "uv", "args": [ "--directory", "/path/to/mcp_local_python_executor/", "run", "mcp_server.py" ] } } }
* **Important:** Replace `/path/to/mcp_local_python_executor/` with the actual path to the cloned repository.
4. Restart Claude Desktop: This ensures the new configuration is loaded. 5. Access the Python Executor: The Python executor tool will now be available within Claude, indicated by a hammer icon in the message input field.
git clone <repository_url>
.uv venv .venv
.uv sync --group dev
.python -m pytest tests/
.๐ ๐ Autonomous shell execution, computer control and coding agent. (Mac)
๐ MCP server that can execute commands such as keyboard input and mouse movement
๐ ๐ ๐ฆ Analyzes your codebase identifying important files based on dependency relationships. Generates diagrams and importance scores, helping AI assistants understand the codebase.
๐ ๐ An MCP Server and VS Code Extension which enables (language agnostic) automatic debugging via breakpoints and expression evaluation.