Table of Contents

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

transportOptions HttpClientTransportOptions

Configuration options for the transport.

loggerFactory ILoggerFactory

Logger 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

transportOptions HttpClientTransportOptions

Configuration options for the transport.

httpClient HttpClient

The HTTP client instance used for requests.

loggerFactory ILoggerFactory

Logger factory for creating loggers used for diagnostic output during transport operations.

ownsHttpClient bool

true to dispose of httpClient when the transport is disposed; false if the caller is retaining ownership of the httpClient's lifetime.

Properties

Name

Gets a transport identifier, used for logging purposes.

public string Name { get; }

Property Value

string

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

cancellationToken CancellationToken

The 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.