Remove McpServerConfig and have McpClientFactory accept IClientTransport instances directly. (#230)
* Remove McpServerConfig and have McpClientFactory accept IClientTransport instances directly. * Address feedback and use lists when representing stdio arguments. * Use "Name" for transport descriptors and "EndpointName" for MCP clients and servers. * Update code samples. * Fix source file naming
This commit is contained in:
parent
8bd9ee81c0
commit
3e21f3594a
2 changed files with 315 additions and 527 deletions
|
|
@ -33,9 +33,8 @@ internal sealed class McpClient : McpEndpoint, IMcpClient
|
|||
/// </summary>
|
||||
/// <param name="clientTransport">The transport to use for communication with the server.</param>
|
||||
/// <param name="options">Options for the client, defining protocol version and capabilities.</param>
|
||||
/// <param name="serverConfig">The server configuration.</param>
|
||||
/// <param name="loggerFactory">The logger factory.</param>
|
||||
public McpClient(IClientTransport clientTransport, McpClientOptions? options, McpServerConfig serverConfig, ILoggerFactory? loggerFactory)
|
||||
public McpClient(IClientTransport clientTransport, McpClientOptions? options, ILoggerFactory? loggerFactory)
|
||||
: base(loggerFactory)
|
||||
{
|
||||
options ??= new();
|
||||
|
|
@ -43,7 +42,7 @@ internal sealed class McpClient : McpEndpoint, IMcpClient
|
|||
_clientTransport = clientTransport;
|
||||
_options = options;
|
||||
|
||||
EndpointName = $"Client ({serverConfig.Id}: {serverConfig.Name})";
|
||||
EndpointName = clientTransport.Name;
|
||||
|
||||
if (options.Capabilities is { } capabilities)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -12,128 +12,39 @@ namespace ModelContextProtocol.Client;
|
|||
public static class McpClientFactory
|
||||
{
|
||||
/// <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="clientTransport">The transport instance used to communicate with the server.</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">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
|
||||
/// <returns>An <see cref="IMcpClient"/> that's connected to the specified server.</returns>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="serverConfig"/> is <see langword="null"/>.</exception>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="clientTransport"/> is <see langword="null"/>.</exception>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="clientOptions"/> is <see langword="null"/>.</exception>
|
||||
/// <exception cref="ArgumentException"><paramref name="serverConfig"/> contains invalid information.</exception>
|
||||
/// <exception cref="InvalidOperationException"><paramref name="createTransportFunc"/> returns an invalid transport.</exception>
|
||||
public static async Task<IMcpClient> CreateAsync(
|
||||
McpServerConfig serverConfig,
|
||||
IClientTransport clientTransport,
|
||||
McpClientOptions? clientOptions = null,
|
||||
Func<McpServerConfig, ILoggerFactory?, IClientTransport>? createTransportFunc = null,
|
||||
ILoggerFactory? loggerFactory = null,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
Throw.IfNull(serverConfig);
|
||||
|
||||
createTransportFunc ??= CreateTransport;
|
||||
|
||||
string endpointName = $"Client ({serverConfig.Id}: {serverConfig.Name})";
|
||||
Throw.IfNull(clientTransport);
|
||||
|
||||
string endpointName = clientTransport.Name;
|
||||
var logger = loggerFactory?.CreateLogger(typeof(McpClientFactory)) ?? NullLogger.Instance;
|
||||
logger.CreatingClient(endpointName);
|
||||
|
||||
var transport =
|
||||
createTransportFunc(serverConfig, loggerFactory) ??
|
||||
throw new InvalidOperationException($"{nameof(createTransportFunc)} returned a null transport.");
|
||||
|
||||
McpClient client = new(clientTransport, clientOptions, loggerFactory);
|
||||
try
|
||||
{
|
||||
McpClient client = new(transport, clientOptions, serverConfig, loggerFactory);
|
||||
try
|
||||
{
|
||||
await client.ConnectAsync(cancellationToken).ConfigureAwait(false);
|
||||
logger.ClientCreated(endpointName);
|
||||
return client;
|
||||
}
|
||||
catch
|
||||
{
|
||||
await client.DisposeAsync().ConfigureAwait(false);
|
||||
throw;
|
||||
}
|
||||
await client.ConnectAsync(cancellationToken).ConfigureAwait(false);
|
||||
logger.ClientCreated(endpointName);
|
||||
return client;
|
||||
}
|
||||
catch
|
||||
{
|
||||
if (transport is IAsyncDisposable asyncDisposableTransport)
|
||||
{
|
||||
await asyncDisposableTransport.DisposeAsync().ConfigureAwait(false);
|
||||
}
|
||||
else if (transport is IDisposable disposableTransport)
|
||||
{
|
||||
disposableTransport.Dispose();
|
||||
}
|
||||
await client.DisposeAsync().ConfigureAwait(false);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
private static IClientTransport CreateTransport(McpServerConfig serverConfig, ILoggerFactory? loggerFactory)
|
||||
{
|
||||
if (string.Equals(serverConfig.TransportType, TransportTypes.StdIo, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
string? command = serverConfig.TransportOptions?.GetValueOrDefault("command");
|
||||
if (string.IsNullOrWhiteSpace(command))
|
||||
{
|
||||
command = serverConfig.Location;
|
||||
if (string.IsNullOrWhiteSpace(command))
|
||||
{
|
||||
throw new ArgumentException("Command is required for stdio transport.", nameof(serverConfig));
|
||||
}
|
||||
}
|
||||
|
||||
string? arguments = serverConfig.TransportOptions?.GetValueOrDefault("arguments");
|
||||
|
||||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) &&
|
||||
serverConfig.TransportType.Equals(TransportTypes.StdIo, StringComparison.OrdinalIgnoreCase) &&
|
||||
!string.IsNullOrEmpty(command) &&
|
||||
!string.Equals(Path.GetFileName(command), "cmd.exe", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
// On Windows, for stdio, we need to wrap non-shell commands with cmd.exe /c {command} (usually npx or uvicorn).
|
||||
// The stdio transport will not work correctly if the command is not run in a shell.
|
||||
arguments = string.IsNullOrWhiteSpace(arguments) ?
|
||||
$"/c {command}" :
|
||||
$"/c {command} {arguments}";
|
||||
command = "cmd.exe";
|
||||
}
|
||||
|
||||
return new StdioClientTransport(new StdioClientTransportOptions
|
||||
{
|
||||
Command = command!,
|
||||
Arguments = arguments,
|
||||
WorkingDirectory = serverConfig.TransportOptions?.GetValueOrDefault("workingDirectory"),
|
||||
EnvironmentVariables = serverConfig.TransportOptions?
|
||||
.Where(kv => kv.Key.StartsWith("env:", StringComparison.Ordinal))
|
||||
.ToDictionary(kv => kv.Key.Substring("env:".Length), kv => kv.Value),
|
||||
ShutdownTimeout = TimeSpan.TryParse(serverConfig.TransportOptions?.GetValueOrDefault("shutdownTimeout"), CultureInfo.InvariantCulture, out var timespan) ? timespan : StdioClientTransportOptions.DefaultShutdownTimeout
|
||||
}, serverConfig, loggerFactory);
|
||||
}
|
||||
|
||||
if (string.Equals(serverConfig.TransportType, TransportTypes.Sse, StringComparison.OrdinalIgnoreCase) ||
|
||||
string.Equals(serverConfig.TransportType, "http", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return new SseClientTransport(new SseClientTransportOptions
|
||||
{
|
||||
ConnectionTimeout = TimeSpan.FromSeconds(ParseInt32OrDefault(serverConfig.TransportOptions, "connectionTimeout", 30)),
|
||||
MaxReconnectAttempts = ParseInt32OrDefault(serverConfig.TransportOptions, "maxReconnectAttempts", 3),
|
||||
ReconnectDelay = TimeSpan.FromSeconds(ParseInt32OrDefault(serverConfig.TransportOptions, "reconnectDelay", 5)),
|
||||
AdditionalHeaders = serverConfig.TransportOptions?
|
||||
.Where(kv => kv.Key.StartsWith("header.", StringComparison.Ordinal))
|
||||
.ToDictionary(kv => kv.Key.Substring("header.".Length), kv => kv.Value)
|
||||
}, serverConfig, loggerFactory);
|
||||
|
||||
static int ParseInt32OrDefault(Dictionary<string, string>? options, string key, int defaultValue) =>
|
||||
options?.TryGetValue(key, out var value) is not true ? defaultValue :
|
||||
int.TryParse(value, out var result) ? result :
|
||||
throw new ArgumentException($"Invalid value '{value}' for option '{key}' in transport options.", nameof(serverConfig));
|
||||
}
|
||||
|
||||
throw new ArgumentException($"Unsupported transport type '{serverConfig.TransportType}'.", nameof(serverConfig));
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue