Table of Contents

Constructor McpClient

Namespace
LMKit.Mcp.Client
Assembly
LM-Kit.NET.dll

McpClient(string, TimeSpan?)

Initializes a new MCP client with an internally managed HTTP client.

public McpClient(string baseUrl, TimeSpan? timeout = null)

Parameters

baseUrl string

The base URL of the MCP server endpoint (e.g., https://api.example.com/mcp). Must be a valid HTTP or HTTPS URL.

timeout TimeSpan?

The timeout for HTTP requests. If null, defaults to 60 seconds. This applies to individual HTTP operations, not the entire MCP session.

Examples

// Basic usage with default timeout
using var client = new McpClient("https://api.example.com/mcp");

// With custom timeout
using var client = new McpClient(
    "https://api.example.com/mcp", 
    TimeSpan.FromMinutes(2)
);

Remarks

This constructor creates and manages its own HttpClient instance, which is automatically configured with compression support (GZip, Deflate, and Brotli when available). The client owns this HTTP client and will dispose of it when the MCP client is disposed.

If you need to share an HttpClient instance (recommended for dependency injection scenarios or connection pooling), use the McpClient(string, HttpClient) constructor instead.

Exceptions

ArgumentException

Thrown when baseUrl is null, empty, or consists only of whitespace.

McpClient(string, HttpClient)

Initializes a new MCP client using an externally provided HTTP client.

public McpClient(string baseUrl, HttpClient externalHttpClient)

Parameters

baseUrl string

The base URL of the MCP server endpoint (e.g., https://api.example.com/mcp). Must be a valid HTTP or HTTPS URL.

externalHttpClient HttpClient

An existing HttpClient instance to use for HTTP operations. The caller retains ownership and is responsible for disposing this instance.

Examples

// Using dependency injection
public class MyService
{
    private readonly McpClient _mcpClient;

    public MyService(HttpClient httpClient)
    {
        _mcpClient = new McpClient("https://api.example.com/mcp", httpClient);
    }
}

Remarks

Use this constructor when you want to manage the HttpClient lifecycle yourself. This is the recommended approach when using dependency injection or when you need to share an HTTP client instance across multiple components for better connection pooling.

The MCP client will attempt to set its User-Agent header on the provided client but will silently ignore any errors to avoid interfering with your existing HTTP client configuration. The MCP client will never dispose the provided HTTP client.

Exceptions

ArgumentException

Thrown when baseUrl is null, empty, or consists only of whitespace.

ArgumentNullException

Thrown when externalHttpClient is null.