MCP PHP SDK

Tool implements JsonSerializable

Definition for a tool the client can call.

Tags
phpstan-import-type

ToolAnnotationsData from ToolAnnotations

IconData from Icon

ToolExecutionData from ToolExecution

author

Kyrian Obikwelu koshnawaza@gmail.com

Table of Contents

Interfaces

JsonSerializable

Constants

SUB_SCHEMA_KEYWORDS  : mixed = ['additionalItems', 'additionalProperties', 'co...
JSON Schema keywords whose value is a single sub-schema.
SUB_SCHEMA_LIST_KEYWORDS  : mixed = ['allOf', 'anyOf', 'oneOf', 'prefixItems']
JSON Schema keywords whose value is a list of sub-schemas.
SUB_SCHEMA_MAP_KEYWORDS  : mixed = ['$defs', 'definitions', 'dependentSchemas', 'p...
JSON Schema keywords whose value maps names to sub-schemas.

Properties

$annotations  : ToolAnnotations|null
$description  : string|null
$execution  : ToolExecution|null
$icons  : array<string|int, mixed>|null
$inputSchema  : ToolInputSchema
$meta  : array<string|int, mixed>|null
$name  : string
$outputSchema  : ToolOutputSchema|null
$title  : string|null

Methods

__construct()  : mixed
fromArray()  : self
jsonSerialize()  : ToolExecution}
normalizeInputSchema()  : ToolInputSchema
Normalize an input schema and refuse members that are not what they claim.
normalizeSchema()  : array<string, mixed>
Normalize a JSON Schema so that empty sub-schemas JSON-encode as `{}` rather than `[]`.
normalizeSubSchema()  : array<string, mixed>|stdClass

Constants

SUB_SCHEMA_KEYWORDS

JSON Schema keywords whose value is a single sub-schema.

private mixed SUB_SCHEMA_KEYWORDS = ['additionalItems', 'additionalProperties', 'contains', 'else', 'if', 'not', 'propertyNames', 'then', 'unevaluatedItems', 'unevaluatedProperties']

SUB_SCHEMA_LIST_KEYWORDS

JSON Schema keywords whose value is a list of sub-schemas.

private mixed SUB_SCHEMA_LIST_KEYWORDS = ['allOf', 'anyOf', 'oneOf', 'prefixItems']

SUB_SCHEMA_MAP_KEYWORDS

JSON Schema keywords whose value maps names to sub-schemas.

private mixed SUB_SCHEMA_MAP_KEYWORDS = ['$defs', 'definitions', 'dependentSchemas', 'patternProperties', 'properties']

Properties

$description read-only

public string|null $description

$icons read-only

public array<string|int, mixed>|null $icons = null

$inputSchema read-only

public ToolInputSchema $inputSchema

$meta read-only

public array<string|int, mixed>|null $meta = null

$name read-only

public string $name

$outputSchema read-only

public ToolOutputSchema|null $outputSchema

$title read-only

public string|null $title

Methods

__construct()

public __construct(string $name, string|null $title, array<string, mixed> $inputSchema, string|null $description, ToolAnnotations|null $annotations[, array<string|int, Icon>|null $icons = null ][, array<string, mixed>|null $meta = null ][, array<string, mixed>|null $outputSchema = null ][, ToolExecution|null $execution = null ]) : mixed
Parameters
$name : string

the name of the tool

$title : string|null

Optional human-readable title for display in UI

$inputSchema : array<string, mixed>

a JSON Schema object (as a PHP array) defining the expected 'arguments' for the tool

$description : string|null

A human-readable description of the tool. This can be used by clients to improve the LLM's understanding of available tools. It can be thought of like a "hint" to the model.

$annotations : ToolAnnotations|null

optional additional tool information

$icons : array<string|int, Icon>|null = null

optional icons representing the tool

$meta : array<string, mixed>|null = null

Optional metadata

$outputSchema : array<string, mixed>|null = null

Optional JSON Schema (as a PHP array) describing the tool's structuredContent. Unlike $inputSchema its root is unconstrained — it may describe an array, a primitive, or a composition.

$execution : ToolExecution|null = null

optional execution-related properties, such as whether the tool can be called as a task

fromArray()

public static fromArray(ToolData|array<string|int, mixed> $data) : self
Parameters
$data : ToolData|array<string|int, mixed>
Return values
self

jsonSerialize()

public jsonSerialize() : ToolExecution}
Return values
ToolExecution}

normalizeInputSchema()

Normalize an input schema and refuse members that are not what they claim.

private static normalizeInputSchema(array<string, mixed> $schema) : ToolInputSchema

fromArray() feeds this untrusted wire data, so a member of the wrong type is rejected rather than replaced: quietly turning properties: "x" into } would publish a tool contract nobody declared.

Parameters
$schema : array<string, mixed>
Return values
ToolInputSchema

normalizeSchema()

Normalize a JSON Schema so that empty sub-schemas JSON-encode as `{}` rather than `[]`.

private static normalizeSchema(array<string, mixed> $schema) : array<string, mixed>

Once JSON is decoded into associative arrays, PHP cannot tell the empty object } from the empty array [] — both are []. Re-encoding then produces [], which is invalid wherever a schema is expected (properties, items, additionalProperties, …), and strict clients reject it. Every empty sub-schema is therefore replaced with a \stdClass before serialization.

The walk is recursive and covers the schema keywords of draft-07 through 2020-12, so nested object parameters, $defs, combinators, and outputSchema are all covered — not only the top-level properties map.

Parameters
$schema : array<string, mixed>
Return values
array<string, mixed>

normalizeSubSchema()

private static normalizeSubSchema(array<string, mixed> $schema) : array<string, mixed>|stdClass
Parameters
$schema : array<string, mixed>
Return values
array<string, mixed>|stdClass
On this page

Search results