Table of Contents

Class McpClientHandlers

Namespace
ModelContextProtocol.Client
Assembly
ModelContextProtocol.Core.dll

Provides a container for handlers used in the creation of an MCP client.

public class McpClientHandlers
Inheritance
McpClientHandlers
Inherited Members

Remarks

This class provides a centralized collection of delegates that implement various capabilities of the Model Context Protocol.

Each handler in this class corresponds to a specific client endpoint in the Model Context Protocol and is responsible for processing a particular type of message. The handlers are used to customize the behavior of the MCP server by providing implementations for the various protocol operations.

When a server sends a message to the client, the appropriate handler is invoked to process it according to the protocol specification. Which handler is selected is done based on an ordinal, case-sensitive string comparison.

Properties

ElicitationHandler

Gets or sets the handler for processing ElicitationCreate requests.

public Func<ElicitRequestParams?, CancellationToken, ValueTask<ElicitResult>>? ElicitationHandler { get; set; }

Property Value

Func<ElicitRequestParams, CancellationToken, ValueTask<ElicitResult>>

Remarks

This handler function is called when an MCP server requests the client to provide additional information during interactions. The client must set this property for the elicitation capability to work.

The handler receives message parameters and a cancellation token. It should return a ElicitResult containing the response to the elicitation request.

NotificationHandlers

Gets or sets notification handlers to register with the client.

public IEnumerable<KeyValuePair<string, Func<JsonRpcNotification, CancellationToken, ValueTask>>>? NotificationHandlers { get; set; }

Property Value

IEnumerable<KeyValuePair<string, Func<JsonRpcNotification, CancellationToken, ValueTask>>>

Remarks

When constructed, the client will enumerate these handlers once, which may contain multiple handlers per notification method key. The client will not re-enumerate the sequence after initialization.

Notification handlers allow the client to respond to server-sent notifications for specific methods. Each key in the collection is a notification method name, and each value is a callback that will be invoked when a notification with that method is received.

Handlers provided via NotificationHandlers will be registered with the client for the lifetime of the client. For transient handlers, RegisterNotificationHandler(string, Func<JsonRpcNotification, CancellationToken, ValueTask>) may be used to register a handler that can then be unregistered by disposing of the IAsyncDisposable returned from the method.

RootsHandler

Gets or sets the handler for RootsList requests.

public Func<ListRootsRequestParams?, CancellationToken, ValueTask<ListRootsResult>>? RootsHandler { get; set; }

Property Value

Func<ListRootsRequestParams, CancellationToken, ValueTask<ListRootsResult>>

Remarks

This handler is invoked when a client sends a RootsList request to retrieve available roots. The handler receives request parameters and should return a ListRootsResult containing the collection of available roots.

SamplingHandler

Gets or sets the handler for processing SamplingCreateMessage requests.

public Func<CreateMessageRequestParams?, IProgress<ProgressNotificationValue>, CancellationToken, ValueTask<CreateMessageResult>>? SamplingHandler { get; set; }

Property Value

Func<CreateMessageRequestParams, IProgress<ProgressNotificationValue>, CancellationToken, ValueTask<CreateMessageResult>>

Remarks

This handler function is called when an MCP server requests the client to generate content using an AI model. The client must set this property for the sampling capability to work.

The handler receives message parameters, a progress reporter for updates, and a cancellation token. It should return a CreateMessageResult containing the generated content.

You can create a handler using the CreateSamplingHandler(IChatClient) extension method with any implementation of Microsoft.Extensions.AI.IChatClient.