Table of Contents

Class StreamableHttpServerTransport

Namespace
ModelContextProtocol.Protocol.Transport
Assembly
ModelContextProtocol.dll

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

public sealed class StreamableHttpServerTransport : ITransport, IAsyncDisposable
Inheritance
StreamableHttpServerTransport
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.

Properties

MessageReader

Gets a channel reader for receiving messages from the transport.

public ChannelReader<JsonRpcMessage> MessageReader { get; }

Property Value

ChannelReader<JsonRpcMessage>

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.

HandleGetRequest(Stream, CancellationToken)

Handles an optional SSE GET request a client using the Streamable HTTP transport might make by writing any unsolicited JSON-RPC messages sent via SendMessageAsync(JsonRpcMessage, CancellationToken) to the SSE response stream until cancellation is requested or the transport is disposed.

public Task HandleGetRequest(Stream sseResponseStream, CancellationToken cancellationToken)

Parameters

sseResponseStream Stream

The response stream to write MCP JSON-RPC messages as SSE events to.

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.

HandlePostRequest(IDuplexPipe, CancellationToken)

Handles a Streamable HTTP POST request processing both the request body and response body ensuring that JsonRpcResponse and other correlated messages are sent back to the client directly in response to the JsonRpcRequest that initiated the message.

public Task<bool> HandlePostRequest(IDuplexPipe httpBodies, CancellationToken cancellationToken)

Parameters

httpBodies IDuplexPipe

The duplex pipe facilitates the reading and writing of HTTP request and response data.

cancellationToken CancellationToken

This token allows for the operation to be canceled if needed.

Returns

Task<bool>

True, if data was written to the respond body. False, if nothing was written because the request body did not contain any JsonRpcRequest messages to respond to. The HTTP application should typically respond with an empty "202 Accepted" response in this scenario.

SendMessageAsync(JsonRpcMessage, CancellationToken)

Sends a JSON-RPC message through the transport.

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

Parameters

message JsonRpcMessage

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.