Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions src/huggingface_hub/inference/_mcp/mcp_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,19 @@ async def process_single_turn_with_tools(
# Process tool calls one by one
for tool_call in final_tool_calls.values():
function_name = tool_call.function.name
function_args = json.loads(tool_call.function.arguments or "{}")
try:
function_args = json.loads(tool_call.function.arguments or "{}")
except json.JSONDecodeError as err:
tool_message = {
"role": "tool",
"tool_call_id": tool_call.id,
"name": function_name,
"content": f"Invalid JSON generated by the model: {err}",
}
tool_message_as_obj = ChatCompletionInputMessage.parse_obj_as_instance(tool_message)
messages.append(tool_message_as_obj)
yield tool_message_as_obj
continue # move to next tool call

tool_message = {"role": "tool", "tool_call_id": tool_call.id, "content": "", "name": function_name}

Expand All @@ -324,11 +336,13 @@ async def process_single_turn_with_tools(
# Execute tool call with the appropriate session
session = self.sessions.get(function_name)
if session is not None:
result = await session.call_tool(function_name, function_args)
tool_message["content"] = format_result(result)
try:
result = await session.call_tool(function_name, function_args)
tool_message["content"] = format_result(result)
except Exception as err:
tool_message["content"] = f"Error: MCP tool call failed with error message: {err}"
Comment on lines +342 to +343
Copy link

Copilot AI Jun 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid catching the broad Exception; consider handling specific exceptions thrown by session.call_tool or re-raising unexpected errors to avoid masking serious issues.

Suggested change
except Exception as err:
tool_message["content"] = f"Error: MCP tool call failed with error message: {err}"
except (ValueError, TypeError, json.JSONDecodeError) as err:
tool_message["content"] = f"Error: MCP tool call failed with error message: {err}"
except Exception as err:
logger.error(f"Unexpected error during MCP tool call: {err}")
raise

Copilot uses AI. Check for mistakes.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree in general but disagree on this specific use case. Let's keep it like this

else:
error_msg = f"Error: No session found for tool: {function_name}"
tool_message["content"] = error_msg
tool_message["content"] = f"Error: No session found for tool: {function_name}"

# Yield tool message
tool_message_as_obj = ChatCompletionInputMessage.parse_obj_as_instance(tool_message)
Expand Down