MCP PHP SDK

TransportInterface

Interface for client transports that communicate with MCP servers.

The transport owns its execution loop and manages all blocking operations. The client delegates completely to the transport for I/O.

Tags
author

Kyrian Obikwelu koshnawaza@gmail.com

Table of Contents

Methods

close()  : void
Close the transport and clean up resources.
connect()  : void
Connect to the MCP server and perform initialization handshake.
onClose()  : void
Register callback for when connection closes.
onError()  : void
Register callback for transport errors.
onInitialize()  : void
Register callback for initialization handshake.
onMessage()  : void
Register callback for incoming messages from server.
runRequest()  : Response<string|int, array<string, mixed>>|Error
Run a request fiber to completion.
send()  : void
Send a message to the server immediately.
setState()  : void
Set the client state for runtime state management.

Methods

connect()

Connect to the MCP server and perform initialization handshake.

public connect() : void

This method blocks until:

  • Initialization completes successfully
  • Connection fails (throws ConnectionException)
Tags
throws
ConnectionException

onClose()

Register callback for when connection closes.

public onClose(callable(string $reason): void $callback) : void
Parameters
$callback : callable(string $reason): void

onError()

Register callback for transport errors.

public onError(callable(Throwable $error): void $callback) : void
Parameters
$callback : callable(Throwable $error): void

onInitialize()

Register callback for initialization handshake.

public onInitialize(callable(): mixed $callback) : void

The callback should return a Fiber that performs the initialization.

Parameters
$callback : callable(): mixed

onMessage()

Register callback for incoming messages from server.

public onMessage(callable(string $message): void $callback) : void
Parameters
$callback : callable(string $message): void

runRequest()

Run a request fiber to completion.

public runRequest(McpFiber $fiber[, callable(float $progress, Array $total, Array $message): void|null $onProgress = null ]) : Response<string|int, array<string, mixed>>|Error

The transport starts the fiber, runs its internal loop, and resumes the fiber when a response arrives or timeout occurs.

During the loop, the transport checks session for progress data and executes the callback if provided.

Parameters
$fiber : McpFiber

The fiber to execute

$onProgress : callable(float $progress, Array $total, Array $message): void|null = null

Optional callback for progress updates

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

The response or error

send()

Send a message to the server immediately.

public send(string $data) : void
Parameters
$data : string

JSON-encoded message


        
On this page

Search results