Class TransportBase | MCP C# SDK

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

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>

Name

Gets the name that identifies this transport endpoint in logs.

protected string Name { get; }

Property Value

string

SessionId

Gets an identifier associated with the current MCP session.

public virtual string? SessionId { get; protected set; }

Property Value

string

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.

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 null 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