ToolReference extends ElementReference
Tags
Table of Contents
Properties
Methods
- __construct() : mixed
- extractStructuredContent() : mixed
- Extracts structured content from a tool result using the output schema.
- formatResult() : array<string|int, Content>
- Formats the result of a tool execution into an array of MCP Content items.
- acceptsNonObjectStructuredContent() : bool
- Whether the negotiated revision and the tool's own declaration together allow a non-object `structuredContent`.
- objectsToArrays() : mixed
- Turns decoded JSON objects into arrays, except empty ones, which would be sent as `[]`.
Properties
$handler read-only
public
Closure|array<string|int, mixed>|string
$handler
$tool read-only
public
Tool
$tool
Methods
__construct()
public
__construct(Tool $tool, Handler $handler) : mixed
Parameters
- $tool : Tool
- $handler : Handler
extractStructuredContent()
Extracts structured content from a tool result using the output schema.
public
extractStructuredContent(mixed $toolExecutionResult[, ProtocolVersion|null $protocolVersion = null ]) : mixed
What may be sent as structuredContent depends on the protocol revision in
use. Up to 2025-11-25 it has to be a JSON object, and outputSchema is
restricted to type: "object" to match. From 2026-07-28 on (SEP-2106)
outputSchema is any JSON Schema 2020-12 and structuredContent is any JSON
value conforming to it — a list included.
Parameters
- $toolExecutionResult : mixed
-
the raw value returned by the tool's PHP method
- $protocolVersion : ProtocolVersion|null = null
-
revision the result is produced for; defaults to the newest handshake revision, whose stricter rule is what every revision reachable through
initializerequires
Tags
Return values
mixed —the structured content, which may be null, or StructuredContent::Absent if there is none to send
formatResult()
Formats the result of a tool execution into an array of MCP Content items.
public
formatResult(mixed $toolExecutionResult) : array<string|int, Content>
- If the result is already a Content object, it's wrapped in an array.
- If the result is an array:
- If all elements are Content objects, the array is returned as is.
- If it's a mixed array (Content and non-Content items), non-Content items are individually formatted (scalars to TextContent, others to JSON TextContent).
- If it's an array with no Content items, the entire array is JSON-encoded into a single TextContent.
- Scalars (string, int, float, bool) are wrapped in TextContent.
- null is represented as TextContent('(null)').
- Other objects are JSON-encoded and wrapped in TextContent.
Parameters
- $toolExecutionResult : mixed
-
the raw value returned by the tool's PHP method
Tags
Return values
array<string|int, Content> —the content items for CallToolResult
acceptsNonObjectStructuredContent()
Whether the negotiated revision and the tool's own declaration together allow a non-object `structuredContent`.
private
acceptsNonObjectStructuredContent(bool $objectOnly) : bool
Parameters
- $objectOnly : bool
Return values
boolobjectsToArrays()
Turns decoded JSON objects into arrays, except empty ones, which would be sent as `[]`.
private
objectsToArrays(mixed $value) : mixed
Parameters
- $value : mixed