Table of Contents

Interface ITransport

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

MessageReader

Gets a channel reader for receiving messages from the transport.

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.

Methods

SendMessageAsync(JsonRpcMessage, CancellationToken)

Sends a JSON-RPC message through the transport.

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.