@@ -39,6 +39,7 @@ private static async Task Main(string[] args)
3939 Tools = ConfigureTools ( ) ,
4040 Resources = ConfigureResources ( ) ,
4141 Prompts = ConfigurePrompts ( ) ,
42+ Logging = ConfigureLogging ( )
4243 } ,
4344 ProtocolVersion = "2024-11-05" ,
4445 ServerInstructions = "This is a test server with only stub functionality" ,
@@ -54,10 +55,35 @@ private static async Task Main(string[] args)
5455
5556 Log . Logger . Information ( "Server started." ) ;
5657
58+ // everything server sends random log level messages every 15 seconds
59+ int loggingSeconds = 0 ;
60+ Random random = Random . Shared ;
61+ var loggingLevels = Enum . GetValues < LoggingLevel > ( ) . ToList ( ) ;
62+
5763 // Run until process is stopped by the client (parent process)
5864 while ( true )
5965 {
6066 await Task . Delay ( 5000 ) ;
67+ if ( _minimumLoggingLevel is not null )
68+ {
69+ loggingSeconds += 5 ;
70+
71+ // Send random log messages every 15 seconds
72+ if ( loggingSeconds >= 15 )
73+ {
74+ var logLevelIndex = random . Next ( loggingLevels . Count ) ;
75+ var logLevel = loggingLevels [ logLevelIndex ] ;
76+ await server . SendMessageAsync ( new JsonRpcNotification ( )
77+ {
78+ Method = NotificationMethods . LoggingMessageNotification ,
79+ Params = new LoggingMessageNotificationParams
80+ {
81+ Level = logLevel ,
82+ Data = JsonSerializer . Deserialize < JsonElement > ( "\" Random log message\" " )
83+ }
84+ } ) ;
85+ }
86+ }
6187
6288 // Snapshot the subscribed resources, rather than locking while sending notifications
6389 List < string > resources ;
@@ -266,6 +292,26 @@ private static PromptsCapability ConfigurePrompts()
266292 } ;
267293 }
268294
295+ private static LoggingLevel ? _minimumLoggingLevel = null ;
296+
297+ private static LoggingCapability ConfigureLogging ( )
298+ {
299+ return new ( )
300+ {
301+ SetLoggingLevelHandler = ( request , cancellationToken ) =>
302+ {
303+ if ( request . Params ? . Level is null )
304+ {
305+ throw new McpServerException ( "Missing required argument 'level'" ) ;
306+ }
307+
308+ _minimumLoggingLevel = request . Params . Level ;
309+
310+ return Task . FromResult ( new EmptyResult ( ) ) ;
311+ }
312+ } ;
313+ }
314+
269315 private static readonly HashSet < string > _subscribedResources = new ( ) ;
270316 private static readonly object _subscribedResourcesLock = new ( ) ;
271317
0 commit comments