Skip to content

Commit dd1ffe5

Browse files
committed
Converting to proper background services
1 parent 6ba94be commit dd1ffe5

3 files changed

Lines changed: 22 additions & 27 deletions

File tree

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
using Microsoft.Extensions.DependencyInjection;
22
using Microsoft.Extensions.Hosting;
33
using ModelContextProtocol;
4-
using ModelContextProtocol.Protocol.Messages;
54
using ModelContextProtocol.Protocol.Types;
65
using ModelContextProtocol.Server;
76

87
namespace EverythingServer;
98

10-
public class LoggingUpdateMessageSender(IMcpServer server) : IHostedService
9+
public class LoggingUpdateMessageSender(IMcpServer server) : BackgroundService
1110
{
1211
readonly Dictionary<LoggingLevel, string> _loggingLevelMap = new()
1312
{
@@ -21,11 +20,11 @@ public class LoggingUpdateMessageSender(IMcpServer server) : IHostedService
2120
{ LoggingLevel.Emergency, "Emergency-level message" }
2221
};
2322

24-
public async Task StartAsync(CancellationToken cancellationToken)
23+
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
2524
{
2625
var currentLevel = server.Services!.GetRequiredService<Func<LoggingLevel>>();
2726

28-
while (!cancellationToken.IsCancellationRequested)
27+
while (!stoppingToken.IsCancellationRequested)
2928
{
3029
var newLevel = (LoggingLevel)Random.Shared.Next(_loggingLevelMap.Count);
3130

@@ -37,15 +36,10 @@ public async Task StartAsync(CancellationToken cancellationToken)
3736

3837
if (newLevel > currentLevel())
3938
{
40-
await server.SendNotificationAsync("notifications/message", message, cancellationToken: cancellationToken);
39+
await server.SendNotificationAsync("notifications/message", message, cancellationToken: stoppingToken);
4140
}
4241

43-
await Task.Delay(15000, cancellationToken);
42+
await Task.Delay(15000, stoppingToken);
4443
}
4544
}
46-
47-
public Task StopAsync(CancellationToken cancellationToken)
48-
{
49-
return Task.CompletedTask;
50-
}
5145
}

samples/EverythingServer/Program.cs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
1-
using Microsoft.Extensions.Hosting;
2-
using ModelContextProtocol.Protocol.Types;
3-
using EverythingServer;
4-
using ModelContextProtocol.Server;
5-
using Microsoft.Extensions.AI;
6-
using Microsoft.Extensions.DependencyInjection;
1+
using EverythingServer;
72
using EverythingServer.Prompts;
83
using EverythingServer.Tools;
4+
using Microsoft.Extensions.AI;
5+
using Microsoft.Extensions.DependencyInjection;
6+
using Microsoft.Extensions.Hosting;
7+
using Microsoft.Extensions.Logging;
98
using ModelContextProtocol;
9+
using ModelContextProtocol.Protocol.Types;
10+
using ModelContextProtocol.Server;
1011

1112
var builder = Host.CreateApplicationBuilder(args);
13+
builder.Logging.AddConsole(consoleLogOptions =>
14+
{
15+
// Configure all logs to go to stderr
16+
consoleLogOptions.LogToStandardErrorThreshold = LogLevel.Trace;
17+
});
1218

1319
HashSet<string> subscriptions = [];
1420
var _minimumLoggingLevel = LoggingLevel.Debug;

samples/EverythingServer/SubscriptionMessageSender.cs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,22 @@
22
using ModelContextProtocol;
33
using ModelContextProtocol.Server;
44

5-
internal class SubscriptionMessageSender(IMcpServer server, HashSet<string> subscriptions) : IHostedService
5+
internal class SubscriptionMessageSender(IMcpServer server, HashSet<string> subscriptions) : BackgroundService
66
{
7-
public async Task StartAsync(CancellationToken cancellationToken)
7+
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
88
{
9-
while (!cancellationToken.IsCancellationRequested)
9+
while (!stoppingToken.IsCancellationRequested)
1010
{
1111
foreach (var uri in subscriptions)
1212
{
1313
await server.SendNotificationAsync("notifications/resource/updated",
1414
new
1515
{
1616
Uri = uri,
17-
}, cancellationToken: cancellationToken);
17+
}, cancellationToken: stoppingToken);
1818
}
1919

20-
await Task.Delay(5000, cancellationToken);
20+
await Task.Delay(5000, stoppingToken);
2121
}
2222
}
23-
24-
public Task StopAsync(CancellationToken cancellationToken)
25-
{
26-
return Task.CompletedTask;
27-
}
2823
}

0 commit comments

Comments
 (0)