Table of Contents

Class StreamClientTransport

Namespace
ModelContextProtocol.Protocol.Transport
Assembly
ModelContextProtocol.dll

Provides an IClientTransport implemented around a pair of input/output streams.

public sealed class StreamClientTransport : IClientTransport
Inheritance
StreamClientTransport
Implements
Inherited Members

Remarks

This transport is useful for scenarios where you already have established streams for communication, such as custom network protocols, pipe connections, or for testing purposes. It works with any readable and writable streams.

Constructors

StreamClientTransport(Stream, Stream, ILoggerFactory?)

Initializes a new instance of the StreamClientTransport class.

public StreamClientTransport(Stream serverInput, Stream serverOutput, ILoggerFactory? loggerFactory = null)

Parameters

serverInput Stream

The stream representing the connected server's input. Writes to this stream will be sent to the server.

serverOutput Stream

The stream representing the connected server's output. Reads from this stream will receive messages from the server.

loggerFactory ILoggerFactory

A logger factory for creating loggers.

Properties

Name

Gets a transport identifier, used for logging purposes.

public string Name { get; }

Property Value

string

Methods

ConnectAsync(CancellationToken)

Asynchronously establishes a transport session with an MCP server and returns a transport for the duplex message stream.

public Task<ITransport> ConnectAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken

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

Returns

Task<ITransport>

Returns an interface for the duplex message stream.

Remarks

This method is responsible for initializing the connection to the server using the specific transport mechanism implemented by the derived class. The returned ITransport interface provides methods to send and receive messages over the established connection.

The lifetime of the returned ITransport instance is typically managed by the ModelContextProtocol.Client.McpClient that uses this transport. When the client is disposed, it will dispose the transport session as well.

This method is used by McpClientFactory to initialize the connection.

Exceptions

McpTransportException

The transport connection could not be established.