Add EverythingServer sample (#151)
This commit is contained in:
parent
d21d933876
commit
01410df5ad
1 changed files with 620 additions and 1 deletions
43
samples/EverythingServer/LoggingUpdateMessageSender.cs
Normal file
43
samples/EverythingServer/LoggingUpdateMessageSender.cs
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using ModelContextProtocol;
|
||||
using ModelContextProtocol.Protocol.Types;
|
||||
using ModelContextProtocol.Server;
|
||||
|
||||
namespace EverythingServer;
|
||||
|
||||
public class LoggingUpdateMessageSender(IMcpServer server, Func<LoggingLevel> getMinLevel) : BackgroundService
|
||||
{
|
||||
readonly Dictionary<LoggingLevel, string> _loggingLevelMap = new()
|
||||
{
|
||||
{ LoggingLevel.Debug, "Debug-level message" },
|
||||
{ LoggingLevel.Info, "Info-level message" },
|
||||
{ LoggingLevel.Notice, "Notice-level message" },
|
||||
{ LoggingLevel.Warning, "Warning-level message" },
|
||||
{ LoggingLevel.Error, "Error-level message" },
|
||||
{ LoggingLevel.Critical, "Critical-level message" },
|
||||
{ LoggingLevel.Alert, "Alert-level message" },
|
||||
{ LoggingLevel.Emergency, "Emergency-level message" }
|
||||
};
|
||||
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
while (!stoppingToken.IsCancellationRequested)
|
||||
{
|
||||
var newLevel = (LoggingLevel)Random.Shared.Next(_loggingLevelMap.Count);
|
||||
|
||||
var message = new
|
||||
{
|
||||
Level = newLevel.ToString().ToLower(),
|
||||
Data = _loggingLevelMap[newLevel],
|
||||
};
|
||||
|
||||
if (newLevel > getMinLevel())
|
||||
{
|
||||
await server.SendNotificationAsync("notifications/message", message, cancellationToken: stoppingToken);
|
||||
}
|
||||
|
||||
await Task.Delay(15000, stoppingToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue