Skip to content

Commit 6e5066b

Browse files
committed
Stop the host when the single session server service finishes
1 parent 848c9df commit 6e5066b

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

src/ModelContextProtocol/Hosting/SingleSessionMcpServerHostedService.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,18 @@ namespace ModelContextProtocol.Hosting;
66
/// <summary>
77
/// Hosted service for a single-session (e.g. stdio) MCP server.
88
/// </summary>
9-
internal sealed class SingleSessionMcpServerHostedService(IMcpServer session) : BackgroundService
9+
internal sealed class SingleSessionMcpServerHostedService(IMcpServer session, IHostApplicationLifetime lifetime) : BackgroundService
1010
{
1111
/// <inheritdoc />
12-
protected override Task ExecuteAsync(CancellationToken stoppingToken) => session.RunAsync(stoppingToken);
12+
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
13+
{
14+
try
15+
{
16+
await session.RunAsync(stoppingToken);
17+
}
18+
finally
19+
{
20+
lifetime.StopApplication();
21+
}
22+
}
1323
}

0 commit comments

Comments
 (0)