Table of Contents

Class SseResponseStreamTransport

Namespace
ModelContextProtocol.Protocol.Transport
Assembly
ModelContextProtocol.dll

Provides an ITransport implementation using Server-Sent Events (SSE) for server-to-client communication.

public sealed class SseResponseStreamTransport : ITransport, IAsyncDisposable
Inheritance
SseResponseStreamTransport
Implements
Inherited Members

Remarks

This transport provides one-way communication from server to client using the SSE protocol over HTTP, while receiving client messages through a separate mechanism. It writes messages as SSE events to a response stream, typically associated with an HTTP response.

This transport is used in scenarios where the server needs to push messages to the client in real-time, such as when streaming completion results or providing progress updates during long-running operations.

Constructors

SseResponseStreamTransport(Stream, string)

Provides an ITransport implementation using Server-Sent Events (SSE) for server-to-client communication.

public SseResponseStreamTransport(Stream sseResponseStream, string messageEndpoint = "/message")

Parameters

sseResponseStream Stream
messageEndpoint string

Remarks

This transport provides one-way communication from server to client using the SSE protocol over HTTP, while receiving client messages through a separate mechanism. It writes messages as SSE events to a response stream, typically associated with an HTTP response.

This transport is used in scenarios where the server needs to push messages to the client in real-time, such as when streaming completion results or providing progress updates during long-running operations.

Properties

IsConnected

Gets whether the transport is currently connected and able to send/receive messages.

public bool IsConnected { get; }

Property Value

bool

Remarks

The IsConnected property indicates the current state of the transport connection. When true, the transport is ready to send and receive messages. When false, any attempt to send messages will typically result in exceptions being thrown.

The property transitions to true when the transport successfully establishes a connection, and transitions to false when the transport is disposed or encounters a connection error.

MessageReader

Gets a channel reader for receiving messages from the transport.

public ChannelReader<IJsonRpcMessage> MessageReader { get; }

Property Value

ChannelReader<IJsonRpcMessage>

Remarks

The MessageReader provides access to incoming JSON-RPC messages received by the transport. It returns a ChannelReader<T> which allows consuming messages in a thread-safe manner.

The reader will continue to provide messages as long as the transport is connected. When the transport is disconnected or disposed, the channel will be completed and no more messages will be available after any already transmitted messages are consumed.

Methods

DisposeAsync()

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources asynchronously.

public ValueTask DisposeAsync()

Returns

ValueTask

A task that represents the asynchronous dispose operation.

OnMessageReceivedAsync(IJsonRpcMessage, CancellationToken)

Handles incoming JSON-RPC messages received on the /message endpoint.

public Task OnMessageReceivedAsync(IJsonRpcMessage message, CancellationToken cancellationToken)

Parameters

message IJsonRpcMessage

The JSON-RPC message received from the client.

cancellationToken CancellationToken

The CancellationToken to monitor for cancellation requests. The default is None.

Returns

Task

A task representing the asynchronous operation to buffer the JSON-RPC message for processing.

Remarks

This method is the entry point for processing client-to-server communication in the SSE transport model. While the SSE protocol itself is unidirectional (server to client), this method allows bidirectional communication by handling HTTP POST requests sent to the message endpoint.

When a client sends a JSON-RPC message to the /message endpoint, the server calls this method to process the message and make it available to the MCP server via the MessageReader channel.

This method validates that the transport is connected before processing the message, ensuring proper sequencing of operations in the transport lifecycle.

Exceptions

InvalidOperationException

Thrown when there is an attempt to process a message before calling RunAsync(CancellationToken).

RunAsync(CancellationToken)

Starts the transport and writes the JSON-RPC messages sent via SendMessageAsync(IJsonRpcMessage, CancellationToken) to the SSE response stream until cancellation is requested or the transport is disposed.

public Task RunAsync(CancellationToken cancellationToken)

Parameters

cancellationToken CancellationToken

The CancellationToken to monitor for cancellation requests. The default is None.

Returns

Task

A task representing the send loop that writes JSON-RPC messages to the SSE response stream.

SendMessageAsync(IJsonRpcMessage, CancellationToken)

Sends a JSON-RPC message through the transport.

public Task SendMessageAsync(IJsonRpcMessage message, CancellationToken cancellationToken = default)

Parameters

message IJsonRpcMessage

The JSON-RPC message to send.

cancellationToken CancellationToken

The CancellationToken to monitor for cancellation requests. The default is None.

Returns

Task

A task that represents the asynchronous send operation.

Remarks

This method serializes and sends the provided JSON-RPC message through the transport connection.

This is a core method used by higher-level abstractions in the MCP protocol implementation. Most client code should use the higher-level methods provided by IMcpEndpoint, McpEndpointExtensions, McpClientExtensions, or McpServerExtensions, rather than accessing this method directly.

Exceptions

InvalidOperationException

The transport is not connected.