useCallTool function allows you to call additional MCP tools from your widget. It wraps the window.openai.callTool function while providing a more convenient state management and typed API.
Make sure your tool _meta["openai/widgetAccessible"] property is set to true to make it accessible from widgets - more info on component-initiated tool calls.
Basic usage
Parameters
name
Type Parameters
ToolArgs
null for tools that don’t require arguments.
- If your tool has no arguments, you can omit this type parameter or set it to
null. - If your tool requires arguments, define the shape as a
Record<string, unknown>.
ToolResponse
structuredContent and meta fields of your tool’s response.
Returns
callTool
toolArgs: ToolArgs- Optional if
ToolArgsisnull - The arguments to pass to the tool
- Optional if
sideEffects: SideEffects- Optional
- The side-effect callbacks to execute when the tool call is successful, encounters an error, or is settled:
onSuccess: (data: ToolResponse, toolArgs: ToolArgs) => void- Optional
- This function will fire when the tool call is successful and will be passed the tool’s response
onError: (error: unknown, toolArgs: ToolArgs) => void- Optional
- This function will fire if the tool call encounters an error
onSettled: (data: ToolResponse | undefined, error: unknown | undefined, toolArgs: ToolArgs) => void- Optional
- This function will fire when the tool call is either successful or encounters an error
callToolAsync
callTool but returns a promise which can be awaited.
status
idle- Initial status prior to the tool call executingpending- The tool call is currently executingsuccess- The last tool call was successfulerror- The last tool call resulted in an error
isIdle, isPending, isSuccess, isError
status for convenience.
data
- The successfully resolved data from the last tool call. Only available when the status is
success. - Contains the default
CallToolResponseshape merged with yourToolResponsetype:
error
- Only available when the status is
error. - The error object if the last tool call encountered an error
Examples
With Side Effects
Handle success, error, and settled states with callbacks:Tools without arguments
You can pass side effect options as the first argument to thecallTool function if it doesn’t require any arguments.
Async/Await Pattern
UsecallToolAsync for async/await syntax:
