MCP PHP SDK

ClientGateway

Tags
final

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, RequestContext $context): string {
    $client = $context->getClientGateway();
    // Send progress notification
    $client->notify(new ProgressNotification("Starting analysis..."));

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

    return $response->content->text;
}
author

Kyrian Obikwelu koshnawaza@gmail.com

Table of Contents

Properties

$session  : SessionInterface

Methods

__construct()  : mixed
elicit()  : ElicitResult
Convenience method for elicitation requests.
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.
supportsElicitation()  : bool
Check if the connected client supports elicitation.
request()  : Response<string|int, array<string, mixed>>|Error
Send a request to the client and wait for a response (blocking).

Properties

Methods

elicit()

Convenience method for elicitation requests.

public elicit(string $message, ElicitationSchema $requestedSchema[, int $timeout = 120 ]) : ElicitResult

Requests additional information from the user via the client. The user can accept (providing the requested data), decline, or cancel the request.

Parameters
$message : string

A human-readable message describing what information is needed

$requestedSchema : ElicitationSchema

The schema defining the fields to elicit from the user

$timeout : int = 120

The timeout in seconds

Tags
throws
ClientException

if the client request results in an error message

Return values
ElicitResult

The elicitation response containing the user's action and any provided content

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

supportsElicitation()

Check if the connected client supports elicitation.

public supportsElicitation() : bool

Elicitation allows servers to request additional information from users during tool execution. This method checks the client's advertised capabilities to determine if elicitation/create requests are supported.

Return values
bool

True if the client supports elicitation, false otherwise

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