Table of Contents

Class McpClientBuilder

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

Fluent builder for constructing McpClient instances with various transport configurations.

public sealed class McpClientBuilder
Inheritance
McpClientBuilder
Inherited Members

Examples

HTTP Client:

var client = McpClientBuilder.ForHttp("https://api.example.com/mcp")
    .WithBearerToken("your-api-key")
    .WithTimeout(TimeSpan.FromMinutes(2))
    .Build();

Stdio Client (filesystem server):

var client = McpClientBuilder.ForStdio("npx", "@modelcontextprotocol/server-filesystem /tmp")
    .WithStderrHandler(line => Console.WriteLine($"[Server] {line}"))
    .Build();

Stdio Client (Python server with environment):

var client = McpClientBuilder.ForStdio("python", "-m my_mcp_server")
    .WithWorkingDirectory("/path/to/server")
    .WithEnvironment("API_KEY", "secret")
    .WithEnvironment("DEBUG", "true")
    .WithRequestTimeout(TimeSpan.FromMinutes(5))
    .Build();

Remarks

The builder pattern provides a clean, discoverable API for configuring MCP clients with different transport mechanisms. It supports:

  • HTTP + SSE: For remote MCP servers accessed over HTTP
  • Stdio: For local MCP servers running as subprocesses
  • Custom transports: For specialized transport implementations

Methods

Build()

Builds and returns the configured McpClient instance.

ForHttp(string)

Creates a builder configured for HTTP + SSE transport.

ForHttp(string, HttpClient)

Creates a builder configured for HTTP + SSE transport using an existing HttpClient.

ForStdio(StdioTransportOptions)

Creates a builder configured for stdio transport with detailed options.

ForStdio(string, string)

Creates a builder configured for stdio transport.

ForTransport(IMcpTransport)

Creates a builder using a custom transport implementation.

WithAutoRestart(int, TimeSpan?)

Enables automatic restart on unexpected process termination (stdio transport only).

WithBearerToken(string)

Sets the Bearer token for authentication (HTTP transport only).

WithClientInfo(string, string, string)

Sets the client information sent during MCP initialization.

WithEnvironment(IDictionary<string, string>)

Sets multiple environment variables for the subprocess (stdio transport only).

WithEnvironment(string, string)

Adds an environment variable for the subprocess (stdio transport only).

WithImmediateShutdown()

Disables graceful shutdown (stdio transport only).

WithRequestTimeout(TimeSpan)

Sets the request timeout for stdio transport.

WithStartupDelay(TimeSpan)

Sets the startup delay for stdio transport.

WithStderrHandler(Action<string>)

Sets a handler for stderr output from the subprocess (stdio transport only).

WithTimeout(TimeSpan)

Sets the HTTP request timeout (HTTP transport only).

WithUserAgent(string)

Sets the User-Agent string for HTTP requests.

WithWorkingDirectory(string)

Sets the working directory for the subprocess (stdio transport only).