Files
Alchegos.MCP/Program.cs
2025-05-08 01:14:41 +01:00

50 lines
1.1 KiB
C#

using Alchegos.MCP.Middleware;
var builder = WebApplication.CreateBuilder(args);
const int BIND_PORT = 5050;
builder.Services
.AddMcpServer()
.WithHttpTransport()
.WithToolsFromAssembly();
builder.WebHost.ConfigureKestrel(options => options.ListenAnyIP(BIND_PORT));
var app = builder.Build();
app.UseRequestBodyLogging();
app.Use(async (context, next) =>
{
var expectedToken = Environment.GetEnvironmentVariable("MCP_API_KEY");
if (string.IsNullOrEmpty(expectedToken))
{
context.Response.StatusCode = 500;
await context.Response.WriteAsync("API Key not configured");
return;
}
if (!context.Request.Headers.TryGetValue("X-Api-Key", out var apiKey) ||
string.IsNullOrEmpty(apiKey))
{
context.Response.StatusCode = 401;
await context.Response.WriteAsync("Missing API Key");
return;
}
if (apiKey != expectedToken)
{
context.Response.StatusCode = 401;
await context.Response.WriteAsync("Invalid API Key");
return;
}
await next();
});
app.MapMcp();
app.Run();