MCP PHP SDK

ClientGateway
in package

FinalYes

Helper class for tools to communicate with the client.

This class provides a clean API for element handlers to send requests and notifications to the client. It uses PHP Fibers internally to make the communication appear synchronous while the transport handles all blocking operations.

Example usage in a tool:

public function analyze(string $text, ClientGateway $client): string {
    // Send progress notification
    $client->notify(new ProgressNotification("Starting analysis..."));

    // Request LLM sampling from client
    $response = $client->request(new SamplingRequest($text));

    return $response->content->text;
}
Tags
phpstan-type

SampleOptions array{ preferences?: ModelPreferences, systemPrompt?: string, temperature?: float, includeContext?: SamplingContext, stopSequences?: string[], metadata?: array<string, mixed>, }

author

Kyrian Obikwelu koshnawaza@gmail.com

Table of Contents

Properties

$session  : SessionInterface

Methods

__construct()  : mixed
log()  : void
Convenience method to send a logging notification to the client.
notify()  : void
Send a notification to the client (fire and forget).
progress()  : void
Convenience method to send a progress notification to the client.
sample()  : CreateSamplingMessageResult
Convenience method for LLM sampling requests.
request()  : Response<string|int, array<string, mixed>>|Error
Send a request to the client and wait for a response (blocking).

Properties

Methods

log()

Convenience method to send a logging notification to the client.

public log(LoggingLevel $level, mixed $data[, string|null $logger = null ]) : void
Parameters
$level : LoggingLevel
$data : mixed
$logger : string|null = null

notify()

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

public notify(Notification $notification) : void

This suspends the Fiber to let the transport flush the notification via SSE, then immediately resumes execution.

Parameters
$notification : Notification

progress()

Convenience method to send a progress notification to the client.

public progress(float $progress[, float|null $total = null ][, string|null $message = null ]) : void
Parameters
$progress : float
$total : float|null = null
$message : string|null = null

sample()

Convenience method for LLM sampling requests.

public sample(array<string|int, SamplingMessage>|TextContent|AudioContent|ImageContent|string $message[, int $maxTokens = 1000 ][, int $timeout = 120 ][, SampleOptions $options = [] ]) : CreateSamplingMessageResult
Parameters
$message : array<string|int, SamplingMessage>|TextContent|AudioContent|ImageContent|string

The message for the LLM

$maxTokens : int = 1000

Maximum tokens to generate

$timeout : int = 120

The timeout in seconds

$options : SampleOptions = []

Additional sampling options (temperature, etc.)

Tags
throws
ClientException

if the client request results in an error message

Return values
CreateSamplingMessageResult —

The sampling response

request()

Send a request to the client and wait for a response (blocking).

private request(Request $request[, int $timeout = 120 ]) : Response<string|int, array<string, mixed>>|Error

This suspends the Fiber and waits for the client to respond. The transport handles polling the session for the response and resuming the Fiber when ready.

Parameters
$request : Request

The request to send

$timeout : int = 120

Maximum time to wait for response (seconds)

Tags
throws
RuntimeException

If Fiber support is not available

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

The client's response message


        
On this page

Search results