Make client config optional to McpClientFactory.CreateAsync (#65)
* Make client config optional to McpClientFactory.CreateAsync Unless you're specifying capabilities (sampling/rooting) or need a specific client name/version, the client information can be inferred from the assembly, simplifying the getting-started experience. * Cache default options * Update src/ModelContextProtocol/Client/McpClientFactory.cs Co-authored-by: Eirik Tsarpalis <eirik.tsarpalis@gmail.com> --------- Co-authored-by: Eirik Tsarpalis <eirik.tsarpalis@gmail.com>
This commit is contained in:
parent
36d5019de0
commit
729e688b25
1 changed files with 50 additions and 23 deletions
|
|
@ -6,15 +6,36 @@ using ModelContextProtocol.Protocol.Transport;
|
|||
using ModelContextProtocol.Utils;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
using System.Reflection;
|
||||
|
||||
namespace ModelContextProtocol.Client;
|
||||
|
||||
/// <summary>Provides factory methods for creating MCP clients.</summary>
|
||||
public static class McpClientFactory
|
||||
{
|
||||
/// <summary>Default client options to use when none are supplied.</summary>
|
||||
private static readonly McpClientOptions s_defaultClientOptions = CreateDefaultClientOptions();
|
||||
|
||||
/// <summary>Creates default client options to use when no options are supplied.</summary>
|
||||
private static McpClientOptions CreateDefaultClientOptions()
|
||||
{
|
||||
var asmName = (Assembly.GetEntryAssembly() ?? Assembly.GetExecutingAssembly()).GetName();
|
||||
return new()
|
||||
{
|
||||
ClientInfo = new()
|
||||
{
|
||||
Name = asmName.Name ?? "McpClient",
|
||||
Version = asmName.Version?.ToString() ?? "1.0.0",
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>Creates an <see cref="IMcpClient"/>, connecting it to the specified server.</summary>
|
||||
/// <param name="serverConfig">Configuration for the target server to which the client should connect.</param>
|
||||
/// <param name="clientOptions">A client configuration object which specifies client capabilities and protocol version.</param>
|
||||
/// <param name="clientOptions">
|
||||
/// A client configuration object which specifies client capabilities and protocol version.
|
||||
/// If <see langword="null"/>, details based on the current process will be employed.
|
||||
/// </param>
|
||||
/// <param name="createTransportFunc">An optional factory method which returns transport implementations based on a server configuration.</param>
|
||||
/// <param name="loggerFactory">A logger factory for creating loggers for clients.</param>
|
||||
/// <param name="cancellationToken">A token to cancel the operation.</param>
|
||||
|
|
@ -25,14 +46,14 @@ public static class McpClientFactory
|
|||
/// <exception cref="InvalidOperationException"><paramref name="createTransportFunc"/> returns an invalid transport.</exception>
|
||||
public static async Task<IMcpClient> CreateAsync(
|
||||
McpServerConfig serverConfig,
|
||||
McpClientOptions clientOptions,
|
||||
McpClientOptions? clientOptions = null,
|
||||
Func<McpServerConfig, ILoggerFactory?, IClientTransport>? createTransportFunc = null,
|
||||
ILoggerFactory? loggerFactory = null,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
Throw.IfNull(serverConfig);
|
||||
Throw.IfNull(clientOptions);
|
||||
|
||||
clientOptions ??= s_defaultClientOptions;
|
||||
createTransportFunc ??= CreateTransport;
|
||||
|
||||
string endpointName = $"Client ({serverConfig.Id}: {serverConfig.Name})";
|
||||
|
|
|
|||
Loading…
Reference in a new issue