MCP PHP SDK

SchemaGenerator implements SchemaGeneratorInterface

FinalYes

Generates JSON Schema for method parameters with intelligent Schema attribute handling.

Priority system:

  1. Schema attributes (method-level and parameter-level)
  2. Reflection type information
  3. DocBlock type information
Tags
phpstan-import-type

SchemaAttributeData from Schema

author

Kyrian Obikwelu koshnawaza@gmail.com

Table of Contents

Interfaces

SchemaGeneratorInterface
Provides JSON Schema generation for reflected elements.

Properties

$docBlockParser  : DocBlockParser

Methods

__construct()  : mixed
generate()  : array<string, mixed>
Generates a JSON Schema object (as a PHP array) for parameters.
generateOutputSchema()  : array<string, mixed>|null
Generates a JSON Schema object (as a PHP array) for a method's or function's return type.
applyArrayConstraints()  : array<string|int, mixed>
Applies array-specific constraints to parameter schema.
applyEnumConstraints()  : array<string|int, mixed>
Applies enum constraints to parameter schema.
buildInferredParameterSchema()  : InferredParameterSchema
Builds parameter schema from inferred type and docblock information only.
buildParameterSchema()  : array<string|int, mixed>
Builds the final schema for a single parameter by merging all three levels.
buildSchemaFromParameters()  : array<string, mixed>
Builds the final schema from parameter information and method-level schema.
buildVariadicParameterSchema()  : VariadicParameterSchema
Builds schema for variadic parameters.
extractMethodLevelSchema()  : SchemaAttributeData
Extracts method-level or function-level Schema attribute.
extractParameterLevelSchema()  : SchemaAttributeData
Extracts parameter-level Schema attribute.
getParameterTypeString()  : string
Determines the type string for a parameter, prioritizing DocBlock.
getTypeStringFromReflection()  : string
Converts a ReflectionType object into a type string representation.
inferArrayItemsType()  : string|array<string, mixed>
Infers the 'items' schema type for an array based on DocBlock type hints.
inferParameterTypes()  : array<string|int, mixed>
Infers JSON Schema types for a parameter.
mapPhpTypeToJsonSchemaType()  : array<string|int, string>
Maps a PHP type string (potentially a union) to an array of JSON Schema type names.
mapSimpleTypeToJsonSchema()  : string
Helper method to map basic PHP types to JSON Schema types.
parseObjectLikeArray()  : array{type: "object", properties?: array, required?: string[]}
Parses object-like array syntax into a JSON Schema object.
parseParametersInfo()  : array<string|int, ParameterInfo>
Parses detailed information about a method's parameters.
parsePropertyDefinition()  : void
Parses a single property definition from an object-like array syntax.

Properties

Methods

generate()

Generates a JSON Schema object (as a PHP array) for parameters.

public generate(Reflector $reflection) : array<string, mixed>
Parameters
$reflection : Reflector
Return values
array<string, mixed>

generateOutputSchema()

Generates a JSON Schema object (as a PHP array) for a method's or function's return type.

public generateOutputSchema(Reflector $reflection) : array<string, mixed>|null

Only returns an outputSchema if explicitly provided in the McpTool attribute. Per MCP spec, outputSchema should only be present when explicitly provided.

Parameters
$reflection : Reflector
Return values
array<string, mixed>|null

applyArrayConstraints()

Applies array-specific constraints to parameter schema.

private applyArrayConstraints(array<string|int, mixed> $paramSchema, array<string|int, mixed> $paramInfo) : array<string|int, mixed>
Parameters
$paramSchema : array<string|int, mixed>
$paramInfo : array<string|int, mixed>
Return values
array<string|int, mixed>

applyEnumConstraints()

Applies enum constraints to parameter schema.

private applyEnumConstraints(array<string|int, mixed> $paramSchema, array<string|int, mixed> $paramInfo) : array<string|int, mixed>
Parameters
$paramSchema : array<string|int, mixed>
$paramInfo : array<string|int, mixed>
Return values
array<string|int, mixed>

buildInferredParameterSchema()

Builds parameter schema from inferred type and docblock information only.

private buildInferredParameterSchema(ParameterInfo $paramInfo) : InferredParameterSchema

