Class McpClient
Client for Model Context Protocol (MCP) servers over HTTP using JSON-RPC 2.0.
Establishes a session (initialize
then notifications/initialized
), exposes the
server’s tool catalog (tools/list
), executes tool invocations (tools/call
) and
health checks (ping
), and preserves the MCP session id across requests.
Includes content negotiation fallbacks, retries for transient HTTP errors (408/429/5xx),
and consistent surfacing of JSON-RPC errors to callers.
public sealed class McpClient : IDisposable
- Inheritance
-
McpClient
- Implements
- Inherited Members
Constructors
- McpClient(string, HttpClient)
Initializes a new instance of McpClient using a caller-provided HttpClient. The caller retains ownership and is responsible for the lifecycle of the provided client.
- McpClient(string, TimeSpan?)
Initializes a new instance of McpClient with an internally owned HttpClient.
Properties
- ClientInfoName
Gets or sets the
clientInfo.name
value sent in theinitialize
RPC. Defaults toLMKit.NET
.
- ClientInfoTitle
Gets or sets the
clientInfo.title
value sent in theinitialize
RPC. Defaults toLMKit MCP Client
.
- ClientInfoVersion
Gets or sets the
clientInfo.version
value sent in theinitialize
RPC. Defaults to1.0.0
.
- InitialAccept
Gets or sets the initial Accept profile for requests. If the server responds with
406
, the client automatically falls back:JsonOrEventStream → JsonOnly → Wildcard → Omit
. Default is JsonOrEventStream.
- OnAuthFailed
Callback invoked when an authentication failure is detected (HTTP 401/403). Parameter is the HTTP status code (401 or 403).
- OnReceived
Callback invoked after an HTTP response is received. Parameters are the JSON-RPC method name, HTTP status code, and a truncated response body snippet.
- OnSending
Callback invoked immediately before an HTTP request is sent. Parameter is the JSON-RPC method name (e.g.,
tools/list
).
- SessionId
Gets the most recently observed MCP session identifier, as provided by the server either via the
Mcp-Session-Id
response header or in theinitialize
response payload.
- Tools
Gets the cached tool list returned by
tools/list
. If not yet fetched, this property synchronously fetches the list (blocking the current thread). Prefer using GetToolsAsync(CancellationToken) for asynchronous code paths.
- UserAgent
Gets or sets the User-Agent string added to outgoing requests. Changing this updates the DefaultRequestHeaders immediately. Defaults to
LMKitMCPClient/1.0
.
Methods
- Dispose()
Disposes managed resources held by the client. Safe to call multiple times.
- GetToolsAsync(CancellationToken)
Retrieves the server’s tool catalog once via
tools/list
and caches it for future access.
- Initialize(CancellationToken)
Synchronously ensures the MCP session is initialized and returns a clone of the JSON-RPC
initialize
response payload.
- InitializeAsync(CancellationToken)
Ensures the MCP session is initialized. If already initialized, returns a clone of the cached
initialize
response. If not, performsinitialize
and thennotifications/initialized
, caching the session id and payload. All concurrent callers await the same initialization task.
- RefreshTools(CancellationToken)
Synchronously clears the cached tool list and fetches a fresh catalog via
tools/list
.
- RefreshToolsAsync(CancellationToken)
Clears the cached tool list and fetches a fresh copy from the server.
- SetBearerToken(string)
Sets or clears the Bearer token used for authorization. Marking a new token invalidates the current session so the next call re-initializes with fresh headers.
- Shutdown(CancellationToken)
Synchronously shuts down the client, clearing cached state, canceling in-flight requests, and (when owned) recreating the internal HttpClient so the instance can be reused.
- ShutdownAsync(CancellationToken)
Gracefully shuts down the client: clears cached state, cancels pending requests, and recreates the owned HttpClient so the instance can be reused later.