Class AsyncToolCallbackAdapter
- All Implemented Interfaces:
AsyncToolCallback,StateAwareToolCallback,org.springframework.ai.tool.ToolCallback
ToolCallback implementations to provide
asynchronous execution capabilities.
This adapter enables existing synchronous tools (such as @Tool annotated methods,
FunctionToolCallback, MethodToolCallback, and MCP tools) to benefit from
the async execution infrastructure without requiring user code changes.
The adapter follows the LangChain pattern of running synchronous tools in an executor to achieve parallel execution. This is especially useful when multiple independent tools need to execute concurrently.
Example usage:
// Wrap a sync tool for async execution
ToolCallback syncTool = getSomeSyncTool();
AsyncToolCallback asyncTool = AsyncToolCallbackAdapter.wrapIfNeeded(syncTool, executor);
// Use the wrapped tool in async context
CompletableFuture<String> future = asyncTool.callAsync(args, context);
Note: Wrapped sync tools cannot be truly cancelled since they are blocking operations. The adapter provides async semantics but cancellation will only take effect after the synchronous operation completes.
- Since:
- 1.0.0
- Author:
- disaster
- See Also:
-
AsyncToolCallbackToolCallback
-
Field Summary
Fields inherited from interface org.springframework.ai.tool.ToolCallback
logger -
Constructor Summary
ConstructorsConstructorDescriptionAsyncToolCallbackAdapter(org.springframework.ai.tool.ToolCallback delegate, Executor executor) Creates a new adapter with the default timeout of 5 minutes.AsyncToolCallbackAdapter(org.springframework.ai.tool.ToolCallback delegate, Executor executor, Duration timeout) Creates a new adapter with a custom timeout. -
Method Summary
Modifier and TypeMethodDescriptionSynchronous fallback implementation.Asynchronously executes the wrapped synchronous tool.org.springframework.ai.tool.ToolCallbackReturns the wrapped delegate callback.Returns the timeout duration for this tool execution.org.springframework.ai.tool.definition.ToolDefinitionorg.springframework.ai.tool.metadata.ToolMetadatastatic AsyncToolCallbackwrapIfNeeded(org.springframework.ai.tool.ToolCallback callback, Executor executor) Wraps a tool callback in an async adapter if it's not already async.static AsyncToolCallbackwrapIfNeeded(org.springframework.ai.tool.ToolCallback callback, Executor executor, Duration timeout) Wraps a tool callback in an async adapter if it's not already async.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface com.alibaba.cloud.ai.graph.agent.tool.AsyncToolCallback
isAsync
-
Constructor Details
-
AsyncToolCallbackAdapter
public AsyncToolCallbackAdapter(org.springframework.ai.tool.ToolCallback delegate, Executor executor) Creates a new adapter with the default timeout of 5 minutes.- Parameters:
delegate- the synchronous tool callback to wrapexecutor- the executor to use for async execution
-
AsyncToolCallbackAdapter
public AsyncToolCallbackAdapter(org.springframework.ai.tool.ToolCallback delegate, Executor executor, Duration timeout) Creates a new adapter with a custom timeout.- Parameters:
delegate- the synchronous tool callback to wrapexecutor- the executor to use for async executiontimeout- the timeout duration for tool execution
-
-
Method Details
-
callAsync
public CompletableFuture<String> callAsync(String arguments, org.springframework.ai.chat.model.ToolContext context) Asynchronously executes the wrapped synchronous tool.The synchronous tool's
callmethod is executed in the configured executor, allowing it to run concurrently with other tools.- Specified by:
callAsyncin interfaceAsyncToolCallback- Parameters:
arguments- the tool arguments as JSON stringcontext- the tool execution context- Returns:
- a CompletableFuture that will complete with the result string
-
getTimeout
Description copied from interface:AsyncToolCallbackReturns the timeout duration for this tool execution.- Specified by:
getTimeoutin interfaceAsyncToolCallback- Returns:
- the timeout duration, defaults to 5 minutes
-
getToolDefinition
public org.springframework.ai.tool.definition.ToolDefinition getToolDefinition()- Specified by:
getToolDefinitionin interfaceorg.springframework.ai.tool.ToolCallback
-
getToolMetadata
public org.springframework.ai.tool.metadata.ToolMetadata getToolMetadata()- Specified by:
getToolMetadatain interfaceorg.springframework.ai.tool.ToolCallback
-
call
- Specified by:
callin interfaceorg.springframework.ai.tool.ToolCallback
-
call
Description copied from interface:AsyncToolCallbackSynchronous fallback implementation. Blocks until the async operation completes.This method unwraps
CompletionExceptionto expose the original exception, making error handling more intuitive for callers.- Specified by:
callin interfaceAsyncToolCallback- Specified by:
callin interfaceorg.springframework.ai.tool.ToolCallback- Parameters:
toolInput- the tool arguments as JSON stringtoolContext- the tool execution context- Returns:
- the result string
-
getDelegate
public org.springframework.ai.tool.ToolCallback getDelegate()Returns the wrapped delegate callback.- Returns:
- the original synchronous tool callback
-
wrapIfNeeded
public static AsyncToolCallback wrapIfNeeded(org.springframework.ai.tool.ToolCallback callback, Executor executor) Wraps a tool callback in an async adapter if it's not already async.If the callback already implements
AsyncToolCallback, it is returned directly without wrapping. Otherwise, a newAsyncToolCallbackAdapteris created to wrap the synchronous callback.- Parameters:
callback- the tool callback to potentially wrapexecutor- the executor to use for async execution- Returns:
- an AsyncToolCallback (either the original or a wrapped adapter)
-
wrapIfNeeded
public static AsyncToolCallback wrapIfNeeded(org.springframework.ai.tool.ToolCallback callback, Executor executor, Duration timeout) Wraps a tool callback in an async adapter if it's not already async.If the callback already implements
AsyncToolCallback, it is returned directly without wrapping. Otherwise, a newAsyncToolCallbackAdapteris created to wrap the synchronous callback.- Parameters:
callback- the tool callback to potentially wrapexecutor- the executor to use for async executiontimeout- the timeout duration for wrapped tool execution- Returns:
- an AsyncToolCallback (either the original or a wrapped adapter)
-