Returns empty array for variadic parameters (handled separately).

Parameters
$paramInfo : ParameterInfo
Return values
InferredParameterSchema

buildParameterSchema()

Builds the final schema for a single parameter by merging all three levels.

private buildParameterSchema(ParameterInfo $paramInfo, array<string, mixed>|null $methodLevelParamSchema) : array<string|int, mixed>
Parameters
$paramInfo : ParameterInfo
$methodLevelParamSchema : array<string, mixed>|null
Return values
array<string|int, mixed>

buildSchemaFromParameters()

Builds the final schema from parameter information and method-level schema.

private buildSchemaFromParameters(array<string|int, ParameterInfo$parametersInfo, SchemaAttributeData $methodSchema) : array<string, mixed>
Parameters
$parametersInfo : array<string|int, ParameterInfo>
$methodSchema : SchemaAttributeData
Return values
array<string, mixed>

buildVariadicParameterSchema()

Builds schema for variadic parameters.

private buildVariadicParameterSchema(ParameterInfo $paramInfo) : VariadicParameterSchema
Parameters
$paramInfo : ParameterInfo
Return values
VariadicParameterSchema

extractMethodLevelSchema()

Extracts method-level or function-level Schema attribute.

private extractMethodLevelSchema(ReflectionFunctionAbstract $reflection) : SchemaAttributeData
Parameters
$reflection : ReflectionFunctionAbstract
Return values
SchemaAttributeData

extractParameterLevelSchema()

Extracts parameter-level Schema attribute.

private extractParameterLevelSchema(ReflectionParameter $parameter) : SchemaAttributeData
Parameters
$parameter : ReflectionParameter
Return values
SchemaAttributeData

getParameterTypeString()

Determines the type string for a parameter, prioritizing DocBlock.

private getParameterTypeString(ReflectionParameter $rp, Param|null $paramTag) : string
Parameters
$rp : ReflectionParameter
$paramTag : Param|null
Return values
string

getTypeStringFromReflection()

Converts a ReflectionType object into a type string representation.

private getTypeStringFromReflection(ReflectionType|null $type, bool $nativeAllowsNull) : string
Parameters
$type : ReflectionType|null
$nativeAllowsNull : bool
Return values
string

inferArrayItemsType()

Infers the 'items' schema type for an array based on DocBlock type hints.

private inferArrayItemsType(string $phpTypeString) : string|array<string, mixed>
Parameters
$phpTypeString : string
Return values
string|array<string, mixed>

inferParameterTypes()

Infers JSON Schema types for a parameter.

private inferParameterTypes(ParameterInfo $paramInfo) : array<string|int, mixed>
Parameters
$paramInfo : ParameterInfo
Return values
array<string|int, mixed>

mapPhpTypeToJsonSchemaType()

Maps a PHP type string (potentially a union) to an array of JSON Schema type names.

private mapPhpTypeToJsonSchemaType(string $phpTypeString) : array<string|int, string>
Parameters
$phpTypeString : string
Return values
array<string|int, string>

mapSimpleTypeToJsonSchema()

Helper method to map basic PHP types to JSON Schema types.

private mapSimpleTypeToJsonSchema(string $type) : string
Parameters
$type : string
Return values
string

parseObjectLikeArray()

Parses object-like array syntax into a JSON Schema object.

private parseObjectLikeArray(string $propertiesStr) : array{type: "object", properties?: array, required?: string[]}
Parameters
$propertiesStr : string
Return values
array{type: "object", properties?: array, required?: string[]}

parseParametersInfo()

Parses detailed information about a method's parameters.

private parseParametersInfo(ReflectionMethod|ReflectionFunction $reflection) : array<string|int, ParameterInfo>
Parameters
$reflection : ReflectionMethod|ReflectionFunction
Return values
array<string|int, ParameterInfo>

parsePropertyDefinition()

Parses a single property definition from an object-like array syntax.

private parsePropertyDefinition(string $propDefinition, array<string|int, mixed> &$properties, array<string|int, mixed> &$required) : void
Parameters
$propDefinition : string
$properties : array<string|int, mixed>
$required : array<string|int, mixed>

        
On this page

Search results