Table of Contents

Class TransportBase

Namespace
ModelContextProtocol.Protocol.Transport
Assembly
ModelContextProtocol.dll

Provides a base class for implementing ITransport.

public abstract class TransportBase : ITransport, IAsyncDisposable
Inheritance
TransportBase
Implements
Derived
Inherited Members

Remarks

The TransportBase class provides core functionality required by most ITransport implementations, including message channel management, connection state tracking, and logging support.

Custom transport implementations should inherit from this class and implement the abstract SendMessageAsync(IJsonRpcMessage, CancellationToken) and DisposeAsync() methods to handle the specific transport mechanism being used.

Constructors

TransportBase(string, ILoggerFactory?)

Initializes a new instance of the TransportBase class.

protected TransportBase(string name, ILoggerFactory? loggerFactory)

Parameters

name string
loggerFactory ILoggerFactory

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.

Name

Gets the name that identifies this transport endpoint in logs.

protected string Name { get; }

Property Value

string

Remarks

This name is used in log messages to identify the source of transport-related events.

Methods

DisposeAsync()

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

public abstract ValueTask DisposeAsync()

Returns

ValueTask

A task that represents the asynchronous dispose operation.

SendMessageAsync(IJsonRpcMessage, CancellationToken)

Sends a JSON-RPC message through the transport.

public abstract 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.

SetConnected(bool)

Sets the connected state of the transport.

protected void SetConnected(bool isConnected)

Parameters

isConnected bool

Whether the transport is connected.

WriteMessageAsync(IJsonRpcMessage, CancellationToken)

Writes a message to the message channel.

protected Task WriteMessageAsync(IJsonRpcMessage message, CancellationToken cancellationToken = default)

Parameters

message IJsonRpcMessage

The message to write.

cancellationToken CancellationToken

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

Returns

Task