MCP PHP SDK

MessageFactory

FinalYes

Factory for creating JSON-RPC message objects from raw input.

Handles all types of JSON-RPC messages:

  • Requests (have method + id)
  • Notifications (have method, no id)
  • Responses (have result + id)
  • Errors (have error + id)
Tags
author

Christopher Hertel mail@christopher-hertel.de

Kyrian Obikwelu koshnawaza@gmail.com

Table of Contents

Constants

DEFAULT_MAX_BATCH_SIZE  : mixed = 100
Upper bound on the number of messages accepted in a single batch, guarding against amplification where one small request expands into many operations.
REGISTERED_MESSAGES  : array<int, Request>|Notification>> = [\Mcp\Schema\Notification\CancelledNotification...
Registry of all known message classes that have methods.

Properties

$maxBatchSize  : int
$registeredMessages  : array<string|int, mixed>

Methods

__construct()  : mixed
create()  : array<string|int, MessageInterface|InvalidInputMessageException>
Creates message objects from JSON input.
make()  : self
Creates a new Factory instance with all the protocol's default messages.
createMessage()  : MessageInterface
Creates a single message object from parsed JSON data.
findMessageClassByMethod()  : Request>|Notification>
Finds the registered message class for a given method name.

Constants

DEFAULT_MAX_BATCH_SIZE

Upper bound on the number of messages accepted in a single batch, guarding against amplification where one small request expands into many operations.

public mixed DEFAULT_MAX_BATCH_SIZE = 100

REGISTERED_MESSAGES

Registry of all known message classes that have methods.

private array<int, Request>|Notification>> REGISTERED_MESSAGES = [\Mcp\Schema\Notification\CancelledNotification::class, \Mcp\Schema\Notification\InitializedNotification::class, \Mcp\Schema\Notification\LoggingMessageNotification::class, \Mcp\Schema\Notification\ProgressNotification::class, \Mcp\Schema\Notification\PromptListChangedNotification::class, \Mcp\Schema\Notification\ResourceListChangedNotification::class, \Mcp\Schema\Notification\ResourceUpdatedNotification::class, \Mcp\Schema\Notification\RootsListChangedNotification::class, \Mcp\Schema\Notification\ToolListChangedNotification::class, \Mcp\Schema\Request\CallToolRequest::class, \Mcp\Schema\Request\CompletionCompleteRequest::class, \Mcp\Schema\Request\CreateSamplingMessageRequest::class, \Mcp\Schema\Request\ElicitRequest::class, \Mcp\Schema\Request\GetPromptRequest::class, \Mcp\Schema\Request\InitializeRequest::class, \Mcp\Schema\Request\ListPromptsRequest::class, \Mcp\Schema\Request\ListResourcesRequest::class, \Mcp\Schema\Request\ListResourceTemplatesRequest::class, \Mcp\Schema\Request\ListRootsRequest::class, \Mcp\Schema\Request\ListToolsRequest::class, \Mcp\Schema\Request\PingRequest::class, \Mcp\Schema\Request\ReadResourceRequest::class, \Mcp\Schema\Request\ResourceSubscribeRequest::class, \Mcp\Schema\Request\ResourceUnsubscribeRequest::class, \Mcp\Schema\Request\SetLogLevelRequest::class]

Properties

$maxBatchSize read-only

private int $maxBatchSize = self::DEFAULT_MAX_BATCH_SIZE

$registeredMessages read-only

private array<string|int, mixed> $registeredMessages

Methods

__construct()

public __construct(array<int, Request>|Notification>$registeredMessages[, int $maxBatchSize = self::DEFAULT_MAX_BATCH_SIZE ]) : mixed
Parameters
$registeredMessages : array<int, Request>|Notification>>
$maxBatchSize : int = self::DEFAULT_MAX_BATCH_SIZE

Maximum number of messages accepted in a single JSON-RPC batch

create()

Creates message objects from JSON input.

public create(string $input) : array<string|int, MessageInterface|InvalidInputMessageException>

Supports both single messages and batch requests. Returns an array containing MessageInterface objects or InvalidInputMessageException instances for invalid messages.

Parameters
$input : string
Tags
throws
JsonException

When the input string is not valid JSON

Return values
array<string|int, MessageInterface|InvalidInputMessageException>

make()

Creates a new Factory instance with all the protocol's default messages.

public static make([int $maxBatchSize = self::DEFAULT_MAX_BATCH_SIZE ][, array<int, Request>|Notification>$additional = [] ]) : self
Parameters
$maxBatchSize : int = self::DEFAULT_MAX_BATCH_SIZE
$additional : array<int, Request>|Notification>> = []

message classes an extension defines

Return values
self

findMessageClassByMethod()

Finds the registered message class for a given method name.

private findMessageClassByMethod(string $method) : Request>|Notification>
Parameters
$method : string
Tags
throws
InvalidInputMessageException
Return values
Request>|Notification>
On this page

Search results