MCP PHP SDK

ToolReference extends ElementReference

Tags
phpstan-import-type

Handler from ElementReference

author

Kyrian Obikwelu koshnawaza@gmail.com

Table of Contents

Properties

$handler  : Closure|array<string|int, mixed>|string
$tool  : Tool

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.
acceptsScalarStructuredContent()  : bool
Whether the negotiated revision and the tool's own declaration together allow a non-object `structuredContent`.

Properties

$handler read-only

public Closure|array<string|int, mixed>|string $handler

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 initialize requires

Tags
throws
JsonException

if JSON encoding fails for non-Content array/object results

Return values
mixed

the structured content, or null if not extractable

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
throws
JsonException

if JSON encoding fails for non-Content array/object results

Return values
array<string|int, Content>

the content items for CallToolResult

acceptsScalarStructuredContent()

Whether the negotiated revision and the tool's own declaration together allow a non-object `structuredContent`.

private acceptsScalarStructuredContent(bool $objectOnly) : bool
Parameters
$objectOnly : bool
Return values
bool
On this page

Search results