Class McpClient
Provides a client for communicating with Model Context Protocol (MCP) servers over HTTP using JSON-RPC 2.0.
public sealed class McpClient : IDisposable
- Inheritance
-
McpClient
- Implements
- Inherited Members
Remarks
The McpClient handles the complete MCP lifecycle including:
- Establishing and maintaining sessions with MCP servers
- Discovering available tools, resources, and prompts
- Executing tool invocations and resource operations
- Automatic retry logic for transient failures (408, 429, 5xx errors)
- Content negotiation with fallback support
- Real-time server notifications via Server-Sent Events (SSE)
Basic Usage:
// Create a client
using var client = new McpClient("https://api.example.com/mcp");
// Set authentication if required
client.SetBearerToken("your-api-token");
// Initialize the connection (happens automatically on first operation)
await client.InitializeAsync();
// Use the client to discover and call tools
var tools = await client.ListToolsAsync();
var result = await client.CallToolAsync("tool-name", parameters);
The client automatically manages the session lifecycle. If the connection is lost or authentication changes, subsequent operations will re-initialize the session transparently.
Constructors
- McpClient(string, HttpClient)
Initializes a new MCP client using an externally provided HTTP client.
- McpClient(string, TimeSpan?)
Initializes a new MCP client with an internally managed HTTP client.
Properties
- ClientInfoName
Gets or sets the client name sent during MCP initialization.
- ClientInfoTitle
Gets or sets the client title (display name) sent during MCP initialization.
- ClientInfoVersion
Gets or sets the client version sent during MCP initialization.
- InitialAccept
Gets or sets the initial Accept header profile for HTTP requests.
- McpProtocolVersion
Gets the MCP protocol version currently in use for communication with the server.
- Prompts
Gets a cached view of the server's prompt catalog. On first access, this property blocks to fetch the catalog via GetPromptsAsync(CancellationToken). Use RefreshPrompts(CancellationToken) to force a fresh fetch.
- Resources
Gets a cached view of the server's resource catalog. On first access, this property blocks to fetch the catalog via GetResourcesAsync(CancellationToken).
- ServerCapabilities
Gets the capabilities supported by the connected MCP server.
- SessionId
Gets the current session identifier for this MCP connection.
- 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 sent with HTTP requests.
Methods
- Dispose()
Releases all resources used by the MCP client.
- GetPrompt(string, IDictionary<string, object>, CancellationToken)
Retrieves a fully rendered prompt by name via
prompts/get(blocking wrapper).
- GetPromptAsync(string, IDictionary<string, object>, CancellationToken)
Retrieves a fully rendered prompt by name via
prompts/get.
- GetPrompts(CancellationToken)
Synchronously retrieves the server's prompt catalog. This is a blocking wrapper over GetPromptsAsync(CancellationToken) and returns the cached list thereafter.
- GetPromptsAsync(CancellationToken)
Retrieves the server's prompt catalog. Results are cached after the first call until invalidation or a manual refresh via RefreshPromptsAsync(CancellationToken).
- GetResources(CancellationToken)
Retrieves the list of resources offered by the server (synchronous wrapper). Results are cached after the first call until invalidation or a manual refresh.
- GetResourcesAsync(CancellationToken)
Retrieves the list of resources offered by the server. Results are cached after the first call until invalidation or a manual refresh.
- GetTools(CancellationToken)
Retrieves the server’s tool catalog once via
tools/listand caches it for future access.
- GetToolsAsync(CancellationToken)
Retrieves and caches the server’s tool catalog via
tools/list. Concurrent callers share the same underlying fetch task.
- HasCapability(McpServerCapabilities)
Checks whether the server supports a specific capability.
- Initialize(CancellationToken)
Synchronously initializes the MCP session and returns the server's initialization response.
- InitializeAsync(CancellationToken)
Asynchronously initializes the MCP session and returns the server's initialization response.
- ReadResource(string, CancellationToken)
Reads the content of a specific resource from the server (synchronous wrapper).
- ReadResourceAsync(string, CancellationToken)
Reads the content of a specific resource from the server.
- RefreshPrompts(CancellationToken)
Clears the cached prompt catalog and fetches a fresh copy (blocking wrapper).
- RefreshPromptsAsync(CancellationToken)
Clears the cached prompt catalog and fetches a fresh copy from the server (async).
- RefreshTools(CancellationToken)
Clears the cached tool list and fetches a fresh catalog via
tools/list.
- RefreshToolsAsync(CancellationToken)
Clears the cached tool list and fetches a fresh catalog via
tools/list.
- SetBearerToken(string)
Sets or clears the Bearer token used for authentication with the MCP server.
- Shutdown(CancellationToken)
Synchronously shuts down the MCP client, clearing all cached state and preparing for potential reuse.
- ShutdownAsync(CancellationToken)
Asynchronously shuts down the MCP client, clearing all cached state and preparing for potential reuse.
Events
- AuthFailed
Gets or sets a callback invoked when an authentication failure is detected.
- CatalogChanged
Raised when the server indicates that any catalog has changed (tools, resources, prompts, or roots). Fired after the client invalidates its local cache for that catalog.
- PromptsChanged
Raised when the server indicates that the prompt catalog has changed. Fired after the client invalidates its local prompt cache.
- Received
Gets or sets a callback invoked after receiving an HTTP response from the server.
- ResourcesChanged
Raised when the server indicates that the resource catalog has changed. Fired after the client invalidates its local resource cache.
- Sending
Invoked immediately before sending an HTTP request to the server.
- ToolsChanged
Raised when the server indicates that the tool catalog has changed. Fired after the client invalidates its local tool cache.