Table of Contents

Class DistributedCacheEventStreamStore

Namespace
ModelContextProtocol.Server
Assembly
ModelContextProtocol.dll

An ISseEventStreamStore implementation backed by IDistributedCache.

public sealed class DistributedCacheEventStreamStore : ISseEventStreamStore
Inheritance
DistributedCacheEventStreamStore
Implements
Inherited Members

Remarks

This implementation stores SSE events in a distributed cache, enabling resumability across multiple server instances. Event IDs are encoded with session, stream, and sequence information to allow efficient retrieval of events after a given point.

The writer maintains in-memory state for sequence number generation, as there is guaranteed to be only one writer per stream. Readers may be created from separate processes.

Constructors

DistributedCacheEventStreamStore(IDistributedCache, DistributedCacheEventStreamStoreOptions?, ILogger<DistributedCacheEventStreamStore>?)

Initializes a new instance of the DistributedCacheEventStreamStore class.

public DistributedCacheEventStreamStore(IDistributedCache cache, DistributedCacheEventStreamStoreOptions? options = null, ILogger<DistributedCacheEventStreamStore>? logger = null)

Parameters

cache IDistributedCache

The distributed cache to use for storage.

options DistributedCacheEventStreamStoreOptions

Optional configuration options for the store.

logger ILogger<DistributedCacheEventStreamStore>

Optional logger for diagnostic output.

Methods

CreateStreamAsync(SseEventStreamOptions, CancellationToken)

Creates a new SSE event stream with the specified options.

public ValueTask<ISseEventStreamWriter> CreateStreamAsync(SseEventStreamOptions options, CancellationToken cancellationToken = default)

Parameters

options SseEventStreamOptions

The configuration options for the new stream.

cancellationToken CancellationToken

A token to cancel the operation.

Returns

ValueTask<ISseEventStreamWriter>

A writer for the newly created event stream.

GetStreamReaderAsync(string, CancellationToken)

Gets a reader for an existing event stream based on the last event ID.

public ValueTask<ISseEventStreamReader?> GetStreamReaderAsync(string lastEventId, CancellationToken cancellationToken = default)

Parameters

lastEventId string

The ID of the last event received by the client, used to resume from that point.

cancellationToken CancellationToken

A token to cancel the operation.

Returns

ValueTask<ISseEventStreamReader>

A reader for the event stream, or null if no matching stream is found.