- 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
namestringloggerFactoryILoggerFactory
Properties
IsConnected
public bool IsConnected { get; }
Property Value
MessageReader
Gets a channel reader for receiving messages from the transport.
public ChannelReader<JsonRpcMessage> MessageReader { get; }
Property Value
Name
Gets the name that identifies this transport endpoint in logs.
protected string Name { get; }
Property Value
SessionId
Gets an identifier associated with the current MCP session.
public virtual string? SessionId { get; protected set; }
Property Value
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
messageJsonRpcMessageThe JSON-RPC message to send.
cancellationTokenCancellationTokenThe 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
errorExceptionOptional 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
messageJsonRpcMessageThe message to write.
cancellationTokenCancellationTokenThe CancellationToken to monitor for cancellation requests. The default is None.