MCP PHP SDK

Protocol

Client protocol handler for MCP communication.

Handles message routing, request/response correlation, and the initialization handshake. All blocking operations are delegated to the transport.

Tags
phpstan-import-type

FiberSuspend from TransportInterface

author

Kyrian Obikwelu koshnawaza@gmail.com

Table of Contents

Properties

$logger  : LoggerInterface
$messageFactory  : MessageFactory
$notificationHandlers  : array<string|int, NotificationHandlerInterface>
$requestHandlers  : array<string|int, mixed>
$state  : ClientStateInterface
$transport  : TransportInterface|null

Methods

__construct()  : mixed
connect()  : void
Connect this protocol to a transport.
getState()  : ClientStateInterface
initialize()  : Response<string|int, array<string, mixed>>|Error
Perform the MCP initialization handshake.
processMessage()  : void
Process an incoming message from the server.
request()  : Response<string|int, array<string, mixed>>|Error
Send a request to the server and wait for response.
sendNotification()  : void
Send a notification to the server (fire and forget).
handleNotification()  : void
Handle a notification from the server.
handleRequest()  : void
Handle a request from the server (e.g., sampling request).
handleResponse()  : void
Handle a response from the server.
sendRequest()  : void
Send a request to the server.
sendResponse()  : void
Send a response back to the server (for server-initiated requests).

Properties

$requestHandlers read-only

private array<string|int, mixed> $requestHandlers = []

Methods

__construct()

public __construct([array<string|int, RequestHandlerInterface<string|int, mixed>> $requestHandlers = [] ][, array<string|int, NotificationHandlerInterface$notificationHandlers = [] ][, MessageFactory|null $messageFactory = null ][, LoggerInterface|null $logger = null ]) : mixed
Parameters
$requestHandlers : array<string|int, RequestHandlerInterface<string|int, mixed>> = []
$notificationHandlers : array<string|int, NotificationHandlerInterface> = []
$messageFactory : MessageFactory|null = null
$logger : LoggerInterface|null = null

initialize()

Perform the MCP initialization handshake.

public initialize(Configuration $config) : Response<string|int, array<string, mixed>>|Error

Sends InitializeRequest and waits for response, then sends InitializedNotification.

Parameters
$config : Configuration

The client configuration

Return values
Response<string|int, array<string, mixed>>|Error

processMessage()

Process an incoming message from the server.

public processMessage(string $input) : void

Routes to appropriate handler based on message type.

Parameters
$input : string

request()

Send a request to the server and wait for response.

public request(Request $request, int $timeout[, bool $withProgress = false ]) : Response<string|int, array<string, mixed>>|Error

If a response is immediately available (sync HTTP), returns it. Otherwise, suspends the Fiber and waits for the transport to resume it.

Parameters
$request : Request

The request to send

$timeout : int

The timeout in seconds

$withProgress : bool = false

Whether to attach a progress token to the request

Return values
Response<string|int, array<string, mixed>>|Error

sendNotification()

Send a notification to the server (fire and forget).

public sendNotification(Notification $notification) : void
Parameters
$notification : Notification

handleNotification()

Handle a notification from the server.

private handleNotification(Notification $notification) : void
Parameters
$notification : Notification

handleRequest()

Handle a request from the server (e.g., sampling request).

private handleRequest(Request $request) : void
Parameters
$request : Request

handleResponse()

Handle a response from the server.

private handleResponse(Response<string|int, mixed>|Error $response) : void

This stores it in session. The transport will pick it up and resume the Fiber.

Parameters
$response : Response<string|int, mixed>|Error

sendRequest()

Send a request to the server.

private sendRequest(Request $request) : void
Parameters
$request : Request

sendResponse()

Send a response back to the server (for server-initiated requests).

private sendResponse(Response<string|int, mixed>|Error $response) : void
Parameters
$response : Response<string|int, mixed>|Error

        
On this page

Search results