MCP PHP SDK

TransportInterface

Tags
author

Christopher Hertel mail@christopher-hertel.de

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 the pending server-initiated request a suspended Fiber waits on.
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, Array $accessToken, Array $principal): void $listener) : void

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

The access token is the one the message was authorized with, if the transport authorizes requests. The principal identifies who the message was authenticated as, if the transport authenticates requests.

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

onSessionEnd()

Register a listener for when a session is terminated.

public onSessionEnd(callable(Uuid $sessionId, Array $principal): bool $listener) : void

The transport calls this when a client disconnects or explicitly ends their session. The principal identifies who ended it, if the transport authenticates requests. The listener throws a SessionStoreException when it cannot tell who opened the session.

Parameters
$listener : callable(Uuid $sessionId, Array $principal): bool

The callback function to execute when destroying a session, returning false when the session does not exist

send()

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

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

Used for the response of a handler that returns without suspending, on the exchange that carried its request. A handler that suspends returns its result from the fiber the transport resumes instead. An error with a status_code was raised before a session was resolved and rejects the whole exchange. 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, McpFiber $fiber): 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, McpFiber $fiber): 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 the pending server-initiated request a suspended Fiber waits on.

public setPendingRequestsProvider(callable(Uuid $sessionId, McpFiber $fiber): array<string, 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, McpFiber $fiber): array<string, array<string, mixed>>

setResponseFinder()

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

public setResponseFinder(callable(Uuid, Uuid): FiberResume $finder) : void
Parameters
$finder : callable(Uuid, 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