using ModelContextProtocol.Protocol.Types; namespace ModelContextProtocol.Server; /// /// Container for handlers used in the creation of an MCP server. /// public sealed class McpServerHandlers { /// /// Gets or sets the handler for list tools requests. /// public Func, CancellationToken, Task>? ListToolsHandler { get; set; } /// /// Gets or sets the handler for call tool requests. /// public Func, CancellationToken, Task>? CallToolHandler { get; set; } /// /// Gets or sets the handler for list prompts requests. /// public Func, CancellationToken, Task>? ListPromptsHandler { get; set; } /// /// Gets or sets the handler for get prompt requests. /// public Func, CancellationToken, Task>? GetPromptHandler { get; set; } /// /// Gets or sets the handler for list resources requests. /// public Func, CancellationToken, Task>? ListResourcesHandler { get; set; } /// /// Gets or sets the handler for read resources requests. /// public Func, CancellationToken, Task>? ReadResourceHandler { get; set; } /// /// Gets or sets the handler for get completion requests. /// public Func, CancellationToken, Task>? GetCompletionHandler { get; set; } /// /// Gets or sets the handler for subscribe to resources messages. /// public Func, CancellationToken, Task>? SubscribeToResourcesHandler { get; set; } /// /// Gets or sets the handler for unsubscribe from resources messages. /// public Func, CancellationToken, Task>? UnsubscribeFromResourcesHandler { get; set; } /// /// Overwrite any handlers in McpServerOptions with non-null handlers from this instance. /// /// /// internal void OverwriteWithSetHandlers(McpServerOptions options) { PromptsCapability? promptsCapability = options.Capabilities?.Prompts; if (ListPromptsHandler is not null || GetPromptHandler is not null) { promptsCapability = promptsCapability is null ? new() { ListPromptsHandler = ListPromptsHandler, GetPromptHandler = GetPromptHandler, } : promptsCapability with { ListPromptsHandler = ListPromptsHandler ?? promptsCapability.ListPromptsHandler, GetPromptHandler = GetPromptHandler ?? promptsCapability.GetPromptHandler, }; } ResourcesCapability? resourcesCapability = options.Capabilities?.Resources; if (ListResourcesHandler is not null || ReadResourceHandler is not null) { resourcesCapability = resourcesCapability is null ? new() { ListResourcesHandler = ListResourcesHandler, ReadResourceHandler = ReadResourceHandler } : resourcesCapability with { ListResourcesHandler = ListResourcesHandler ?? resourcesCapability.ListResourcesHandler, ReadResourceHandler = ReadResourceHandler ?? resourcesCapability.ReadResourceHandler }; if (SubscribeToResourcesHandler is not null || UnsubscribeFromResourcesHandler is not null) { resourcesCapability = resourcesCapability with { SubscribeToResourcesHandler = SubscribeToResourcesHandler ?? resourcesCapability.SubscribeToResourcesHandler, UnsubscribeFromResourcesHandler = UnsubscribeFromResourcesHandler ?? resourcesCapability.UnsubscribeFromResourcesHandler, Subscribe = true }; } } ToolsCapability? toolsCapability = options.Capabilities?.Tools; if (ListToolsHandler is not null || CallToolHandler is not null) { toolsCapability = toolsCapability is null ? new() { ListToolsHandler = ListToolsHandler, CallToolHandler = CallToolHandler, } : toolsCapability with { ListToolsHandler = ListToolsHandler ?? toolsCapability.ListToolsHandler, CallToolHandler = CallToolHandler ?? toolsCapability.CallToolHandler, }; } options.Capabilities = options.Capabilities is null ? new() { Prompts = promptsCapability, Resources = resourcesCapability, Tools = toolsCapability, } : options.Capabilities with { Prompts = promptsCapability, Resources = resourcesCapability, Tools = toolsCapability, }; options.GetCompletionHandler = GetCompletionHandler ?? options.GetCompletionHandler; } }