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

Constants

CONNECTION_LOST  : mixed = 'connectionLost'
Error data key marking an error the transport filed because the connection is gone.
REDIRECTED  : mixed = 'redirected'
Error data key marking an error the transport filed because the server answered with a redirect.
RESPONSE_TOO_LARGE  : mixed = 'responseTooLarge'
Error data key marking an error the transport filed because a response body is over its limit.

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.

Constants

CONNECTION_LOST

Error data key marking an error the transport filed because the connection is gone.

public mixed CONNECTION_LOST = 'connectionLost'

REDIRECTED

Error data key marking an error the transport filed because the server answered with a redirect.

public mixed REDIRECTED = 'redirected'

RESPONSE_TOO_LARGE

Error data key marking an error the transport filed because a response body is over its limit.

public mixed RESPONSE_TOO_LARGE = 'responseTooLarge'

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.

Parameters
$fiber : McpFiber

The fiber to execute

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

Unused: the protocol delivers progress as it is parsed

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