Overhaul McpClientFactory/McpServerFactory (#87)

Handlers are now specified before connecting the client/server. Otherwise, race conditions exist. Handlers move into being a part of the McpClientOptions/McpServerOptions, so that they're provided to the factories. Where relevant, the handlers are specified as part of the capability descriptors, so that they go hand in hand.

The factories are no longer stateful. Instead of allocating a factory and then calling a create method on it, you just call a static factory method.
This commit is contained in:
Stephen Toub 2025-03-16 12:08:52 -04:00 • committed by GitHub
commit 08b0a04dab
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -0,0 +1,135 @@
using McpDotNet.Protocol.Types;
namespace McpDotNet.Server;
/// <summary>
/// Container for handlers used in the creation of an MCP server.
/// </summary>
public sealed class McpServerHandlers
{
/// <summary>
/// Gets or sets the handler for list tools requests.
/// </summary>
public Func<RequestContext<ListToolsRequestParams>, CancellationToken, Task<ListToolsResult>>? ListToolsHandler { get; set; }
/// <summary>
/// Gets or sets the handler for call tool requests.
/// </summary>
public Func<RequestContext<CallToolRequestParams>, CancellationToken, Task<CallToolResponse>>? CallToolHandler { get; set; }
/// <summary>
/// Gets or sets the handler for list prompts requests.
/// </summary>
public Func<RequestContext<ListPromptsRequestParams>, CancellationToken, Task<ListPromptsResult>>? ListPromptsHandler { get; set; }
/// <summary>
/// Gets or sets the handler for get prompt requests.
/// </summary>
public Func<RequestContext<GetPromptRequestParams>, CancellationToken, Task<GetPromptResult>>? GetPromptHandler { get; set; }
/// <summary>
/// Gets or sets the handler for list resources requests.
/// </summary>
public Func<RequestContext<ListResourcesRequestParams>, CancellationToken, Task<ListResourcesResult>>? ListResourcesHandler { get; set; }
/// <summary>
/// Gets or sets the handler for read resources requests.
/// </summary>
public Func<RequestContext<ReadResourceRequestParams>, CancellationToken, Task<ReadResourceResult>>? ReadResourceHandler { get; set; }
/// <summary>
/// Gets or sets the handler for get completion requests.
/// </summary>
public Func<RequestContext<CompleteRequestParams>, CancellationToken, Task<CompleteResult>>? GetCompletionHandler { get; set; }
/// <summary>
/// Gets or sets the handler for subscribe to resources messages.
/// </summary>
public Func<RequestContext<string>, CancellationToken, Task>? SubscribeToResourcesHandler { get; set; }
/// <summary>
/// Gets or sets the handler for unsubscribe from resources messages.
/// </summary>
public Func<RequestContext<string>, CancellationToken, Task>? UnsubscribeFromResourcesHandler { get; set; }
/// <summary>
/// Overwrite any handlers in McpServerOptions with non-null handlers from this instance.
/// </summary>
/// <param name="options"></param>
/// <returns></returns>
internal McpServerOptions 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 ||
SubscribeToResourcesHandler is not null ||
UnsubscribeFromResourcesHandler is not null)
{
resourcesCapability = resourcesCapability is null ?
new()
{
ListResourcesHandler = ListResourcesHandler,
ReadResourceHandler = ReadResourceHandler,
SubscribeToResourcesHandler = SubscribeToResourcesHandler,
UnsubscribeFromResourcesHandler = UnsubscribeFromResourcesHandler,
} :
resourcesCapability with
{
ListResourcesHandler = ListResourcesHandler ?? resourcesCapability.ListResourcesHandler,
ReadResourceHandler = ReadResourceHandler ?? resourcesCapability.ReadResourceHandler,
SubscribeToResourcesHandler = SubscribeToResourcesHandler ?? resourcesCapability.SubscribeToResourcesHandler,
UnsubscribeFromResourcesHandler = UnsubscribeFromResourcesHandler ?? resourcesCapability.UnsubscribeFromResourcesHandler,
};
}
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,
};
}
return options with
{
GetCompletionHandler = GetCompletionHandler ?? options.GetCompletionHandler,
Capabilities = options.Capabilities is null ?
new()
{
Prompts = promptsCapability,
Resources = resourcesCapability,
Tools = toolsCapability,
} :
options.Capabilities with
{
Prompts = promptsCapability,
Resources = resourcesCapability,
Tools = toolsCapability,
},
};
}
}