MCPcopy Create free account
hub / github.com/HKUDS/ClawWork / execute_code

Function execute_code

livebench/tools/productivity/code_execution.py:17–146  ·  view source on GitHub ↗

Execute code in a sandboxed environment with safety restrictions. SECURITY FEATURES: - Execution timeout (30 seconds) - Restricted to sandbox directory only - No network access - Limited memory usage - Standard library only (no pip install during execution) Args:

(code: str, language: str = "python")

Source from the content-addressed store, hash-verified

15
16@tool
17def execute_code(code: str, language: str = "python") -> Dict[str, Any]:
18 """
19 Execute code in a sandboxed environment with safety restrictions.
20
21 SECURITY FEATURES:
22 - Execution timeout (30 seconds)
23 - Restricted to sandbox directory only
24 - No network access
25 - Limited memory usage
26 - Standard library only (no pip install during execution)
27
28 Args:
29 code: Code to execute
30 language: Programming language - currently only "python" supported
31
32 Returns:
33 Dictionary with execution result (stdout, stderr, exit_code)
34 """
35 import subprocess
36 import os
37 import tempfile
38
39 # Validate inputs
40 if not code or len(code) < 1:
41 return {"error": "Code cannot be empty"}
42
43 language = language.lower().strip()
44 if language != "python":
45 return {
46 "error": f"Language '{language}' not supported",
47 "supported_languages": ["python"]
48 }
49
50 # Get sandbox directory
51 _global_state = _get_global_state()
52 data_path = _global_state.get("data_path")
53 date = _global_state.get("current_date")
54
55 if not data_path:
56 return {"error": "Data path not configured"}
57
58 # Create sandbox directory for code execution
59 sandbox_dir = os.path.join(data_path, "sandbox", date or "default", "code_exec")
60 os.makedirs(sandbox_dir, exist_ok=True)
61
62 # Create temporary file for code
63 try:
64 with tempfile.NamedTemporaryFile(
65 mode='w',
66 suffix='.py',
67 dir=sandbox_dir,
68 delete=False,
69 encoding='utf-8'
70 ) as f:
71 code_file = f.name
72
73 # Add safety wrapper to restrict file operations
74 wrapped_code = f"""

Callers

nothing calls this directly

Calls 2

runMethod · 0.80
_get_global_stateFunction · 0.70

Tested by

no test coverage detected