using ModelContextProtocol.Client; using ModelContextProtocol.Server; using System.Threading.Channels; namespace ModelContextProtocol.Protocol; /// /// Represents a transport mechanism for MCP (Model Context Protocol) communication between clients and servers. /// /// /// /// The 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 handle the serialization, transmission, and reception of /// messages over various channels like standard input/output streams and HTTP (Server-Sent Events). /// /// /// While is responsible for establishing a client's connection, /// represents an established session. Client implementations typically obtain an /// instance by calling . /// /// public interface ITransport : IAsyncDisposable { /// Gets an identifier associated with the current MCP session. /// /// Typically populated in transports supporting multiple sessions such as Streamable HTTP or SSE. /// Can return if the session hasn't initialized or if the transport doesn't /// support multiple sessions (as is the case with STDIO). /// string? SessionId { get; } /// /// Gets a channel reader for receiving messages from the transport. /// /// /// /// The provides access to incoming JSON-RPC messages received by the transport. /// It returns a 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. /// /// ChannelReader MessageReader { get; } /// /// Sends a JSON-RPC message through the transport. /// /// The JSON-RPC message to send. /// The to monitor for cancellation requests. The default is . /// A task that represents the asynchronous send operation. /// The transport is not connected. /// /// /// 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 , /// , or , /// rather than accessing this method directly. /// /// Task SendMessageAsync(JsonRpcMessage message, CancellationToken cancellationToken = default); }