using Microsoft.Extensions.AI; using ModelContextProtocol.Protocol; using System.Diagnostics.CodeAnalysis; namespace ModelContextProtocol.Client; /// /// Provides a container for handlers used in the creation of an MCP client. /// /// /// /// 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. /// /// public class McpClientHandlers { /// Gets or sets notification handlers to register with the client. /// /// /// When constructed, the client will enumerate these handlers, which might contain multiple handlers per notification method key, once. /// 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 will be registered with the client for the lifetime of the client. /// For transient handlers, you can use to register a handler that can /// then be unregistered by disposing of the returned from the method. /// /// public IEnumerable>>? NotificationHandlers { get; set; } /// /// Gets or sets the handler for requests. /// /// /// This handler is invoked when a client sends a request to retrieve available roots. /// The handler receives request parameters and should return a containing the collection of available roots. /// public Func>? RootsHandler { get; set; } /// /// Gets or sets the handler for processing requests. /// /// /// /// 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 containing the response to the elicitation request. /// /// public Func>? ElicitationHandler { get; set; } /// /// Gets or sets the handler for processing requests. /// /// /// /// 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 containing the /// generated content. /// /// /// You can create a handler using the extension /// method with any implementation of . /// /// public Func, CancellationToken, ValueTask>? SamplingHandler { get; set; } /// /// Gets or sets the handler for processing notifications. /// /// /// /// This handler is called when the server sends a task status notification to inform the client /// about changes to a task's state. These notifications are optional and clients MUST NOT rely /// on receiving them. /// /// /// The handler receives the updated object containing the current task state, /// including its status, status message, and timestamps. /// /// /// This handler is typically used to update UI or trigger actions based on task progress /// without requiring explicit polling. /// /// [Experimental(Experimentals.Tasks_DiagnosticId, UrlFormat = Experimentals.Tasks_Url)] public Func? TaskStatusHandler { get; set; } }