|
| 1 | +// This Source Code Form is subject to the terms of the MIT License. |
| 2 | +// If a copy of the MIT was not distributed with this file, You can obtain one at https://opensource.org/licenses/MIT. |
| 3 | +// Copyright (C) Leszek Pomianowski and OpenAPI Client Contributors. |
| 4 | +// All Rights Reserved. |
| 5 | + |
| 6 | +WebApplicationBuilder builder = WebApplication.CreateBuilder(args); |
| 7 | + |
| 8 | +builder.Logging.AddConsole(consoleLogOptions => |
| 9 | +{ |
| 10 | + // Configure all logs to go to stderr |
| 11 | + consoleLogOptions.LogToStandardErrorThreshold = LogLevel.Trace; |
| 12 | +}); |
| 13 | + |
| 14 | +#if DEBUG |
| 15 | +builder |
| 16 | + .Services.AddOpenTelemetry() |
| 17 | + .WithTracing(b => b.AddSource("*").AddAspNetCoreInstrumentation().AddHttpClientInstrumentation()) |
| 18 | + .WithMetrics(b => b.AddMeter("*").AddAspNetCoreInstrumentation().AddHttpClientInstrumentation()) |
| 19 | + .WithLogging() |
| 20 | + .UseOtlpExporter(); |
| 21 | +#endif |
| 22 | + |
| 23 | +builder.Services.AddTransient<IOpenApiService, OpenApiService>(); |
| 24 | + |
| 25 | +builder.Services.AddHttpClient(); |
| 26 | + |
| 27 | +ServerOptions serverOptions = new(); |
| 28 | +IConfigurationSection section = builder.Configuration.GetSection("Server"); |
| 29 | +section.Bind(serverOptions); |
| 30 | + |
| 31 | +string? mode = Environment.GetEnvironmentVariable("mode") ?? Environment.GetEnvironmentVariable("MODE"); |
| 32 | + |
| 33 | +if (mode?.Contains("both", StringComparison.InvariantCultureIgnoreCase) ?? false) |
| 34 | +{ |
| 35 | + serverOptions.Mode = McpMode.Both; |
| 36 | +} |
| 37 | +else if (mode?.Contains("http", StringComparison.InvariantCultureIgnoreCase) ?? false) |
| 38 | +{ |
| 39 | + serverOptions.Mode = McpMode.Http; |
| 40 | +} |
| 41 | + |
| 42 | +IMcpServerBuilder mcpBuilder = builder.Services.AddMcpServer(); |
| 43 | + |
| 44 | +if (serverOptions.Mode == McpMode.Both) |
| 45 | +{ |
| 46 | + _ = mcpBuilder.WithHttpTransport().WithStdioServerTransport(); |
| 47 | +} |
| 48 | +else if (serverOptions.Mode == McpMode.Stdio) |
| 49 | +{ |
| 50 | + _ = mcpBuilder.WithStdioServerTransport(); |
| 51 | +} |
| 52 | +else |
| 53 | +{ |
| 54 | + _ = mcpBuilder.WithHttpTransport(); |
| 55 | +} |
| 56 | + |
| 57 | +_ = mcpBuilder.WithTools<OpenApiTools>(); |
| 58 | + |
| 59 | +await using WebApplication app = builder.Build(); |
| 60 | + |
| 61 | +if (serverOptions.Mode is McpMode.Http or McpMode.Both) |
| 62 | +{ |
| 63 | + app.MapMcp(); |
| 64 | +} |
| 65 | + |
| 66 | +await app.RunAsync(); |
| 67 | + |
| 68 | +return; |
0 commit comments