MCPcopy Index your code
hub / github.com/HKUDS/DeepCode / execute_python

Function execute_python

tools/code_implementation_server.py:687–763  ·  view source on GitHub ↗

Execute Python code and return output Args: code: Python code to execute timeout: Timeout in seconds Returns: JSON string of execution result

(code: str, timeout: int = 30)

Source from the content-addressed store, hash-verified

685
686@mcp.tool()
687async def execute_python(code: str, timeout: int = 30) -> str:
688 """
689 Execute Python code and return output
690
691 Args:
692 code: Python code to execute
693 timeout: Timeout in seconds
694
695 Returns:
696 JSON string of execution result
697 """
698 try:
699 # Create temporary file
700 with tempfile.NamedTemporaryFile(
701 mode="w", suffix=".py", delete=False, encoding="utf-8"
702 ) as f:
703 f.write(code)
704 temp_file = f.name
705
706 try:
707 # Ensure workspace directory exists
708 ensure_workspace_exists()
709
710 # Execute Python code
711 result = subprocess.run(
712 [sys.executable, temp_file],
713 cwd=WORKSPACE_DIR,
714 capture_output=True,
715 text=True,
716 timeout=timeout,
717 encoding="utf-8",
718 )
719
720 execution_result = {
721 "status": "success" if result.returncode == 0 else "error",
722 "return_code": result.returncode,
723 "stdout": result.stdout,
724 "stderr": result.stderr,
725 "timeout": timeout,
726 }
727
728 if result.returncode != 0:
729 execution_result["message"] = "Python code execution failed"
730 else:
731 execution_result["message"] = "Python code execution successful"
732
733 log_operation(
734 "execute_python",
735 {
736 "return_code": result.returncode,
737 "stdout_length": len(result.stdout),
738 "stderr_length": len(result.stderr),
739 },
740 )
741
742 return json.dumps(execution_result, ensure_ascii=False, indent=2)
743
744 finally:

Callers

nothing calls this directly

Calls 3

ensure_workspace_existsFunction · 0.85
log_operationFunction · 0.85
runMethod · 0.80

Tested by

no test coverage detected