Skip to content

Commit d4d2a4b

Browse files
committed
Update all Transport comments
1 parent 9eb56d1 commit d4d2a4b

12 files changed

+101
-490
lines changed

src/ModelContextProtocol/Protocol/Transport/IClientTransport.cs

Lines changed: 19 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,56 +6,42 @@ namespace ModelContextProtocol.Protocol.Transport;
66
/// Represents a transport mechanism for Model Context Protocol (MCP) client-to-server communication.
77
/// </summary>
88
/// <remarks>
9+
/// <para>
910
/// The <see cref="IClientTransport"/> interface abstracts the communication layer between MCP clients
10-
/// and servers, allowing different transport protocols to be used interchangeably. The protocol
11-
/// provides several built-in implementations:
12-
/// <list type="bullet">
13-
/// <item><description><see cref="SseClientTransport"/>: HTTP-based transport using Server-Sent Events (SSE)</description></item>
14-
/// <item><description><see cref="StdioClientTransport"/>: Process-based transport using standard input/output</description></item>
15-
/// <item><description><see cref="StreamClientTransport"/>: Transport based on arbitrary input/output streams</description></item>
16-
/// </list>
11+
/// and servers, allowing different transport protocols to be used interchangeably.
12+
/// </para>
1713
/// <para>
18-
/// When creating an MCP client, you typically use the <see cref="McpClientFactory"/> to create
19-
/// a client with the appropriate transport based on a server configuration:
14+
/// When creating an <see cref="IMcpClient"/>, <see cref="McpClientFactory"/> is typically used, and is
15+
/// provided with the <see cref="IClientTransport"/> based on expected server configuration.
2016
/// </para>
21-
/// <code>
22-
/// // Create an MCP client using a stdio transport
23-
/// await using var mcpClient = await McpClientFactory.CreateAsync(new()
24-
/// {
25-
/// Id = "demo-server",
26-
/// Name = "Demo Server",
27-
/// TransportType = TransportTypes.StdIo,
28-
/// TransportOptions = new()
29-
/// {
30-
/// ["command"] = "path/to/server",
31-
/// ["arguments"] = "--some-arg value",
32-
/// }
33-
/// });
34-
/// </code>
3517
/// </remarks>
3618
public interface IClientTransport
3719
{
3820
/// <summary>
39-
/// Specifies a transport identifier used for logging purposes.
21+
/// Gets a transport identifier, used for logging purposes.
4022
/// </summary>
4123
string Name { get; }
4224

4325
/// <summary>
44-
/// Asynchronously establishes a transport session with an MCP server and returns an interface for the duplex JSON-RPC message stream.
26+
/// Asynchronously establishes a transport session with an MCP server and returns a transport for the duplex message stream.
4527
/// </summary>
4628
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
47-
/// <returns>Returns an interface for the duplex JSON-RPC message stream.</returns>
29+
/// <returns>Returns an interface for the duplex message stream.</returns>
4830
/// <remarks>
31+
/// <para>
4932
/// This method is responsible for initializing the connection to the server using the specific transport
5033
/// mechanism implemented by the derived class. The returned <see cref="ITransport"/> interface
51-
/// provides methods to send and receive JSON-RPC messages over the established connection.
52-
///
53-
/// <para>Implementations should handle connection errors appropriately and clean up resources if connection fails.</para>
54-
///
55-
/// <para>The lifetime of the returned <see cref="ITransport"/> instance is typically managed by the
34+
/// provides methods to send and receive messages over the established connection.
35+
/// </para>
36+
/// <para>
37+
/// The lifetime of the returned <see cref="ITransport"/> instance is typically managed by the
5638
/// <see cref="McpClient"/> that uses this transport. When the client is disposed, it will dispose
57-
/// the transport session as well.</para>
39+
/// the transport session as well.
40+
/// </para>
41+
/// <para>
42+
/// This method is used by <see cref="McpClientFactory"/> to initialize the connection.
43+
/// </para>
5844
/// </remarks>
59-
/// <exception cref="McpTransportException">Thrown when the transport connection cannot be established.</exception>
45+
/// <exception cref="McpTransportException">The transport connection could not be established.</exception>
6046
Task<ITransport> ConnectAsync(CancellationToken cancellationToken = default);
6147
}

src/ModelContextProtocol/Protocol/Transport/ITransport.cs

