44using ModelContextProtocol . Protocol . Types ;
55using ModelContextProtocol . Server ;
66using Serilog ;
7+ using System . Runtime . CompilerServices ;
78using System . Text ;
89using System . Text . Json ;
910
@@ -39,6 +40,7 @@ private static async Task Main(string[] args)
3940 Tools = ConfigureTools ( ) ,
4041 Resources = ConfigureResources ( ) ,
4142 Prompts = ConfigurePrompts ( ) ,
43+ Logging = ConfigureLogging ( )
4244 } ,
4345 ProtocolVersion = "2024-11-05" ,
4446 ServerInstructions = "This is a test server with only stub functionality" ,
@@ -54,10 +56,35 @@ private static async Task Main(string[] args)
5456
5557 Log . Logger . Information ( "Server started." ) ;
5658
59+ // everything server sends random log level messages every 15 seconds
60+ int loggingSeconds = 0 ;
61+ Random random = Random . Shared ;
62+ var loggingLevels = Enum . GetValues < LoggingLevel > ( ) . ToList ( ) ;
63+
5764 // Run until process is stopped by the client (parent process)
5865 while ( true )
5966 {
6067 await Task . Delay ( 5000 ) ;
68+ if ( _minimumLoggingLevel is not null )
69+ {
70+ loggingSeconds += 5 ;
71+
72+ // Send random log messages every 15 seconds
73+ if ( loggingSeconds >= 15 )
74+ {
75+ var logLevelIndex = random . Next ( loggingLevels . Count ) ;
76+ var logLevel = loggingLevels [ logLevelIndex ] ;
77+ await server . SendMessageAsync ( new JsonRpcNotification ( )
78+ {
79+ Method = NotificationMethods . LoggingMessageNotification ,
80+ Params = new LoggingMessageNotificationParams
81+ {
82+ Level = logLevel ,
83+ Data = JsonSerializer . Deserialize < JsonElement > ( "\" Random log message\" " )
84+ }
85+ } ) ;
86+ }
87+ }
6188
6289 // Snapshot the subscribed resources, rather than locking while sending notifications
6390 List < string > resources ;
@@ -266,6 +293,26 @@ private static PromptsCapability ConfigurePrompts()
266293 } ;
267294 }
268295
296+ private static LoggingLevel ? _minimumLoggingLevel = null ;
297+
298+ private static LoggingCapability ConfigureLogging ( )
299+ {
300+ return new ( )
301+ {
302+ SetLoggingLevelHandler = ( request , cancellationToken ) =>
303+ {
304+ if ( request . Params ? . Level is null )
305+ {
306+ throw new McpServerException ( "Missing required argument 'level'" ) ;
307+ }
308+
309+ _minimumLoggingLevel = request . Params . Level ;
310+
311+ return Task . FromResult ( new EmptyResult ( ) ) ;
312+ }
313+ } ;
314+ }
315+
269316 private static readonly HashSet < string > _subscribedResources = new ( ) ;
270317 private static readonly object _subscribedResourcesLock = new ( ) ;
271318
0 commit comments