Improve how to host McpServer (#30)

This commit is contained in:
Toni Wenzel 2025-03-05 21:15:22 +01:00 • committed by GitHub
commit 257a97a659
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -0,0 +1,37 @@
using McpDotNet;
using Microsoft.Extensions.Hosting;
using Serilog;
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Verbose() // Capture all log levels
.WriteTo.File(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "logs", "TestServer_.log"),
rollingInterval: RollingInterval.Day,
outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine}{Exception}")
.WriteTo.Debug()
.WriteTo.Console()
.CreateLogger();
try
{
Log.Information("Starting server...");
var builder = Host.CreateApplicationBuilder(args);
builder.Services.AddSerilog();
builder.Services.AddMcpServer()
.WithStdioServerTransport()
.WithCallToolHandler(async (r, ct) => new McpDotNet.Protocol.Types.CallToolResponse());
var app = builder.Build();
await app.RunAsync();
return 0;
}
catch (Exception ex)
{
Log.Fatal(ex, "Host terminated unexpectedly");
return 1;
}
finally
{
await Log.CloseAndFlushAsync();
}