-
Notifications
You must be signed in to change notification settings - Fork 759
[MCP] reinject JSON parse & runtime tool errors back into the chat history #3137
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR enhances process_single_turn_with_tools
to catch and re-inject JSON parsing and tool execution errors back into the chat history, preventing crashes when malformed JSON is produced or tool calls fail.
- Add
JSONDecodeError
handler to wrap malformed JSON from the model and yield an error message - Wrap tool execution in a
try/except
to catch runtime errors and report them as tool messages
Comments suppressed due to low confidence (2)
src/huggingface_hub/inference/_mcp/mcp_client.py:315
- Introduce unit tests covering the
JSONDecodeError
branch to verify that malformed JSON from the model is caught and properly yielded as a tool message.
except json.JSONDecodeError as err:
src/huggingface_hub/inference/_mcp/mcp_client.py:343
- [nitpick] Consider standardizing the error message format and avoid leaking raw exception text; for example, use a concise message and log the full exception separately.
tool_message["content"] = f"Error: MCP tool call failed with error message: {err}"
except Exception as err: | ||
tool_message["content"] = f"Error: MCP tool call failed with error message: {err}" |
There was a problem hiding this comment.
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.
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.
There was a problem hiding this comment.
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
The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
except Exception as err: | ||
tool_message["content"] = f"Error: MCP tool call failed with error message: {err}" |
There was a problem hiding this comment.
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
Copy-paste from the JS PR: huggingface/huggingface.js#1511 which I find useful.
this should prevents crashes when the model emits malformed JSON and gives the LLM immediate feedback it can reason over, matching the js implem.