Skip to content

Commit 987dadc

Browse files
[MCP] reinject JSON parse & runtime tool errors back into the chat history (#3137)
* re-inject tool parsing errors into the conversation * style
1 parent b4c01ed commit 987dadc

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

src/huggingface_hub/inference/_mcp/mcp_client.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,19 @@ async def process_single_turn_with_tools(
310310
# Process tool calls one by one
311311
for tool_call in final_tool_calls.values():
312312
function_name = tool_call.function.name
313-
function_args = json.loads(tool_call.function.arguments or "{}")
313+
try:
314+
function_args = json.loads(tool_call.function.arguments or "{}")
315+
except json.JSONDecodeError as err:
316+
tool_message = {
317+
"role": "tool",
318+
"tool_call_id": tool_call.id,
319+
"name": function_name,
320+
"content": f"Invalid JSON generated by the model: {err}",
321+
}
322+
tool_message_as_obj = ChatCompletionInputMessage.parse_obj_as_instance(tool_message)
323+
messages.append(tool_message_as_obj)
324+
yield tool_message_as_obj
325+
continue # move to next tool call
314326

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

@@ -324,11 +336,13 @@ async def process_single_turn_with_tools(
324336
# Execute tool call with the appropriate session
325337
session = self.sessions.get(function_name)
326338
if session is not None:
327-
result = await session.call_tool(function_name, function_args)
328-
tool_message["content"] = format_result(result)
339+
try:
340+
result = await session.call_tool(function_name, function_args)
341+
tool_message["content"] = format_result(result)
342+
except Exception as err:
343+
tool_message["content"] = f"Error: MCP tool call failed with error message: {err}"
329344
else:
330-
error_msg = f"Error: No session found for tool: {function_name}"
331-
tool_message["content"] = error_msg
345+
tool_message["content"] = f"Error: No session found for tool: {function_name}"
332346

333347
# Yield tool message
334348
tool_message_as_obj = ChatCompletionInputMessage.parse_obj_as_instance(tool_message)

0 commit comments

Comments
 (0)