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

@ -6,6 +6,7 @@ using System.Text.Json;
using McpDotNet.Configuration;
using McpDotNet.Protocol.Types;
using McpDotNet.Server;
using McpDotNet.Utils;
using Microsoft.Extensions.DependencyInjection;
namespace McpDotNet;
@ -40,15 +41,14 @@ public static partial class McpServerBuilderExtensions
/// <param name="toolTypes">Types with marked methods to add as tools to the server.</param>
public static IMcpServerBuilder WithTools(this IMcpServerBuilder builder, params Type[] toolTypes)
{
if (builder is null)
{
throw new ArgumentNullException(nameof(builder));
}
Throw.IfNull(builder);
if (toolTypes is null || toolTypes.Length == 0)
{
throw new ArgumentException("At least one tool type must be provided.", nameof(toolTypes));
}
var tools = new List<Tool>();
List<Tool> tools = [];
Dictionary<string, Func<RequestContext<CallToolRequestParams>, CancellationToken, Task<CallToolResponse>>> callbacks = [];
foreach (var type in toolTypes)
@ -114,7 +114,9 @@ public static partial class McpServerBuilderExtensions
}
if (toolTypes.Count == 0)
{
throw new ArgumentException("No types with marked methods found in the assembly.", nameof(assembly));
}
return WithTools(builder, [.. toolTypes]);
}