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
$isManual  : bool
$tool  : Tool

Methods

__construct()  : mixed
extractStructuredContent()  : array<string, mixed>|null
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.

Properties

$handler read-only

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

Methods

__construct()

public __construct(Tool $tool, Handler $handler[, bool $isManual = false ]) : mixed
Parameters
$tool : Tool
$handler : Handler
$isManual : bool = false

extractStructuredContent()

Extracts structured content from a tool result using the output schema.

public extractStructuredContent(mixed $toolExecutionResult) : array<string, mixed>|null
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, mixed>|null

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


        
On this page

Search results