from crewai.tools import BaseTool import subprocess import tempfile class PythonExecTool(BaseTool): name: str = "Python Execution Tool" description: str = "Executes Python code safely" def _run(self, code: str): with tempfile.NamedTemporaryFile(mode="w", suffix=".py", delete=False) as file: file.write(code) file_path = file.name try: result = subprocess.check_output( ["python", file_path], stderr=subprocess.STDOUT, text=True, ) return result except subprocess.CalledProcessError as error: return error.output