Enhance error handling in MCP tool invocation#263
Merged
rapids-bot[bot] merged 10 commits intoNVIDIA:developfrom May 14, 2025
Merged
Enhance error handling in MCP tool invocation#263rapids-bot[bot] merged 10 commits intoNVIDIA:developfrom
rapids-bot[bot] merged 10 commits intoNVIDIA:developfrom
Conversation
Added a try-except block around the tool invocation to catch and log exceptions, ensuring that any errors during tool execution are properly handled and reported. This improves the robustness of the agent's interaction with external tools. Signed-off-by: Matthew Penn <mpenn@nvidia.com>
Changed the logging from an exception to a warning level for failed tool invocations, improving clarity in error reporting while maintaining the ability to track issues effectively. Signed-off-by: Matthew Penn <mpenn@nvidia.com>
|
NOTE: Works on both AIQToolkit-UI and console Verified, the new changes work for simple queries, updated the bug #262 for nested query which calls multiple Output - |
…lity errors. Updated the MCPTool to include a configurable option for error handling. The tool can now either return exception messages or raise exceptions based on the `return_exception` flag in the configuration. This enhances flexibility in managing tool invocation errors. Signed-off-by: Matthew Penn <mpenn@nvidia.com>
AnuradhaKaruppiah
approved these changes
May 14, 2025
Contributor
Author
|
/merge |
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR enhances error handling for MCP tool invocations by surfacing exception messages back to the agent and adds retry logic in the React agent when tools fail.
- Added a
return_exceptionsetting to MCPToolConfig to control whether exceptions are returned as strings - Wrapped MCP tool calls in a try/except block to return or re-raise exceptions based on configuration
- Modified the React agent’s
tool_nodeto retry tool invocation after an initial failure
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| src/aiq/tool/mcp/mcp_tool.py | Added return_exception config and exception-catching logic |
| src/aiq/agent/react_agent/agent.py | Inserted retry invocation for tools after a failure |
Comments suppressed due to low confidence (1)
src/aiq/tool/mcp/mcp_tool.py:43
- Introduce unit tests to cover the new
return_exceptionbehavior, ensuring both the exception-returning and exception-raising branches are exercised.
return_exception: bool = Field(default=True,
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Signed-off-by: Matthew Penn <matt.w.penn@gmail.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Signed-off-by: Matthew Penn <matt.w.penn@gmail.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Signed-off-by: Matthew Penn <matt.w.penn@gmail.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Signed-off-by: Matthew Penn <matt.w.penn@gmail.com>
ericevans-nv
pushed a commit
to ericevans-nv/agent-iq
that referenced
this pull request
Jun 3, 2025
Closes NVIDIA#262 This PR handles cases when an MCP tool is invoked and fails because of invalid inputs. Previously, invalid inputs trigger exceptions and the entire agent exits, returning an error message. The resolution in this PR introduces a configuration setting in the MCP tool that propagates the exception message back to the agent, giving it an opportunity to recover. This exception can be observed with the `simple_mcp` example. Without this improvement, the `react_agent` fails to produce valid outputs after a single parsing failure. Note, this example will need to be run several times due to non-deterministic behavior. The `react_agent` intermittently produces invalid inputs for the MCP tool. ## By Submitting this PR I confirm: - I am familiar with the [Contributing Guidelines](https://github.com/NVIDIA/AIQToolkit/blob/develop/docs/source/resources/contributing.md). - We require that all contributors "sign-off" on their commits. This certifies that the contribution is your original work, or you have rights to submit it under the same license, or a compatible license. - Any contribution which contains commits that are not Signed-Off will not be accepted. - When the PR is ready for review, new or existing tests cover these changes. - When the PR is ready for review, the documentation is up to date with these changes. Authors: - Matthew Penn (https://github.com/mpenn) Approvers: - Anuradha Karuppiah (https://github.com/AnuradhaKaruppiah) URL: NVIDIA#263 Signed-off-by: Eric Evans <194135482+ericevans-nv@users.noreply.github.com>
ericevans-nv
pushed a commit
to ericevans-nv/agent-iq
that referenced
this pull request
Jun 3, 2025
Closes NVIDIA#262 This PR handles cases when an MCP tool is invoked and fails because of invalid inputs. Previously, invalid inputs trigger exceptions and the entire agent exits, returning an error message. The resolution in this PR introduces a configuration setting in the MCP tool that propagates the exception message back to the agent, giving it an opportunity to recover. This exception can be observed with the `simple_mcp` example. Without this improvement, the `react_agent` fails to produce valid outputs after a single parsing failure. Note, this example will need to be run several times due to non-deterministic behavior. The `react_agent` intermittently produces invalid inputs for the MCP tool. ## By Submitting this PR I confirm: - I am familiar with the [Contributing Guidelines](https://github.com/NVIDIA/AIQToolkit/blob/develop/docs/source/resources/contributing.md). - We require that all contributors "sign-off" on their commits. This certifies that the contribution is your original work, or you have rights to submit it under the same license, or a compatible license. - Any contribution which contains commits that are not Signed-Off will not be accepted. - When the PR is ready for review, new or existing tests cover these changes. - When the PR is ready for review, the documentation is up to date with these changes. Authors: - Matthew Penn (https://github.com/mpenn) Approvers: - Anuradha Karuppiah (https://github.com/AnuradhaKaruppiah) URL: NVIDIA#263 Signed-off-by: Eric Evans <194135482+ericevans-nv@users.noreply.github.com>
AnuradhaKaruppiah
pushed a commit
to AnuradhaKaruppiah/oss-agentiq
that referenced
this pull request
Aug 4, 2025
Closes NVIDIA#262 This PR handles cases when an MCP tool is invoked and fails because of invalid inputs. Previously, invalid inputs trigger exceptions and the entire agent exits, returning an error message. The resolution in this PR introduces a configuration setting in the MCP tool that propagates the exception message back to the agent, giving it an opportunity to recover. This exception can be observed with the `simple_mcp` example. Without this improvement, the `react_agent` fails to produce valid outputs after a single parsing failure. Note, this example will need to be run several times due to non-deterministic behavior. The `react_agent` intermittently produces invalid inputs for the MCP tool. ## By Submitting this PR I confirm: - I am familiar with the [Contributing Guidelines](https://github.com/NVIDIA/AIQToolkit/blob/develop/docs/source/resources/contributing.md). - We require that all contributors "sign-off" on their commits. This certifies that the contribution is your original work, or you have rights to submit it under the same license, or a compatible license. - Any contribution which contains commits that are not Signed-Off will not be accepted. - When the PR is ready for review, new or existing tests cover these changes. - When the PR is ready for review, the documentation is up to date with these changes. Authors: - Matthew Penn (https://github.com/mpenn) Approvers: - Anuradha Karuppiah (https://github.com/AnuradhaKaruppiah) URL: NVIDIA#263
scheckerNV
pushed a commit
to scheckerNV/aiq-factory-reset
that referenced
this pull request
Aug 22, 2025
Closes NVIDIA#262 This PR handles cases when an MCP tool is invoked and fails because of invalid inputs. Previously, invalid inputs trigger exceptions and the entire agent exits, returning an error message. The resolution in this PR introduces a configuration setting in the MCP tool that propagates the exception message back to the agent, giving it an opportunity to recover. This exception can be observed with the `simple_mcp` example. Without this improvement, the `react_agent` fails to produce valid outputs after a single parsing failure. Note, this example will need to be run several times due to non-deterministic behavior. The `react_agent` intermittently produces invalid inputs for the MCP tool. ## By Submitting this PR I confirm: - I am familiar with the [Contributing Guidelines](https://github.com/NVIDIA/AIQToolkit/blob/develop/docs/source/resources/contributing.md). - We require that all contributors "sign-off" on their commits. This certifies that the contribution is your original work, or you have rights to submit it under the same license, or a compatible license. - Any contribution which contains commits that are not Signed-Off will not be accepted. - When the PR is ready for review, new or existing tests cover these changes. - When the PR is ready for review, the documentation is up to date with these changes. Authors: - Matthew Penn (https://github.com/mpenn) Approvers: - Anuradha Karuppiah (https://github.com/AnuradhaKaruppiah) URL: NVIDIA#263
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Closes #262
This PR handles cases when an MCP tool is invoked and fails because of invalid inputs. Previously, invalid inputs trigger exceptions and the entire agent exits, returning an error message. The resolution in this PR introduces a configuration setting in the MCP tool that propagates the exception message back to the agent, giving it an opportunity to recover.
This exception can be observed with the
simple_mcpexample. Without this improvement, thereact_agentfails to produce valid outputs after a single parsing failure. Note, this example will need to be run several times due to non-deterministic behavior. Thereact_agentintermittently produces invalid inputs for the MCP tool.By Submitting this PR I confirm: