MCPcopy Create free account
hub / github.com/ScaleML/AgentSPEX / process_tool_calls

Method process_tool_calls

src/mcp_client/client.py:327–385  ·  view source on GitHub ↗
(self, msg: Any)

Source from the content-addressed store, hash-verified

325 return wrapper
326
327 def process_tool_calls(self, msg: Any) -> List[dict]:
328 tool_calls = msg.tool_calls or []
329 tool_calls_msg = []
330 tool_results_msg = []
331
332 if not tool_calls:
333 return [], []
334
335 tool_calls_msg.append(
336 {
337 "role": "assistant",
338 "content": msg.content or "",
339 "tool_calls": [
340 {
341 "id": tc.id,
342 "type": "function",
343 "function": {
344 "name": tc.function.name,
345 "arguments": tc.function.arguments,
346 },
347 }
348 for tc in tool_calls
349 ],
350 }
351 )
352
353 for tc in tool_calls:
354 name = tc.function.name
355 try:
356 args = (
357 json.loads(tc.function.arguments)
358 if isinstance(tc.function.arguments, str)
359 else tc.function.arguments
360 ) or {}
361 except Exception:
362 args = {}
363 try:
364 result_dict = self.invoke(name, args, is_catch_exception=False)
365 except Exception as e:
366 tool_results_msg.append(
367 {
368 "role": "tool",
369 "tool_call_id": tc.id,
370 "name": name,
371 "content": f"error: Tool invocation failed with error {e}",
372 }
373 )
374 print(f"Tool {name} failed with error: {e}")
375 continue
376
377 tool_results_msg.append(
378 {
379 "role": "tool",
380 "tool_call_id": tc.id,
381 "name": name,
382 "content": json.dumps(result_dict, default=str),
383 }
384 )

Callers

nothing calls this directly

Calls 1

invokeMethod · 0.95

Tested by

no test coverage detected