fix: handle errors when toolExecution fails for functionToolCallback #4012
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.
FunctionToolCallback does not wrap exceptions in ToolExecutionException
fixes issue 2857
Description
The
FunctionToolCallbackdoes not properly handle exceptions thrown by user-defined tool functions. Unlike other tool callback implementations (such asSyncMcpToolCallback), it does not wrap exceptions inToolExecutionException, which prevents proper error handling by the framework's exception processing mechanisms.Current Behavior
When a tool function throws an exception, it propagates uncaught through the
FunctionToolCallback.call()method:Expected Behavior
Exceptions should be caught and wrapped in
ToolExecutionExceptionto maintain consistency with other tool callback implementations and enable proper error processing.Proposed Solution
Wrap the tool function execution in a try-catch block, similar to the implementation in SyncMcpToolCallback:
Alternatives / Contribution for other options
If this is not an appropriate way to solve this issue please let me know! :) I would be happy to also contribute more general solutions - like making the
ToolExecutionExceptionProcessormore generic (so that it can handle all kind of exceptions - and not just ToolExecutionExceptions)Impact
This change would:
ToolExecutionExceptionProcessor)Example Use Case
Currently, the
RuntimeExceptionthrown inperformCall()is not wrapped in aToolExecutionException, making it difficult for error processors to handle tool-specific failures appropriately.