### Install MCP Executor with Smithery CLI Source: https://context7.com/maxim-saplin/mcp_safe_local_python_executor/llms.txt Use the Smithery CLI to automatically install and configure the MCP executor for Claude Desktop. This command simplifies the setup process. ```bash # --- Smithery auto-install (installs and configures for Claude Desktop) --- npx -y @smithery/cli install @maxim-saplin/mcp_safe_local_python_executor --client claude ``` -------------------------------- ### Setup Development Environment with uv Source: https://context7.com/maxim-saplin/mcp_safe_local_python_executor/llms.txt Create a virtual environment using `uv` and install all project dependencies, including the development group. This is the initial step for local development and testing. ```bash # Create virtualenv and install all dependencies (including dev group) uv venv .venv uv sync --group dev ``` -------------------------------- ### Development Setup and Testing Source: https://github.com/maxim-saplin/mcp_safe_local_python_executor/blob/main/README.md Commands for setting up the development environment, installing dependencies, and running tests using `uv` and `pytest`. ```bash uv venv .venv uv sync --group dev python -m pytest tests/ ``` -------------------------------- ### Build and Run MCP Executor with Docker Source: https://context7.com/maxim-saplin/mcp_safe_local_python_executor/llms.txt Build a Docker image for the safe-python-executor and run it. This method provides a pre-built environment with all dependencies installed, enabling offline container-based deployments. ```bash # --- Docker (pre-built venv, no runtime downloads) --- docker build -t safe-python-executor . docker run --rm -i safe-python-executor # Runs: uv run mcp_server.py (CMD in Dockerfile) ``` -------------------------------- ### Install Safe Local Python Executor via Smithery Source: https://github.com/maxim-saplin/mcp_safe_local_python_executor/blob/main/README.md Use the Smithery CLI to automatically install the Safe Local Python Executor for Claude Desktop. ```bash npx -y @smithery/cli install @maxim-saplin/mcp_safe_local_python_executor --client claude ``` -------------------------------- ### Configure MCP Server for Claude Desktop Source: https://context7.com/maxim-saplin/mcp_safe_local_python_executor/llms.txt Configure Claude Desktop to use the safe-local-python-executor by specifying the command and arguments in `claude_desktop_config.json`. Ensure the path to the repository is absolute. ```json # --- Claude Desktop configuration (claude_desktop_config.json) --- # macOS: ~/Library/Application Support/Claude/claude_desktop_config.json # Windows: %APPDATA%\Claude\claude_desktop_config.json { "mcpServers": { "safe-local-python-executor": { "command": "uv", "args": [ "--directory", "/absolute/path/to/mcp_safe_local_python_executor/", "run", "mcp_server.py" ] } } } ``` -------------------------------- ### Launch MCP Server with Stdio Transport Source: https://context7.com/maxim-saplin/mcp_safe_local_python_executor/llms.txt Manually launch the MCP server using `uv` from the repository root. This is the standard transport for MCP tool servers integrated with desktop AI clients. ```bash # --- Manual launch (from repo root) --- uv run mcp_server.py # 2025-01-01 00:00:00 - root - INFO - Starting MCP server for Python executor ``` -------------------------------- ### Configure Claude Desktop MCP Servers Source: https://github.com/maxim-saplin/mcp_safe_local_python_executor/blob/main/README.md Add the Safe Local Python Executor configuration to your Claude Desktop settings to enable it as an MCP server. ```json { "mcpServers": { "safe-local-python-executor": { "command": "uv", "args": [ "--directory", "/path/to/mcp_local_python_executor/", "run", "mcp_server.py" ] } } } ``` -------------------------------- ### Run Full Test Suite with pytest Source: https://context7.com/maxim-saplin/mcp_safe_local_python_executor/llms.txt Execute the entire test suite using `pytest` from the command line. This command runs tests in both `test_local_python_executor.py` and `test_mcp_server.py`. ```bash # Run the full test suite python -m pytest tests/ -v ``` -------------------------------- ### run_python - Execute sandboxed Python code via MCP Source: https://context7.com/maxim-saplin/mcp_safe_local_python_executor/llms.txt The primary MCP tool exposed by the server. It accepts a string of valid Python 3 code, runs it inside the `LocalPythonExecutor` sandbox, and returns a dictionary containing `result` and `logs`. The sandbox enforces a fixed import allowlist and blocks all file-system, network, and subprocess operations. ```APIDOC ## run_python ### Description Executes sandboxed Python code and returns the result and any print output. ### Method MCP Tool Call ### Parameters #### Request Body - **code** (string) - Required - The Python code to execute. ### Response #### Success Response (200) - **result** (any) - The computed value of the last expression or the `result` variable. - **logs** (string) - All captured `print` output. ### Request Example ```python import asyncio import sys, os sys.path.insert(0, os.path.abspath("..\')) # point to repo root import mcp_server response = asyncio.run(mcp_server.run_python("result = 2 + 2")) print(response) ``` ### Response Example ```json { "result": 4, "logs": "" } ``` ### Error Handling - **InterpreterError**: Raised if the code attempts to perform disallowed operations (e.g., unauthorized imports, file I/O, network access, subprocesses). ### Request Example (Blocked Import) ```python import pytest import asyncio import sys, os sys.path.insert(0, os.path.abspath("..\')) # point to repo root with pytest.raises(Exception) as exc_info: asyncio.run(mcp_server.run_python("import os\nresult = os.listdir('.')")) print(exc_info.value) ``` ### Response Example (Blocked Import) ``` InterpreterError: Import of os is not allowed. ... ``` ``` -------------------------------- ### Run Specific Test File with pytest Source: https://context7.com/maxim-saplin/mcp_safe_local_python_executor/llms.txt Execute tests from a specific file, `test_mcp_server.py`, using `pytest`. This is useful for focusing on a particular set of tests. ```bash # Run a specific test file python -m pytest tests/test_mcp_server.py -v ``` -------------------------------- ### Execute Python Code via MCP Tool Source: https://context7.com/maxim-saplin/mcp_safe_local_python_executor/llms.txt Use the `run_python` MCP tool to execute Python code in a sandboxed environment. It returns the computed result and any print output. Ensure code produces a `result` variable or ends with an expression for a meaningful return value. ```python # Called internally by the MCP framework; direct async usage from tests: import asyncio import sys, os sys.path.insert(0, os.path.abspath("..")) # point to repo root import mcp_server # --- Basic arithmetic --- response = asyncio.run(mcp_server.run_python("result = 2 + 2")) print(response) # {"result": 4, "logs": ""} ``` ```python # --- Using an allowed stdlib module --- response = asyncio.run(mcp_server.run_python( "import math\nresult = math.sqrt(16)" )) print(response) # {"result": 4.0, "logs": ""} ``` ```python # --- Capturing print output --- response = asyncio.run(mcp_server.run_python( "print('hello')\nresult = 42" )) print(response) # {"result": 42, "logs": "hello\n"} ``` ```python # --- Complex computation: primes up to 100 --- code = """ def is_prime(n): if n < 2: return False for i in range(2, int(n**0.5) + 1): if n % i == 0: return False return True primes = [num for num in range(2, 101) if is_prime(num)] result = primes """ response = asyncio.run(mcp_server.run_python(code)) print(response["result"]) # [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, # 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97] ``` ```python # --- Blocked import raises an exception --- import pytest with pytest.raises(Exception) as exc_info: asyncio.run(mcp_server.run_python("import os\nresult = os.listdir('.')")) print(exc_info.value) # InterpreterError: Import of os is not allowed. ... ``` -------------------------------- ### Low-level Sandboxed Python Interpreter Source: https://context7.com/maxim-saplin/mcp_safe_local_python_executor/llms.txt Utilize `LocalPythonExecutor` for direct sandboxed Python execution. It returns the result, logs, and a third unused value. The sandbox restricts imports, file I/O, and network access. ```python from smolagents.local_python_executor import LocalPythonExecutor # Instantiate with no extra imports executor = LocalPythonExecutor(additional_authorized_imports=[]) executor.send_tools({}) # no external tools # Simple expression — last expression value is the result result, logs, _ = executor("2 + 2") assert result == 4 assert logs == "" ``` ```python # Variable assignment — explicit `result` variable result, logs, _ = executor(""" x = 10 y = 20 result = x + y """) assert result == 30 ``` ```python # List iteration result, logs, _ = executor(""" numbers = [1, 2, 3, 4, 5] total = 0 for num in numbers: total += num total """) assert result == 15 ``` ```python # Blocked import — raises InterpreterError import pytest with pytest.raises(Exception) as exc_info: executor("import os\nos.listdir('.')") assert "Import of os is not allowed" in str(exc_info.value) ``` -------------------------------- ### LocalPythonExecutor - Low-level sandboxed interpreter Source: https://context7.com/maxim-saplin/mcp_safe_local_python_executor/llms.txt The underlying execution engine from `smolagents`. It is callable and executes Python code within a restricted sandbox, blocking unauthorized imports and file-system operations. ```APIDOC ## LocalPythonExecutor ### Description Provides a low-level interface to the sandboxed Python interpreter. ### Method Instantiate and call the executor object. ### Parameters #### Initialization - **additional_authorized_imports** (list of strings) - Optional - A list of additional modules allowed to be imported. Defaults to an empty list, using only the built-in allowlist. #### Execution - **code** (string) - Required - The Python code to execute. ### Request Example (Instantiation) ```python from smolagents.local_python_executor import LocalPythonExecutor executor = LocalPythonExecutor(additional_authorized_imports=[]) executor.send_tools({}) ``` ### Response #### Success Response (Execution) - **result** (any) - The computed value of the last expression or the `result` variable. - **logs** (string) - All captured `print` output. - **_** (any) - An unused third return value. ### Request Example (Simple Arithmetic) ```python result, logs, _ = executor("2 + 2") assert result == 4 assert logs == "" ``` ### Response Example (Simple Arithmetic) ```json { "result": 4, "logs": "" } ``` ### Request Example (Variable Assignment) ```python result, logs, _ = executor("\nx = 10\ny = 20\nresult = x + y\n") assert result == 30 ``` ### Response Example (Variable Assignment) ```json { "result": 30, "logs": "" } ``` ### Error Handling - **InterpreterError**: Raised if the code attempts to perform disallowed operations (e.g., unauthorized imports, file I/O, network access, subprocesses). ### Request Example (Blocked Import) ```python import pytest with pytest.raises(Exception) as exc_info: executor("import os\nos.listdir('.')") assert "Import of os is not allowed" in str(exc_info.value) ``` ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.