Interface ITransport
- Namespace
- ModelContextProtocol.Protocol.Transport
- Assembly
- ModelContextProtocol.dll
Represents a transport mechanism for MCP (Model Context Protocol) communication between clients and servers.
public interface ITransport : IAsyncDisposable
- Inherited Members
Remarks
The ITransport interface is the core abstraction for bidirectional communication. It provides methods for sending and receiving messages, abstracting away the underlying transport mechanism and allowing protocol implementations to be decoupled from communication details.
Implementations of ITransport handle the serialization, transmission, and reception of messages over various channels like standard input/output streams and HTTP (Server-Sent Events).
While IClientTransport is responsible for establishing a client's connection, ITransport represents an established session. Client implementations typically obtain an ITransport instance by calling ConnectAsync(CancellationToken).
Properties
IsConnected
Gets whether the transport is currently connected and able to send/receive messages.
bool IsConnected { get; }
Property Value
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.
ChannelReader<IJsonRpcMessage> MessageReader { get; }
Property Value
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.
Methods
SendMessageAsync(IJsonRpcMessage, CancellationToken)
Sends a JSON-RPC message through the transport.
Task SendMessageAsync(IJsonRpcMessage message, CancellationToken cancellationToken = default)
Parameters
message
IJsonRpcMessageThe JSON-RPC message to send.
cancellationToken
CancellationTokenThe 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.