MCP PHP SDK

CallbackStream implements Stringable, StreamInterface

FinalYes

A PSR-7 StreamInterface that executes a callback when read.

This enables true streaming with echo/flush() for SSE (Server-Sent Events). The callback is executed once when the stream is first read and can write directly to the output buffer (e.g. via echo + flush()).

Example usage for SSE:

$stream = new CallbackStream(function() {
    echo "event: message\n";
    echo "data: {\"hello\":\"world\"}\n\n";
    flush();
});
Tags
author

Kyrian Obikwelu koshnawaza@gmail.com

Table of Contents

Interfaces

Stringable
StreamInterface

Properties

$callback  : mixed
$called  : bool
$chunks  : Closure|null
$exception  : Throwable|null
$logger  : LoggerInterface

Methods

__construct()  : mixed
__toString()  : string
chunks()  : Generator<int, string>|null
The body's SSE text, for a runtime that writes it itself; null when only reading this stream writes it.
close()  : void
detach()  : mixed
eof()  : bool
getContents()  : string
getMetadata()  : mixed
getSize()  : int|null
isReadable()  : bool
isSeekable()  : bool
isWritable()  : bool
read()  : string
rewind()  : void
seek()  : void
tell()  : int
write()  : int
invoke()  : void

Properties

Methods

__construct()

public __construct(callable(): void $callback[, LoggerInterface $logger = new NullLogger() ][, callable(): Generator<int, string>|null $chunks = null ]) : mixed
Parameters
$callback : callable(): void

The callback to execute when stream is read

$logger : LoggerInterface = new NullLogger()
$chunks : callable(): Generator<int, string>|null = null

The same body as SSE text, see self::chunks()

chunks()

The body's SSE text, for a runtime that writes it itself; null when only reading this stream writes it.

public chunks() : Generator<int, string>|null

On '', wait on the loop before asking for the next chunk; dropping the generator ends the body. Single use.

Tags
throws
LogicException

when the body was already produced

Return values
Generator<int, string>|null

getContents()

public getContents() : string
Return values
string

getMetadata()

public getMetadata([mixed $key = null ]) : mixed
Parameters
$key : mixed = null

read()

public read(mixed $length) : string
Parameters
$length : mixed
Return values
string

seek()

public seek(mixed $offset[, mixed $whence = SEEK_SET ]) : void
Parameters
$offset : mixed
$whence : mixed = SEEK_SET

write()

public write(mixed $string) : int
Parameters
$string : mixed
Return values
int
On this page

Search results