diff --git a/src/Smith/App.cs b/src/Smith/App.cs index 1d9fd47..6ad8eb2 100644 --- a/src/Smith/App.cs +++ b/src/Smith/App.cs @@ -1,5 +1,8 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; +using ModelContextProtocol.Server; namespace Smith; @@ -8,6 +11,30 @@ namespace Smith; /// public static class App { + class AppBuilder(IServiceCollection services) + { + public IServiceCollection Services => services; + } + + class AppServiceFactory(HostApplicationBuilder host) : IServiceProviderFactory + { + public AppBuilder CreateBuilder(IServiceCollection services) => new(services); + + public IServiceProvider CreateServiceProvider(AppBuilder builder) + { + // If MCP server was registered with AddMcpServer, then tune logging to prevent + // stdio noise from breaking the protocol. + if (host.Services.AsEnumerable().Any(x => x.ServiceType == typeof(IConfigureOptions))) + { + host.Logging.AddConsole(consoleLogOptions => + { + consoleLogOptions.LogToStandardErrorThreshold = LogLevel.Trace; + }); + } + return builder.Services.BuildServiceProvider(); + } + } + /// /// Invokes the with additional pre-configured defaults. /// @@ -19,7 +46,12 @@ public static class App /// /// The command line args. /// The initialized . - public static HostApplicationBuilder CreateBuilder(string[]? args) => Host.CreateApplicationBuilder(args); + public static HostApplicationBuilder CreateBuilder(string[]? args) + { + var host = Host.CreateApplicationBuilder(args); + host.ConfigureContainer(new AppServiceFactory(host)); + return host; + } /// /// Builds the host app and registers the provided function as a hosted service to be