MCP PHP SDK

ToolResultFormatter

FinalYes

Formats tool execution results into MCP Content items.

Tags
author

Kyrian Obikwelu koshnawaza@gmail.com

author

Mateu Aguiló Bosch mateu@mateuaguilo.com

Table of Contents

Methods

format()  : array<string|int, Content>
Formats the result of a tool execution into an array of MCP Content items.

Methods

format()

Formats the result of a tool execution into an array of MCP Content items.

public format(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