|
| 1 | +--- |
| 2 | +title: Logging |
| 3 | +author: mikekistler |
| 4 | +description: How to use the logging feature in the MCP C# SDK. |
| 5 | +uid: logging |
| 6 | +--- |
| 7 | + |
| 8 | +MCP servers may expose log messages to clients through the [Logging utility]. |
| 9 | + |
| 10 | +[Logging utility]: https://modelcontextprotocol.io/specification/2025-06-18/server/utilities/logging |
| 11 | + |
| 12 | +This document describes how to implement logging in MCP servers and how clients can consume log messages. |
| 13 | + |
| 14 | +### Logging Levels |
| 15 | + |
| 16 | +MCP uses the logging levels defined in [RFC 5424](https://tools.ietf.org/html/rfc5424). |
| 17 | + |
| 18 | +The MCP C# SDK uses the standard .NET [ILogger] and [ILoggerProvider] abstractions, which support a slightly |
| 19 | +different set of logging levels. Here's the levels and how they map to standard .NET logging levels. |
| 20 | + |
| 21 | +| Level | .NET | Description | Example Use Case | |
| 22 | +|-----------|------|-----------------------------------|------------------------------| |
| 23 | +| debug | ✓ | Detailed debugging information | Function entry/exit points | |
| 24 | +| info | ✓ | General informational messages | Operation progress updates | |
| 25 | +| notice | | Normal but significant events | Configuration changes | |
| 26 | +| warning | ✓ | Warning conditions | Deprecated feature usage | |
| 27 | +| error | ✓ | Error conditions | Operation failures | |
| 28 | +| critical | ✓ | Critical conditions | System component failures | |
| 29 | +| alert | | Action must be taken immediately | Data corruption detected | |
| 30 | +| emergency | | System is unusable | | |
| 31 | + |
| 32 | +**Note:** .NET's [ILogger] also supports a `Trace` level (more verbose than Debug) log level. |
| 33 | +As there is no equivalent level in the MCP logging levels, Trace level logs messages are silently |
| 34 | +dropped when sending messages to the client. |
| 35 | + |
| 36 | +[ILogger]: https://learn.microsoft.com/dotnet/api/microsoft.extensions.logging.ilogger |
| 37 | +[ILoggerProvider]: https://learn.microsoft.com/dotnet/api/microsoft.extensions.logging.iloggerprovider |
| 38 | + |
| 39 | +### Server configuration and logging |
| 40 | + |
| 41 | +MCP servers that implement the Logging utility must declare this in the capabilities sent in the |
| 42 | +[Initialization] phase at the beginning of the MCP session. |
| 43 | + |
| 44 | +[Initialization]: https://modelcontextprotocol.io/specification/2025-06-18/basic/lifecycle#initialization |
| 45 | + |
| 46 | +Servers built with the C# SDK always declare the logging capability. The C# SDK provides an extension method |
| 47 | +[WithSetLoggingLevelHandler] on [IMcpServerBuilder] to allow the server to perform any special logic it wants to perform |
| 48 | +when a client sets the logging level. However, the SDK already takes care of setting the [LoggingLevel] |
| 49 | +in the [IMcpServer], so most servers will not need to implement this. |
| 50 | + |
| 51 | +[IMcpServer]: https://modelcontextprotocol.github.io/csharp-sdk/api/ModelContextProtocol.Server.IMcpServer.html |
| 52 | +[IMcpServerBuilder]: https://modelcontextprotocol.github.io/csharp-sdk/api/Microsoft.Extensions.DependencyInjection.IMcpServerBuilder.html |
| 53 | +[WithSetLoggingLevelHandler]: https://modelcontextprotocol.github.io/csharp-sdk/api/Microsoft.Extensions.DependencyInjection.McpServerBuilderExtensions.html#Microsoft_Extensions_DependencyInjection_McpServerBuilderExtensions_WithSetLoggingLevelHandler_Microsoft_Extensions_DependencyInjection_IMcpServerBuilder_System_Func_ModelContextProtocol_Server_RequestContext_ModelContextProtocol_Protocol_SetLevelRequestParams__System_Threading_CancellationToken_System_Threading_Tasks_ValueTask_ModelContextProtocol_Protocol_EmptyResult___ |
| 54 | +[LoggingLevel]: https://modelcontextprotocol.github.io/csharp-sdk/api/ModelContextProtocol.Server.IMcpServer.html#ModelContextProtocol_Server_IMcpServer_LoggingLevel |
| 55 | + |
| 56 | +MCP Servers using the MCP C# SDK can obtain an [ILoggerProvider] from the IMcpServer [AsClientLoggerProvider] extension method, |
| 57 | +and from that can create an [ILogger] instance for logging messages that should be sent to the MCP client. |
| 58 | + |
| 59 | +[!code-csharp[](samples/server/Tools/LoggingTools.cs?name=snippet_LoggingConfiguration)] |
| 60 | + |
| 61 | +[ILoggerProvider]: https://learn.microsoft.com/dotnet/api/microsoft.extensions.logging.iloggerprovider |
| 62 | +[AsClientLoggerProvider]: https://modelcontextprotocol.github.io/csharp-sdk/api/ModelContextProtocol.Server.McpServerExtensions.html#ModelContextProtocol_Server_McpServerExtensions_AsClientLoggerProvider_ModelContextProtocol_Server_IMcpServer_ |
| 63 | +[ILogger]: https://learn.microsoft.com/dotnet/api/microsoft.extensions.logging.ilogger |
| 64 | + |
| 65 | +### Client support for logging |
| 66 | + |
| 67 | +Clients that wish to receive log messages from the server must first check if logging is supported. |
| 68 | +This is done by checking the [Logging] property of the [ServerCapabilities] field of [IMcpClient]. |
| 69 | + |
| 70 | +[IMcpClient]: https://modelcontextprotocol.github.io/csharp-sdk/api/ModelContextProtocol.Client.IMcpClient.html |
| 71 | +[ServerCapabilities]: https://modelcontextprotocol.github.io/csharp-sdk/api/ModelContextProtocol.Client.IMcpClient.html#ModelContextProtocol_Client_IMcpClient_ServerCapabilities |
| 72 | +[Logging]: https://modelcontextprotocol.github.io/csharp-sdk/api/ModelContextProtocol.Protocol.ServerCapabilities.html#ModelContextProtocol_Protocol_ServerCapabilities_Logging |
| 73 | + |
| 74 | +[!code-csharp[](samples/client/Program.cs?name=snippet_LoggingCapabilities)] |
| 75 | + |
| 76 | +If the server supports logging, the client can set the level of log messages it wishes to receive with |
| 77 | +the [SetLoggingLevel] method on [IMcpClient]. The `loggingLevel` specified here is an MCP logging level. |
| 78 | +See the [Logging Levels](#logging-levels) section above for the mapping between MCP and .NET logging levels. |
| 79 | + |
| 80 | +[SetLoggingLevel]: https://modelcontextprotocol.github.io/csharp-sdk/api/ModelContextProtocol.Client.McpClientExtensions.html#ModelContextProtocol_Client_McpClientExtensions_SetLoggingLevel_ModelContextProtocol_Client_IMcpClient_Microsoft_Extensions_Logging_LogLevel_System_Threading_CancellationToken_ |
| 81 | + |
| 82 | +[!code-csharp[](samples/client/Program.cs?name=snippet_LoggingLevel)] |
| 83 | + |
| 84 | +Lastly, the client must configure a notification handler for [NotificationMethods.LoggingMessageNotification] notifications. |
| 85 | + |
| 86 | +[NotificationMethods.LoggingMessageNotification]: https://modelcontextprotocol.github.io/csharp-sdk/api/ModelContextProtocol.Protocol.NotificationMethods.html#ModelContextProtocol_Protocol_NotificationMethods_LoggingMessageNotification |
| 87 | + |
| 88 | +[!code-csharp[](samples/client/Program.cs?name=snippet_LoggingHandler)] |
0 commit comments