Class HttpClientTransport
- Namespace
- ModelContextProtocol.Client
- Assembly
- ModelContextProtocol.Core.dll
Provides an IClientTransport over HTTP using the Server-Sent Events (SSE) or Streamable HTTP protocol.
public sealed class HttpClientTransport : IClientTransport, IAsyncDisposable
- Inheritance
-
HttpClientTransport
- Implements
- Inherited Members
Remarks
This transport connects to an MCP server over HTTP using SSE or Streamable HTTP, allowing for real-time server-to-client communication with a standard HTTP requests. Unlike the StdioClientTransport, this transport connects to an existing server rather than launching a new process.
Constructors
HttpClientTransport(HttpClientTransportOptions, ILoggerFactory?)
Initializes a new instance of the HttpClientTransport class.
public HttpClientTransport(HttpClientTransportOptions transportOptions, ILoggerFactory? loggerFactory = null)
Parameters
transportOptionsHttpClientTransportOptionsConfiguration options for the transport.
loggerFactoryILoggerFactoryLogger factory for creating loggers used for diagnostic output during transport operations.
HttpClientTransport(HttpClientTransportOptions, HttpClient, ILoggerFactory?, bool)
Initializes a new instance of the HttpClientTransport class with a provided HTTP client.
public HttpClientTransport(HttpClientTransportOptions transportOptions, HttpClient httpClient, ILoggerFactory? loggerFactory = null, bool ownsHttpClient = false)
Parameters
transportOptionsHttpClientTransportOptionsConfiguration options for the transport.
httpClientHttpClientThe HTTP client instance used for requests.
loggerFactoryILoggerFactoryLogger factory for creating loggers used for diagnostic output during transport operations.
ownsHttpClientbooltrue to dispose of
httpClientwhen the transport is disposed; false if the caller is retaining ownership of thehttpClient's lifetime.
Properties
Name
Gets a transport identifier, used for logging purposes.
public string Name { get; }
Property Value
Methods
ConnectAsync(CancellationToken)
Asynchronously establishes a transport session with an MCP server and returns a transport for the duplex message stream.
public Task<ITransport> ConnectAsync(CancellationToken cancellationToken = default)
Parameters
cancellationTokenCancellationTokenThe CancellationToken to monitor for cancellation requests. The default is None.
Returns
- Task<ITransport>
Returns an interface for the duplex message stream.
Remarks
This method is responsible for initializing the connection to the server using the specific transport mechanism implemented by the derived class. The returned ITransport interface provides methods to send and receive messages over the established connection.
The lifetime of the returned ITransport instance is typically managed by the McpClient that uses this transport. When the client is disposed, it will dispose the transport session as well.
This method is used by McpClient to initialize the connection.
Exceptions
- InvalidOperationException
The transport connection could not be established.
DisposeAsync()
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources asynchronously.
public ValueTask DisposeAsync()
Returns
- ValueTask
A task that represents the asynchronous dispose operation.