Skip to main content
Glama
maxim-saplin

Safe Local Python Executor/Interpreter

run_python

Execute Python code in a secure sandbox environment for calculations and data manipulation. The tool runs simple Python scripts with restricted imports and returns results with logs.

Instructions

Execute Python code in a secure sandbox environment.

This tool allows running simple Python code for calculations and data manipulations.
The execution environment is restricted for security purposes. Make sure you create a single file
that can be executed in one go and it returns a result.

Default allowed imports:
- math
- random 
- datetime
- time
- json
- re
- string
- collections
- itertools
- functools
- operator

Args:
    code: The Python code to execute. Must be valid Python 3 code. The result must be stored in a variable called `result`. E.g.:
    ```python
    import math
    result = math.sqrt(16)
    ```
    
Returns:
    A dictionary with execution results containing:
    - result: The final value or None if no value is returned
    - logs: Any output from print statements

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
codeYes

Output Schema

TableJSON Schema
NameRequiredDescriptionDefault
resultYes

Implementation Reference

  • The core handler function for the 'run_python' tool. It is registered via the @mcp.tool() decorator. Executes the provided Python code using a LocalPythonExecutor sandbox and returns the result and logs. The docstring provides the input/output schema details.
    @mcp.tool()
    async def run_python(
        code: str
    ) -> Dict[str, Any]:
        """Execute Python code in a secure sandbox environment.
        
        This tool allows running simple Python code for calculations and data manipulations.
        The execution environment is restricted for security purposes. Make sure you create a single file
        that can be executed in one go and it returns a result.
    
        Default allowed imports:
        - math
        - random 
        - datetime
        - time
        - json
        - re
        - string
        - collections
        - itertools
        - functools
        - operator
        
        Args:
            code: The Python code to execute. Must be valid Python 3 code. The result must be stored in a variable called `result`. E.g.:
            ```python
            import math
            result = math.sqrt(16)
            ```
            
        Returns:
            A dictionary with execution results containing:
            - result: The final value or None if no value is returned
            - logs: Any output from print statements
        """
        logger.info(f"Executing Python code: {code}")
        
        result, logs, _ = executor(code)
    
        response = {
            "result": result,
            "logs": logs
        }
        
        logger.info(f"Execution result: {result}")
        return response
  • Initialization of the LocalPythonExecutor used by the run_python tool to safely execute Python code.
    executor = LocalPythonExecutor(additional_authorized_imports=[])
    executor.send_tools({})
  • mcp_server.py:24-24 (registration)
    Creation of the FastMCP server instance named 'python-executor', to which tools like run_python are registered.
    mcp = FastMCP("python-executor")

Tool Definition Quality

Score is being calculated. Check back soon.

Install Server

Other Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/maxim-saplin/mcp_safe_local_python_executor'

If you have feedback or need assistance with the MCP directory API, please join our Discord server