Use this file to discover all available pages before exploring further.
Python tools can return structured responses with rich content, widgets, and metadata by using the ToolResult class. This class follows the Model Context Protocol (MCP) specification and provides granular control over how tool outputs are processed by the agent and displayed to users.
The ToolResult class provides structured tool responses following the MCP specification. It allows tools to return content blocks, structured data, and widgets a in a standardized format.
This example demonstrates a tool that retrieves account information and returns a comprehensive response with multiple content blocks, structured data, a form widget for updating the budget, and custom metadata:
from ibm_watsonx_orchestrate.run import ToolResult, TextContentfrom ibm_watsonx_orchestrate.run.forms import FormWidget, NumberInput@tool()def check_account_status(user_id: str): # Fetch account data account_data = fetch_account(user_id) # Create form for next action form = FormWidget( title="Update Budget", inputs=[ NumberInput( name="amount", title="New Budget Amount", default_value=account_data["budget"] ) ] ) return ToolResult( content=[ TextContent(text=f"Account Status: {account_data['status']}"), TextContent(text=f"Current Budget: ${account_data['budget']}") ], structuredContent=account_data, widget=form, meta={"account_type": "premium"} )
Content blocks follow the MCP specification and support audience targeting through annotations. This controls how content is processed by the agent and displayed to users.
Text content blocks represent textual information with optional audience targeting.
from ibm_watsonx_orchestrate.run import TextContent, Annotations, Role# Simple textTextContent(text="Operation completed")# Text with user audienceTextContent( text="Your account has been updated", annotations=Annotations(audience=[Role.USER]))# Text with assistant audienceTextContent( text="Account data: {...}", annotations=Annotations(audience=[Role.ASSISTANT]))
The audience annotation controls how content is processed by the agent and displayed to users. Behavior varies based on whether a widget is present in the response.
Text is not used for reasoning that would trigger next actions.
Message to agent context
Text is passed as AI message to prevent hallucinations.
User output
The exact text displayed as provided.
For the following code snippet, user messages are passed through without agent reasoning, the exact text content is preserved and displayed to the user, context is maintained to prevent hallucinations in follow-up questions, and no transformation or summarization occurs:
from ibm_watsonx_orchestrate.run import ToolResult, TextContent, Annotations, Rolereturn ToolResult( content=[ TextContent( text="Account status retrieved successfully", annotations=Annotations(audience=[Role.USER]) ) ])
Yes - Text IS used for reasoning, triggering next actions
Message to agent context
Yes - Full reasoning chain passed to context
User output
Agent-generated message after summarization
For the following code snippet, the agent performs reasoning on the message, uses the text for reasoning that results in additional tool calls or response modifications, the message undergoes summarization before being shown to the user, and the user sees the agent’s processed output rather than the raw content:
The agent-generated message. It might differ from original message.
For the following code snippet, the agent performs reasoning despite user audience inclusion, the original content text may or may not be displayed since the final message is generated by the agent, may involve multiple tool calls, and the user sees the agent’s synthesized response:
When tool responses contain both content with annotations and a widget contract in widget=form, widget-specific behavior is activated.
If widget=form is present in the tool response, the agent runtime do not send the response for agent reasoning. The tool response is considered final and passed directly back to the user.
The message is passed to context to prevent hallucinations.
User output
The exact text and widget are rendered.
For the following code snippet, no agent reasoning occurs, the user message is preserved exactly as provided, and the widget data is rendered alongside the content:
For the following code snippet, no agent reasoning is performed, the content text is empty and not displayed to the user, and only the widget is shown to the user:
For the following code snippet, no agent reasoning occurs, the message text is displayed exactly as provided to the user, and the widget is rendered alongside the text:
Widgets provide interactive UI components in tool responses.The following example demonstrates a tool that creates a support ticket form with multiple input types such as text input, text area, and radio buttons, returns user-facing content with appropriate audience targeting, and renders the form widget for user interaction:
from ibm_watsonx_orchestrate.run import ToolResult, TextContent, Annotations, Rolefrom ibm_watsonx_orchestrate.run.forms import FormWidget, TextInput, RadioButton@tool()def create_support_ticket(): form = FormWidget( title="Create Support Ticket", submit_text="Submit Ticket", inputs=[ TextInput( name="title", title="Ticket Title", required=True ), TextArea( name="description", title="Description", required=True ), RadioButton( name="priority", title="Priority", options=["low", "medium", "high"], option_labels=["Low", "Medium", "High"], default_value="medium" ) ] ) return ToolResult( content=[ TextContent( text="Please fill out the support ticket details", annotations=Annotations(audience=[Role.USER]) ) ], widget=form )