Lines changed: 17 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using System.Threading.Channels;
2+
using ModelContextProtocol.Client;
23
using ModelContextProtocol.Protocol.Messages;
4+
using ModelContextProtocol.Server;
35

46
namespace ModelContextProtocol.Protocol.Transport;
57

@@ -8,32 +10,19 @@ namespace ModelContextProtocol.Protocol.Transport;
810
/// </summary>
911
/// <remarks>
1012
/// <para>
11-
/// The <see cref="ITransport"/> interface is the core abstraction for bidirectional communication in the MCP architecture.
12-
/// It provides methods for sending and receiving JSON-RPC messages regardless of the underlying transport mechanism,
13-
/// allowing protocol implementation to be decoupled from communication details.
13+
/// The <see cref="ITransport"/> interface is the core abstraction for bidirectional communication.
14+
/// It provides methods for sending and receiving messages, abstracting away the underlying transport mechanism
15+
/// and allowing protocol implementations to be decoupled from communication details.
1416
/// </para>
1517
/// <para>
1618
/// Implementations of <see cref="ITransport"/> handle the serialization, transmission, and reception of
17-
/// MCP messages over various channels like HTTP (Server-Sent Events), standard input/output streams,
18-
/// or arbitrary stream pairs.
19+
/// messages over various channels like standard input/output streams and HTTP (Server-Sent Events).
1920
/// </para>
2021
/// <para>
21-
/// While <see cref="IClientTransport"/> is responsible for establishing connections,
22+
/// While <see cref="IClientTransport"/> is responsible for establishing a client's connection,
2223
/// <see cref="ITransport"/> represents an established session. Client implementations typically obtain an
2324
/// <see cref="ITransport"/> instance by calling <see cref="IClientTransport.ConnectAsync"/>.
2425
/// </para>
25-
/// <para>
26-
/// The lifecycle of a transport typically follows this pattern:
27-
/// <list type="number">
28-
/// <item>Creation of the transport instance (via client connection or server acceptance)</item>
29-
/// <item>Usage through SendMessageAsync/MessageReader while IsConnected is true</item>
30-
/// <item>Disposal through DisposeAsync, which should clean up all resources</item>
31-
/// </list>
32-
/// </para>
33-
/// <para>
34-
/// Implementations must handle connection state changes, message serialization/deserialization,
35-
/// and proper resource cleanup when disposed.
36-
/// </para>
3726
/// </remarks>
3827
public interface ITransport : IAsyncDisposable
3928
{
@@ -43,15 +32,12 @@ public interface ITransport : IAsyncDisposable
4332
/// <remarks>
4433
/// <para>
4534
/// The <see cref="IsConnected"/> property indicates the current state of the transport connection.
46-
/// When <c>true</c>, the transport is ready to send and receive messages. When <c>false</c>,
47-
/// any attempt to send messages will typically throw an exception.
48-
/// </para>
49-
/// <para>
50-
/// The property transitions to <c>true</c> when the transport successfully establishes a connection,
51-
/// and transitions to <c>false</c> when the transport is disposed or encounters a connection error.
35+
/// When <see langword="true"/>, the transport is ready to send and receive messages. When <see langword="false"/>,
36+
/// any attempt to send messages will typically result in exceptions being thrown.
5237
/// </para>
5338
/// <para>
54-
/// Consumers should check this property before attempting to send messages to prevent exceptions.
39+
/// The property transitions to <see langword="true"/> when the transport successfully establishes a connection,
40+
/// and transitions to <see langword="false"/> when the transport is disposed or encounters a connection error.
5541
/// </para>
5642
/// </remarks>
5743
bool IsConnected { get; }
@@ -65,50 +51,28 @@ public interface ITransport : IAsyncDisposable
6551
/// It returns a <see cref="ChannelReader{T}"/> which allows consuming messages in a thread-safe manner.
6652
/// </para>
6753
/// <para>
68-
/// This property is typically used in one of the following ways:
69-
/// <list type="bullet">
70-
/// <item>
71-
/// Async enumeration: <c>await foreach (var message in transport.MessageReader.ReadAllAsync(cancellationToken))</c>
72-
/// </item>
73-
/// <item>
74-
/// Wait and read pattern: <c>await transport.MessageReader.WaitToReadAsync(cancellationToken); transport.MessageReader.TryRead(out var message);</c>
75-
/// </item>
76-
/// </list>
77-
/// </para>
78-
/// <para>
7954
/// The reader will continue to provide messages as long as the transport is connected. When the transport
80-
/// is disconnected or disposed, the channel will be completed and no more messages will be available.
81-
/// </para>
82-
/// <para>
83-
/// When using <see cref="MessageReader"/>, consumers should check <see cref="IsConnected"/> before performing long-running
84-
/// operations to ensure the transport remains viable throughout message processing.
55+
/// is disconnected or disposed, the channel will be completed and no more messages will be available after
56+
/// any already transmitted messages are consumed.
8557
/// </para>
8658
/// </remarks>
8759
ChannelReader<IJsonRpcMessage> MessageReader { get; }
8860

8961
/// <summary>
9062
/// Sends a JSON-RPC message through the transport.
9163
/// </summary>
92-
/// <param name="message">The JSON-RPC message to send. This can be any type that implements IJsonRpcMessage.</param>
64+
/// <param name="message">The JSON-RPC message to send.</param>
9365
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
9466
/// <returns>A task that represents the asynchronous send operation.</returns>
95-
/// <exception cref="InvalidOperationException">Thrown when the transport is not connected.</exception>
67+
/// <exception cref="InvalidOperationException">The transport is not connected.</exception>
9668
/// <remarks>
9769
/// <para>
9870
/// This method serializes and sends the provided JSON-RPC message through the transport connection.
99-
/// Before sending, it checks if the transport is connected using the <see cref="IsConnected"/> property.
100-
/// </para>
101-
/// <para>
102-
/// The behavior of this method varies by transport implementation:
103-
/// <list type="bullet">
104-
/// <item>For SSE transports, messages are written to the SSE event stream</item>
105-
/// <item>For stream transports, messages are serialized and written to the underlying stream</item>
106-
/// <item>For stdio transports, messages are written to the standard output stream</item>
107-
/// </list>
10871
/// </para>
10972
/// <para>
11073
/// This is a core method used by higher-level abstractions in the MCP protocol implementation.
111-
/// Most client code should use the higher-level methods provided by <see cref="IMcpEndpoint"/>
74+
/// Most client code should use the higher-level methods provided by <see cref="IMcpEndpoint"/>,
75+
/// <see cref="McpEndpointExtensions"/>, <see cref="McpClientExtensions"/>, or <see cref="McpServerExtensions"/>,
11276
/// rather than accessing this method directly.
11377
/// </para>
11478
/// </remarks>

src/ModelContextProtocol/Protocol/Transport/McpTransportException.cs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,19 @@
1-
// Protocol/Transport/IMcpTransport.cs
2-
namespace ModelContextProtocol.Protocol.Transport;
3-
4-
// Protocol/Transport/McpTransportException.cs
1+
namespace ModelContextProtocol.Protocol.Transport;
52

63
/// <summary>
74
/// Represents errors that occur in MCP transport operations.
85
/// </summary>
96
public class McpTransportException : Exception
107
{
118
/// <summary>
12-
/// Initializes a new instance of the McpTransportException class.
9+
/// Initializes a new instance of the <see cref="McpTransportException"/> class.
1310
/// </summary>
1411
public McpTransportException()
1512
{
1613
}
1714

1815
/// <summary>
19-
/// Initializes a new instance of the McpTransportException class with a specified error message.
16+
/// Initializes a new instance of the <see cref="McpTransportException"/> class with a specified error message.
2017
/// </summary>
2118
/// <param name="message">The message that describes the error.</param>
2219
public McpTransportException(string message)
@@ -25,7 +22,7 @@ public McpTransportException(string message)
2522
}
2623

2724
/// <summary>
28-
/// Initializes a new instance of the McpTransportException class with a specified error message
25+
/// Initializes a new instance of the <see cref="McpTransportException"/> class with a specified error message
2926
/// and a reference to the inner exception that is the cause of this exception.
3027
/// </summary>
3128
/// <param name="message">The message that describes the error.</param>

src/ModelContextProtocol/Protocol/Transport/SseClientTransport.cs

Lines changed: 10 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -4,44 +4,13 @@
44
namespace ModelContextProtocol.Protocol.Transport;
55

66
/// <summary>
7-
/// The ServerSideEvents client transport implementation
7+
/// Provides an <see cref="IClientTransport"/> over HTTP using the Server-Sent Events (SSE) protocol.
88
/// </summary>
99
/// <remarks>
10-
/// <para>
11-
/// This transport connects to an MCP server over HTTP using the Server-Sent Events protocol,
10+
/// This transport connects to an MCP server over HTTP using SSE,
1211
/// allowing for real-time server-to-client communication with a standard HTTP request.
1312
/// Unlike the <see cref="StdioClientTransport"/>, this transport connects to an existing server
1413
/// rather than launching a new process.
15-
/// </para>
16-
/// <para>
17-
/// SSE is a unidirectional communication protocol where the server can push messages to the client
18-
/// in real-time. For bidirectional communication, this transport sends client messages via regular
19-
/// HTTP POST requests to a configured endpoint, while receiving server messages through the SSE connection.
20-
/// </para>
21-
/// <para>
22-
/// This transport is ideal for web-based scenarios where the server is accessible via HTTP/HTTPS
23-
/// and provides a standard way to integrate with MCP servers running in cloud environments or behind
24-
/// API gateways.
25-
/// </para>
26-
/// <example>
27-
/// <code>
28-
/// // Creating an MCP client with SSE transport
29-
/// await using var mcpClient = await McpClientFactory.CreateAsync(new McpServerConfig
30-
/// {
31-
/// Id = "server-id",
32-
/// Name = "Server Name",
33-
/// TransportType = TransportTypes.Sse,
34-
/// TransportOptions = new()
35-
/// {
36-
/// ["baseUrl"] = "https://api.example.com/mcp",
37-
/// ["headers"] = new Dictionary&lt;string, string&gt;
38-
/// {
39-
/// ["Authorization"] = "Bearer token123"
40-
/// }
41-
/// }
42-
/// });
43-
/// </code>
44-
/// </example>
4514
/// </remarks>
4615
public sealed class SseClientTransport : IClientTransport, IAsyncDisposable
4716
{
@@ -51,13 +20,12 @@ public sealed class SseClientTransport : IClientTransport, IAsyncDisposable
5120
private readonly bool _ownsHttpClient;
5221

5322
/// <summary>
54-
/// SSE transport for client endpoints. Unlike stdio it does not launch a process, but connects to an existing server.
55-
/// The HTTP server can be local or remote, and must support the SSE protocol.
23+
/// Initializes a new instance of the <see cref="SseClientTransport"/> class.
5624
/// </summary>
5725
/// <param name="transportOptions">Configuration options for the transport.</param>
58-
/// <param name="loggerFactory">Logger factory for creating loggers.</param>
26+
/// <param name="loggerFactory">Logger factory for creating loggers used for diagnostic output during transport operations.</param>
5927
public SseClientTransport(SseClientTransportOptions transportOptions, ILoggerFactory? loggerFactory = null)
60-
: this(transportOptions, new HttpClient(), loggerFactory, true)
28+
: this(transportOptions, new HttpClient(), loggerFactory, ownsHttpClient: true)
6129
{
6230
}
6331

@@ -66,13 +34,11 @@ public SseClientTransport(SseClientTransportOptions transportOptions, ILoggerFac
6634
/// </summary>
6735
/// <param name="transportOptions">Configuration options for the transport.</param>
6836
/// <param name="httpClient">The HTTP client instance used for requests.</param>
69-
/// <param name="loggerFactory">Logger factory for creating loggers. Used for diagnostic output during transport operations.
70-
/// If null, a NullLogger will be used that doesn't produce any output.</param>
71-
/// <param name="ownsHttpClient">True to dispose HTTP client when the transport is disposed; false to leave it for the caller to manage.</param>
72-
/// <remarks>
73-
/// This constructor allows providing an external HTTP client, which can be useful for advanced scenarios
74-
/// where you need to configure the HTTP client with custom handlers, base address, or default headers.
75-
/// </remarks>
37+
/// <param name="loggerFactory">Logger factory for creating loggers used for diagnostic output during transport operations.</param>
38+
/// <param name="ownsHttpClient">
39+
/// <see langword="true"/> to dispose of <paramref name="httpClient"/> when the transport is disposed;
40+
/// <see langword="false"/> if the caller is retaining ownership of the <paramref name="httpClient"/>'s lifetime.
41+
/// </param>
7642
public SseClientTransport(SseClientTransportOptions transportOptions, HttpClient httpClient, ILoggerFactory? loggerFactory = null, bool ownsHttpClient = false)
7743
{
7844
Throw.IfNull(transportOptions);

0 commit comments

Comments
 (0)