Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/modelcontextprotocol/csharp-sdk/llms.txt

Use this file to discover all available pages before exploring further.

Transport classes handle the underlying communication between MCP clients and servers. The SDK provides multiple transport implementations for different scenarios.

Namespace

ModelContextProtocol.Client

IClientTransport Interface

Base interface for all client transports.
public interface IClientTransport
{
    string Name { get; }
    Task<ITransport> ConnectAsync(CancellationToken cancellationToken = default);
}

StdioClientTransport

Provides a transport implemented via standard input/output (stdio), launching an external process and communicating through stdin/stdout streams.
public sealed class StdioClientTransport : IClientTransport

Constructor

public StdioClientTransport(
    StdioClientTransportOptions options,
    ILoggerFactory? loggerFactory = null)
options
StdioClientTransportOptions
required
Configuration options for the transport, including the command to execute, arguments, working directory, and environment variables.
loggerFactory
ILoggerFactory
A logger factory for creating loggers used for diagnostic output during transport operations.

StdioClientTransportOptions

Configuration options for stdio transport.
Command
string
required
The command to execute to start the server process (e.g., “node”, “python”, “dotnet”).
Arguments
IList<string>
The arguments to pass to the server process when it is started.
Name
string
A transport identifier used for logging purposes.
WorkingDirectory
string
The working directory for the server process.
EnvironmentVariables
IDictionary<string, string>
Environment variables to set for the server process. By default, the server process inherits the current environment’s variables.
ShutdownTimeout
TimeSpan
The timeout to wait for the server to shut down gracefully. Default is 5 seconds.
StandardErrorLines
Action<string>
A callback that is invoked for each line of stderr received from the server process.

Examples

var transport = new StdioClientTransport(new StdioClientTransportOptions
{
    Command = "node",
    Arguments = ["path/to/server.js"],
    WorkingDirectory = "/path/to/project",
    EnvironmentVariables = new Dictionary<string, string>
    {
        ["API_KEY"] = "your-api-key"
    },
    StandardErrorLines = line => Console.WriteLine($"Server stderr: {line}")
});

await using var client = await McpClient.CreateAsync(transport);

HttpClientTransport

Provides a transport over HTTP using Server-Sent Events (SSE) or Streamable HTTP protocol.
public sealed class HttpClientTransport : IClientTransport, IAsyncDisposable

Constructor

public HttpClientTransport(
    HttpClientTransportOptions transportOptions,
    ILoggerFactory? loggerFactory = null)
public HttpClientTransport(
    HttpClientTransportOptions transportOptions,
    HttpClient httpClient,
    ILoggerFactory? loggerFactory = null,
    bool ownsHttpClient = false)
transportOptions
HttpClientTransportOptions
required
The configuration options for the transport.
httpClient
HttpClient
The HTTP client instance used for requests. If not provided, a new instance is created.
loggerFactory
ILoggerFactory
The logger factory for creating loggers used for diagnostic output.
ownsHttpClient
bool
true to dispose of httpClient when the transport is disposed; false if the caller is retaining ownership.

HttpClientTransportOptions

Configuration options for HTTP transport.
Endpoint
Uri
required
The base address of the server for SSE or Streamable HTTP connections. Must be an absolute HTTP or HTTPS URI.
TransportMode
HttpTransportMode
The transport mode to use for the connection. Default is HttpTransportMode.AutoDetect, which attempts Streamable HTTP first and falls back to SSE.
Name
string
A transport identifier used for logging purposes.
ConnectionTimeout
TimeSpan
Timeout used to establish the initial connection to the SSE server. Default is 30 seconds.
AdditionalHeaders
IDictionary<string, string>
Custom HTTP headers to include in requests to the SSE server.
KnownSessionId
string
A session identifier that should be reused when connecting to a Streamable HTTP server. Allows reconnecting to an existing session.
OwnsSession
bool
Indicates whether this transport endpoint is responsible for ending the session on dispose. Default is true.
OAuth
ClientOAuthOptions
The authorization provider to use for OAuth authentication.
MaxReconnectionAttempts
int
The maximum number of consecutive reconnection attempts when an SSE stream is disconnected. Default is 5.
DefaultReconnectionInterval
TimeSpan
The default interval at which the client attempts reconnection after an SSE stream is disconnected. Default is 1 second.

HttpTransportMode Enum

public enum HttpTransportMode
{
    AutoDetect,      // Try Streamable HTTP, fall back to SSE
    StreamableHttp,  // Use Streamable HTTP only
    Sse              // Use Server-Sent Events only
}

Examples

var transport = new HttpClientTransport(new HttpClientTransportOptions
{
    Endpoint = new Uri("https://example.com/mcp")
});

await using var client = await McpClient.CreateAsync(transport);

Transport Selection Guide

Use StdioClientTransport When:

  • Running a server as a child process
  • Developing locally with servers in different languages (Node.js, Python, .NET)
  • Need to pass environment variables or configure working directory
  • Server doesn’t require network communication
  • Want to capture and process server stderr output

Use HttpClientTransport When:

  • Connecting to a remote MCP server over HTTP/HTTPS
  • Server is already running and accessible via URL
  • Need OAuth authentication
  • Require custom HTTP headers
  • Want to resume sessions (with Streamable HTTP mode)
  • Building web or cloud-based applications

Common Patterns

Error Handling

try
{
    var transport = new StdioClientTransport(new StdioClientTransportOptions
    {
        Command = "node",
        Arguments = ["server.js"]
    });
    
    await using var client = await McpClient.CreateAsync(transport);
    // Use client
}
catch (IOException ex)
{
    Console.WriteLine($"Failed to connect transport: {ex.Message}");
}
catch (McpException ex)
{
    Console.WriteLine($"MCP protocol error: {ex.Message}");
}

Logging

using Microsoft.Extensions.Logging;

var loggerFactory = LoggerFactory.Create(builder =>
{
    builder.AddConsole()
           .SetMinimumLevel(LogLevel.Debug);
});

var transport = new StdioClientTransport(
    new StdioClientTransportOptions
    {
        Command = "node",
        Arguments = ["server.js"]
    },
    loggerFactory);

await using var client = await McpClient.CreateAsync(transport, loggerFactory: loggerFactory);

See Also

Build docs developers (and LLMs) love