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
$exception  : Throwable|null
$logger  : LoggerInterface

Methods

__construct()  : mixed
__toString()  : string
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() ]) : mixed
Parameters
$callback : callable(): void

The callback to execute when stream is read

$logger : LoggerInterface = new NullLogger()

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