StdioTransport extends BaseTransport
Provides a skeletal implementation of the TransportInterface to minimize the effort required to implement this interface.
Tags
Table of Contents
Properties
- $fiberYieldHandler : callable(Array, Array): void
- $logger : LoggerInterface
- $messageListener : callable(TransportInterface<string|int, mixed>, string, Array): void
-
$outgoingMessagesProvider
: callable(Uuid): array<int, array{message: string, context: array
}> - $pendingRequestsProvider : callable(Uuid): array<int, array<string, mixed>>
- $responseFinder : callable(int, Uuid): Response<string|int, array<string, mixed>>|Error|null
- $sessionEndListener : callable(Uuid): void
- $sessionFiber : McpFiber|null
- $sessionId : Uuid|null
- $input : mixed
- $output : mixed
- $runnerControl : RunnerControlInterface
Methods
- __construct() : mixed
- 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
- onSessionEnd() : void
- send() : void
- Send a message to the client immediately (bypassing session queue).
- setFiberYieldHandler() : void
- setOutgoingMessagesProvider() : void
- setPendingRequestsProvider() : void
- setResponseFinder() : void
- setSessionId() : void
- Set the session ID for the current transport context.
- checkForResponse() : Response|Error|null
-
getOutgoingMessages()
: array<int, array{message: string, context: array
}> - getPendingRequests() : array<int, array<string, mixed>>
- handleFiberYield() : void
- handleMessage() : void
- handleSessionEnd() : void
- processInput() : void
- flushOutgoingMessages() : void
- handleFiberTermination() : void
- processFiber() : void
- writeLine() : void
Properties
$fiberYieldHandler
protected
callable(Array, Array): void
$fiberYieldHandler
$logger
protected
LoggerInterface
$logger
$messageListener
protected
callable(TransportInterface<string|int, mixed>, string, Array): void
$messageListener
$outgoingMessagesProvider
protected
callable(Uuid): array<int, array{message: string, context: array}>
$outgoingMessagesProvider
$pendingRequestsProvider
protected
callable(Uuid): array<int, array<string, mixed>>
$pendingRequestsProvider
$responseFinder
protected
callable(int, Uuid): Response<string|int, array<string, mixed>>|Error|null
$responseFinder
$sessionEndListener
protected
callable(Uuid): void
$sessionEndListener
$sessionFiber
protected
McpFiber|null
$sessionFiber
= null
$sessionId
protected
Uuid|null
$sessionId
= null
$input
private
mixed
$input
= \STDIN
$output
private
mixed
$output
= \STDOUT
$runnerControl read-only
private
RunnerControlInterface
$runnerControl
= new RunnerControl()
Methods
__construct()
public
__construct([resource $input = STDIN ][, resource $output = STDOUT ][, LoggerInterface|null $logger = null ][, RunnerControlInterface $runnerControl = new RunnerControl() ]) : mixed
Parameters
- $input : resource = STDIN
- $output : resource = STDOUT
- $logger : LoggerInterface|null = null
- $runnerControl : RunnerControlInterface = new RunnerControl()
attachFiberToSession()
public
attachFiberToSession(McpFiber $fiber, Uuid $sessionId) : void
Parameters
- $fiber : McpFiber
- $sessionId : Uuid
close()
Closes the transport and cleans up any resources.
public
close() : void
initialize()
Initializes the transport.
public
initialize() : void
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()
public
onMessage(callable $listener) : void
Parameters
- $listener : callable
onSessionEnd()
public
onSessionEnd(callable $listener) : void
Parameters
- $listener : callable
send()
Send a message to the client immediately (bypassing session queue).
public
send(string $data, array<string|int, 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|int, mixed>
-
Context about this message:
- 'session_id': Uuid|null
- 'type': 'response'|'request'|'notification'
- 'status_code': int (HTTP status code for errors)
setFiberYieldHandler()
public
setFiberYieldHandler(callable(Array, Array): void $handler) : void
Parameters
- $handler : callable(Array, Array): void
setOutgoingMessagesProvider()
public
setOutgoingMessagesProvider(callable $provider) : void
Parameters
- $provider : callable
setPendingRequestsProvider()
public
setPendingRequestsProvider(callable $provider) : void
Parameters
- $provider : callable
setResponseFinder()
public
setResponseFinder(callable(int, Uuid): Array $finder) : void
Parameters
- $finder : callable(int, Uuid): Array
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
checkForResponse()
protected
checkForResponse(int $requestId, Uuid|null $sessionId) : Response|Error|null
Parameters
- $requestId : int
- $sessionId : Uuid|null
Tags
Return values
Response|Error|nullgetOutgoingMessages()
protected
getOutgoingMessages(Uuid|null $sessionId) : array<int, array{message: string, context: array}>
Parameters
- $sessionId : Uuid|null
Return values
array<int, array{message: string, context: arraygetPendingRequests()
protected
getPendingRequests(Uuid|null $sessionId) : array<int, array<string, mixed>>
Parameters
- $sessionId : Uuid|null
Return values
array<int, array<string, mixed>>handleFiberYield()
protected
handleFiberYield(FiberSuspend|null $yielded, Uuid|null $sessionId) : void
Parameters
- $yielded : FiberSuspend|null
- $sessionId : Uuid|null
handleMessage()
protected
handleMessage(string $payload, Uuid|null $sessionId) : void
Parameters
- $payload : string
- $sessionId : Uuid|null
handleSessionEnd()
protected
handleSessionEnd(Uuid|null $sessionId) : void
Parameters
- $sessionId : Uuid|null
processInput()
protected
processInput() : void
flushOutgoingMessages()
private
flushOutgoingMessages() : void
handleFiberTermination()
private
handleFiberTermination() : void
processFiber()
private
processFiber() : void
writeLine()
private
writeLine(string $payload) : void
Parameters
- $payload : string