run_python
Execute Python code in a secure sandbox environment for calculations and data manipulations. Restricted imports and a single-file execution ensure safety and simplicity, with results stored in a variable named 'result'.
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
| Name | Required | Description | Default |
|---|---|---|---|
| code | Yes |
Input Schema (JSON Schema)
{
"properties": {
"code": {
"title": "Code",
"type": "string"
}
},
"required": [
"code"
],
"title": "run_pythonArguments",
"type": "object"
}
Implementation Reference
- mcp_server.py:30-75 (handler)The main handler function for the 'run_python' tool. It is decorated with @mcp.tool() for registration in the MCP server. Executes the provided Python code using the LocalPythonExecutor instance and returns a dictionary containing the result and logs.@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
- mcp_server.py:30-30 (registration)The @mcp.tool() decorator registers the run_python function as an MCP tool.@mcp.tool()
- mcp_server.py:31-33 (schema)Input schema: code: str. Output: Dict[str, Any] with 'result' and 'logs'. Detailed in docstring.async def run_python( code: str ) -> Dict[str, Any]:
- mcp_server.py:26-27 (helper)Initialization of the LocalPythonExecutor used by run_python tool.executor = LocalPythonExecutor(additional_authorized_imports=[]) executor.send_tools({})