MCP PHP SDK

TransportInterface

Tags
author

Christopher Hertel mail@christopher-hertel.de

author

Kyrian Obikwelu koshnawaza@gmail.com

Table of Contents

Methods

attachFiberToSession()  : void
close()  : void
Closes the transport and cleans up any resources.
initialize()  : void
Initializes the transport.
listen()  : TResult
Starts the transport's execution process.
onMessage()  : void
Register callback for ALL incoming messages.
onSessionEnd()  : void
Register a listener for when a session is terminated.
send()  : void
Send a message to the client immediately (bypassing session queue).
setFiberYieldHandler()  : void
Set a handler for processing values yielded from a suspended Fiber.
setOutgoingMessagesProvider()  : void
Set a provider function to retrieve all queued outgoing messages.
setPendingRequestsProvider()  : void
Set a provider function to retrieve all pending server-initiated requests.
setResponseFinder()  : void
Set a finder function to check for a specific client response.
setSessionId()  : void
Set the session ID for the current transport context.

Methods

attachFiberToSession()

public attachFiberToSession(McpFiber $fiber, Uuid $sessionId) : void
Parameters
$fiber : McpFiber
$sessionId : Uuid

listen()

Starts the transport's execution process.

public listen() : TResult
  • For a blocking transport like STDIO, this method will run a continuous loop.
  • For a single-request transport like HTTP, this will process the request and return a result (e.g., a PSR-7 Response) to be sent to the client.
Return values
TResult

the result of the transport's execution, if any

onMessage()

Register callback for ALL incoming messages.

public onMessage(callable(TransportInterface<string|int, TResult> $transport, string $message, Array $sessionId): void $listener) : void

The transport calls this whenever ANY message arrives, regardless of source.

Parameters
$listener : callable(TransportInterface<string|int, TResult> $transport, string $message, Array $sessionId): void

onSessionEnd()

Register a listener for when a session is terminated.

public onSessionEnd(callable(Uuid $sessionId): void $listener) : void

The transport calls this when a client disconnects or explicitly ends their session.

Parameters
$listener : callable(Uuid $sessionId): void

The callback function to execute when destroying a session

send()

Send a message to the client immediately (bypassing session queue).

public send(string $data, array<string, mixed> $context) : void

Used for session resolution errors when no session is available. The transport decides HOW to send based on context.

Parameters
$data : string
$context : array<string, mixed>

Context about this message:

  • 'session_id': Uuid|null
  • 'type': 'response'|'request'|'notification'
  • 'status_code': int (HTTP status code for errors)

setFiberYieldHandler()

Set a handler for processing values yielded from a suspended Fiber.

public setFiberYieldHandler(callable(Array, Array $sessionId): void $handler) : void

The transport calls this to let the Protocol handle new requests/notifications that are yielded from a Fiber's execution.

Parameters
$handler : callable(Array, Array $sessionId): void

setOutgoingMessagesProvider()

Set a provider function to retrieve all queued outgoing messages.

public setOutgoingMessagesProvider(callable(Uuid $sessionId): array<int, array{message: string, context: array}> $provider) : void

The transport calls this to retrieve all queued messages for a session.

Parameters
$provider : callable(Uuid $sessionId): array<int, array{message: string, context: array}>

setPendingRequestsProvider()

Set a provider function to retrieve all pending server-initiated requests.

public setPendingRequestsProvider(callable(Uuid $sessionId): array<int, array<string, mixed>> $provider) : void

The transport calls this to decide if it should wait for a client response before resuming a Fiber.

Parameters
$provider : callable(Uuid $sessionId): array<int, array<string, mixed>>

setResponseFinder()

Set a finder function to check for a specific client response.

public setResponseFinder(callable(int, Uuid): FiberResume $finder) : void
Parameters
$finder : callable(int, Uuid): FiberResume

setSessionId()

Set the session ID for the current transport context.

public setSessionId(Uuid|null $sessionId) : void
Parameters
$sessionId : Uuid|null

The session ID, or null to clear


        
On this page

Search results