You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
/// Represents a transport mechanism for Model Context Protocol (MCP) client-to-server communication.
7
7
/// </summary>
8
8
/// <remarks>
9
+
/// <para>
9
10
/// 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>
17
13
/// <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.
20
16
/// </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>
35
17
/// </remarks>
36
18
publicinterfaceIClientTransport
37
19
{
38
20
/// <summary>
39
-
/// Specifies a transport identifier used for logging purposes.
21
+
/// Gets a transport identifier, used for logging purposes.
40
22
/// </summary>
41
23
stringName{get;}
42
24
43
25
/// <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.
45
27
/// </summary>
46
28
/// <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>
48
30
/// <remarks>
31
+
/// <para>
49
32
/// This method is responsible for initializing the connection to the server using the specific transport
50
33
/// 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
56
38
/// <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>
58
44
/// </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>
/// 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.
14
16
/// </para>
15
17
/// <para>
16
18
/// 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).
19
20
/// </para>
20
21
/// <para>
21
-
/// While <see cref="IClientTransport"/> is responsible for establishing connections,
22
+
/// While <see cref="IClientTransport"/> is responsible for establishing a client's connection,
22
23
/// <see cref="ITransport"/> represents an established session. Client implementations typically obtain an
23
24
/// <see cref="ITransport"/> instance by calling <see cref="IClientTransport.ConnectAsync"/>.
24
25
/// </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>
37
26
/// </remarks>
38
27
publicinterfaceITransport:IAsyncDisposable
39
28
{
@@ -43,15 +32,12 @@ public interface ITransport : IAsyncDisposable
43
32
/// <remarks>
44
33
/// <para>
45
34
/// 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.
52
37
/// </para>
53
38
/// <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.
55
41
/// </para>
56
42
/// </remarks>
57
43
boolIsConnected{get;}
@@ -65,50 +51,28 @@ public interface ITransport : IAsyncDisposable
65
51
/// It returns a <see cref="ChannelReader{T}"/> which allows consuming messages in a thread-safe manner.
66
52
/// </para>
67
53
/// <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>
79
54
/// 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.
85
57
/// </para>
86
58
/// </remarks>
87
59
ChannelReader<IJsonRpcMessage>MessageReader{get;}
88
60
89
61
/// <summary>
90
62
/// Sends a JSON-RPC message through the transport.
91
63
/// </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>
93
65
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
94
66
/// <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>
96
68
/// <remarks>
97
69
/// <para>
98
70
/// 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>
108
71
/// </para>
109
72
/// <para>
110
73
/// 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"/>,
0 commit comments