Table of Contents

Class TransportBase

Namespace
ModelContextProtocol.Protocol
Assembly
ModelContextProtocol.Core.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(JsonRpcMessage, 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

public bool IsConnected { get; }

Property Value

bool

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.

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(JsonRpcMessage, CancellationToken)

Sends a JSON-RPC message through the transport.

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

SetConnected()

Sets the transport to a connected state.

protected void SetConnected()

SetDisconnected(Exception?)

Sets the transport to a disconnected state.

protected void SetDisconnected(Exception? error = null)

Parameters

error Exception

Optional error information associated with the transport disconnecting. Should be if the disconnect was graceful and expected.

WriteMessageAsync(JsonRpcMessage, CancellationToken)

Writes a message to the message channel.

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

Parameters

message JsonRpcMessage

The message to write.

cancellationToken CancellationToken

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

Returns

Task