MCPcopy Create free account
hub / github.com/modelscope/ms-agent / execute_python_function

Method execute_python_function

ms_agent/skill/container.py:1073–1130  ·  view source on GitHub ↗

Execute a Python function directly (local execution, not sandboxed). Note: Function execution runs locally as it cannot be serialized to sandbox. Use execute_python_code for sandboxed execution. Args: func: Python callable to execute. skill_

(
            self,
            func: Callable,
            skill_id: str = 'unknown',
            input_spec: ExecutionInput = None)

Source from the content-addressed store, hash-verified

1071 return '\n'.join(lines)
1072
1073 def execute_python_function(
1074 self,
1075 func: Callable,
1076 skill_id: str = 'unknown',
1077 input_spec: ExecutionInput = None) -> ExecutionOutput:
1078 """
1079 Execute a Python function directly (local execution, not sandboxed).
1080
1081 Note: Function execution runs locally as it cannot be serialized to sandbox.
1082 Use execute_python_code for sandboxed execution.
1083
1084 Args:
1085 func: Python callable to execute.
1086 skill_id: Identifier of the skill being executed.
1087 input_spec: Input specification with args and kwargs.
1088
1089 Returns:
1090 ExecutionOutput with results.
1091 """
1092 input_spec = input_spec or ExecutionInput()
1093
1094 record = self._create_record(
1095 skill_id=skill_id,
1096 executor_type=ExecutorType.PYTHON_FUNCTION,
1097 input_spec=input_spec,
1098 function_name=func.__name__)
1099 record.sandbox_used = False # Local execution
1100
1101 record.start_time = datetime.now()
1102 record.status = ExecutionStatus.RUNNING
1103
1104 try:
1105 # Add helper paths to kwargs
1106 kwargs = input_spec.kwargs.copy()
1107 kwargs['_output_dir'] = self.output_dir
1108
1109 start_time = datetime.now()
1110 return_value = func(*input_spec.args, **kwargs)
1111 end_time = datetime.now()
1112
1113 output = ExecutionOutput(
1114 return_value=return_value,
1115 exit_code=0,
1116 output_files=self._collect_output_files(),
1117 duration_ms=(end_time - start_time).total_seconds() * 1000)
1118
1119 record.status = ExecutionStatus.SUCCESS
1120
1121 except Exception as e:
1122 output = ExecutionOutput(stderr=str(e), exit_code=-1)
1123 record.status = ExecutionStatus.FAILED
1124 record.error_message = str(e)
1125 logger.error(f'Python function execution failed: {e}')
1126
1127 record.end_time = datetime.now()
1128 record.output_spec = output
1129 self.spec.add_record(record)
1130 return output

Callers 1

executeMethod · 0.95

Calls 5

_create_recordMethod · 0.95
_collect_output_filesMethod · 0.95
ExecutionInputClass · 0.85
ExecutionOutputClass · 0.85
add_recordMethod · 0.80

Tested by

no test coverage detected