MCPcopy Create free account
hub / github.com/AQ-MedAI/MedMemoryBench / run_tool_from_source

Method run_tool_from_source

methods/letta/server/server.py:1281–1337  ·  view source on GitHub ↗

Run a tool from source code

(
        self,
        actor: User,
        tool_args: Dict[str, str],
        tool_source: str,
        tool_env_vars: Optional[Dict[str, str]] = None,
        tool_source_type: Optional[str] = None,
        tool_name: Optional[str] = None,
        tool_args_json_schema: Optional[Dict[str, Any]] = None,
        tool_json_schema: Optional[Dict[str, Any]] = None,
    )

Source from the content-addressed store, hash-verified

1279 return letta_agent.get_context_window()
1280
1281 def run_tool_from_source(
1282 self,
1283 actor: User,
1284 tool_args: Dict[str, str],
1285 tool_source: str,
1286 tool_env_vars: Optional[Dict[str, str]] = None,
1287 tool_source_type: Optional[str] = None,
1288 tool_name: Optional[str] = None,
1289 tool_args_json_schema: Optional[Dict[str, Any]] = None,
1290 tool_json_schema: Optional[Dict[str, Any]] = None,
1291 ) -> ToolReturnMessage:
1292 """Run a tool from source code"""
1293 if tool_source_type is not None and tool_source_type != "python":
1294 raise ValueError("Only Python source code is supported at this time")
1295
1296 # If tools_json_schema is explicitly passed in, override it on the created Tool object
1297 if tool_json_schema:
1298 tool = Tool(name=tool_name, source_code=tool_source, json_schema=tool_json_schema)
1299 else:
1300 # NOTE: we're creating a floating Tool object and NOT persisting to DB
1301 tool = Tool(
1302 name=tool_name,
1303 source_code=tool_source,
1304 args_json_schema=tool_args_json_schema,
1305 )
1306
1307 assert tool.name is not None, "Failed to create tool object"
1308
1309 # TODO eventually allow using agent state in tools
1310 agent_state = None
1311
1312 # Next, attempt to run the tool with the sandbox
1313 try:
1314 sandbox_run_result = ToolExecutionSandbox(tool.name, tool_args, actor, tool_object=tool).run(
1315 agent_state=agent_state, additional_env_vars=tool_env_vars
1316 )
1317 return ToolReturnMessage(
1318 id="null",
1319 tool_call_id="null",
1320 date=get_utc_time(),
1321 status=sandbox_run_result.status,
1322 tool_return=str(sandbox_run_result.func_return),
1323 stdout=sandbox_run_result.stdout,
1324 stderr=sandbox_run_result.stderr,
1325 )
1326
1327 except Exception as e:
1328 func_return = get_friendly_error_msg(function_name=tool.name, exception_name=type(e).__name__, exception_message=str(e))
1329 return ToolReturnMessage(
1330 id="null",
1331 tool_call_id="null",
1332 date=get_utc_time(),
1333 status="error",
1334 tool_return=func_return,
1335 stdout=[],
1336 stderr=[traceback.format_exc()],
1337 )
1338

Callers 1

run_tool_from_sourceFunction · 0.45

Calls 6

ToolClass · 0.90
ToolReturnMessageClass · 0.90
get_utc_timeFunction · 0.90
get_friendly_error_msgFunction · 0.90
runMethod · 0.45

Tested by

no test coverage detected