ToolResult() and dictionary response formats support text content with audience targeting, structured data for downstream processing, widget integration to render interactive UI components, and metadata support for custom extensions and context information.
-
ToolResultclass
Returns tool responses by using a Python class with type-safe parameters and MCP compliance. -
Dictionary format
Returns tool responses as a dictionary structure with explicit control over all response fields.
ToolResult class
TheToolResult class provides structured tool responses following the MCP specification. It allows tools to return content blocks, structured data, and widgets in a standardized format.
Import
Import theToolResult class to create structured responses in your Python tools:
Constructor parameters
Content blocks or strings to display to the user and send to the large language model (LLM). Strings are automatically converted to For details on content structure and audience targeting, see Content blocks and annotations.
TextContent blocks.The following example shows two ways to specify content by using simple strings or explicit TextContent blocks:Structured data for programmatic use. This data is available for downstream processing but not directly displayed to users.The following example demonstrates how to include structured data in a ToolResult:
Form widget to render in the UI. Supports form widgets created by using the For widget implementations by using
FormWidget class and its input components.The following example demonstrates how to include a form widget in a ToolResult response:FormWidget(), see Widget integration.Additional metadata for custom extensions or context. This field is serialized as The following example shows how to add custom metadata using the meta parameter:
_meta in the response.The
ToolResult class does not support explicitly setting the _meta parameter and widget contracts as described for dictionary format. To implement widgets with ToolResult, use widget with the FormWidget class.Example
The following 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:Dictionary format
The dictionary format provides direct control over the response structure, and requires the following top-level fields:Dictionary fields
Content blocks to display to the user and send to the large language model (LLM).The following example demonstrates a simple tool that returns content blocks with user audience targeting:For details on content structure and audience targeting, see Content blocks and annotations.
Structured data for programmatic use. This data is available for downstream processing but not directly displayed to users.The following example demonstrates how to return structured data alongside user-facing content:
Additional metadata for custom extensions or context information. Use this field to pass custom data that doesn’t fit into standard response fields.The following example demonstrates how to add custom metadata to a tool response:For widget implementations by using
Custom metadata example
_meta, see Widget integration.Content blocks and annotations
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
Text content blocks represent textual information with optional audience targeting. Use text content to return messages, status updates, or any string-based data from your tool, and to control who sees the content by using theaudience annotation.
Annotations and audience
Theaudience annotation is set inside content blocks to control content visibility and processing behavior.
Specifies who must see and process the content. The possible values varies according to the response format you implement for the tool result.The audience affects agent reasoning and message display behavior. For more information, see Audience behavior.
- ToolResult class
- Dictionary format
Possible values:
Role.USERRole.ASSISTANT[Role.USER, Role.ASSISTANT]
Role enum provides type-safe constants for specifying content audience when using the ToolResult class:The following example shows how to use the Role enum for type-safe audience specification:Audience behavior
Theaudience 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. The following audiences are the audience types that determine content handling:
-
user
Content intended for end users only, bypassing agent reasoning. -
assistant
Content for the agent and large language model to process and reason about. -
userandassistant
Content for both user and assistant, where the agent processes it before presenting to users.
Content with annotations only
When tool responses contain content with annotations but no widget, the following behavior applies based on the audience type:- User
- Assistant
- Both user and assistant
| Property | Behavior |
|---|---|
| Agent reasoning | 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. |
- 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.
- No transformation or summarization occurs.
Content with annotations and widget metadata
When tool responses contain both content with annotations and a widget, widget-specific behavior is activated.- User
- Assistant
- Both user and assistant
| Property | Behavior |
|---|---|
| Agent reasoning | The text is not used for reasoning. |
| Message to agent context | The message is passed to context to prevent hallucinations. |
| User output | The exact text and widget are rendered. |
- No agent reasoning occurs.
- The user message is preserved exactly as provided.
- The widget data is rendered alongside the content.
Widget integration
For detailed information about integrating interactive widgets and forms into your tool responses, including form widgets, custom widgets, event handling, and theFormWidget, ToolEvent, and MessageEvent classes, see Widget integration.
