-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
78 lines (62 loc) · 2.3 KB
/
Program.cs
File metadata and controls
78 lines (62 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
using System;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using SandboxModelContextProtocol.Server.Services;
using SandboxModelContextProtocol.Server.Services.Interfaces;
using SandboxModelContextProtocol.Server.Services.Models;
using Microsoft.Extensions.Configuration;
namespace SandboxModelContextProtocol.Server;
public class Program
{
public static async Task Main( string[] args )
{
var builder = WebApplication.CreateBuilder( args );
builder.WebHost.UseUrls( "http://0.0.0.0:8080" );
// Configure logging for HTTP transport
builder.Logging.AddConsole();
// Configure WebSocket options
builder.Services.Configure<Services.Models.WebSocketOptions>(
builder.Configuration.GetSection( "WebSocket" )
);
// Register the command service
builder.Services.AddSingleton<IToolService, ToolService>();
// Register the resource service
builder.Services.AddSingleton<IResourceService, ResourceService>();
// Configure MCP Server with HTTP transport, tools and resources from assembly
builder.Services
.AddMcpServer()
.WithHttpTransport()
.WithToolsFromAssembly()
.WithResourcesFromAssembly();
// Register WebSocket service as singleton (no longer a hosted service)
builder.Services.AddSingleton<WebSocketService>();
builder.Services.AddSingleton<IWebSocketService>( provider => provider.GetRequiredService<WebSocketService>() );
var app = builder.Build();
// Enable WebSocket support
app.UseWebSockets();
// Map MCP endpoints for HTTP transport
app.MapMcp();
// Add health check endpoint for Docker
app.MapGet( "/health", () => Results.Ok( new { status = "healthy", timestamp = DateTime.UtcNow } ) );
// Configure WebSocket endpoint
var webSocketService = app.Services.GetRequiredService<WebSocketService>();
app.Map( "/ws", async context =>
{
if ( context.WebSockets.IsWebSocketRequest )
{
using var webSocket = await context.WebSockets.AcceptWebSocketAsync();
await webSocketService.HandleWebSocketConnection( webSocket, context.RequestAborted );
}
else
{
context.Response.StatusCode = 400;
}
} );
await app.RunAsync();
}
}