Skip to content

Add MCP notifications/message for log streaming to clients#3484

Open
anushakolan wants to merge 6 commits intomainfrom
dev/anushakolan/mcp-notifications
Open

Add MCP notifications/message for log streaming to clients#3484
anushakolan wants to merge 6 commits intomainfrom
dev/anushakolan/mcp-notifications

Conversation

@anushakolan
Copy link
Copy Markdown
Contributor

Why make this change?

Enables MCP clients (like MCP Inspector, Claude Desktop, VS Code Copilot) to receive real-time log output via MCP notifications/message.

Related: #3274 (depends on PR #3419)

What is this change?

When logging/setLevel is called with a level other than "none", logs are sent to MCP clients as JSON-RPC notifications:

{
  "jsonrpc": "2.0",
  "method": "notifications/message",
  "params": {
    "level": "info",
    "logger": "Azure.DataApiBuilder.Service.Startup",
    "data": "Starting Data API builder..."
  }
}

New files:

  • McpLogNotificationWriter.cs - Writes logs as MCP notifications to stdout
  • McpLogger.cs / McpLoggerProvider.cs - ILogger implementation for .NET logging pipeline
  • McpLogNotificationTests.cs - Unit tests (8 tests)

Modified files:

  • Program.cs - Registers McpNotificationWriter and McpLoggerProvider for MCP mode
  • McpStdioServer.cs - Enables notifications when logging/setLevel is called

How was this tested?

  • Unit tests: 8 tests covering level mapping, enable/disable, JSON format
  • Manual testing with MCP Inspector: verified notifications appear when logging/setLevel is sent

Note

This PR targets dev/anushakolan/set-log-level (PR #3419) as it depends on the logging/setLevel implementation.

Comment thread src/Service/Program.cs
// For MCP stdio mode, add the MCP logger provider to send logs as notifications
if (runMcpStdio)
{
logging.AddProvider(new McpLoggerProvider(McpNotificationWriter, LogLevelProvider.ShouldLog));
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how do you ensure other LogLevelProviders dont get added when in stdio mode?


var notification = new
{
jsonrpc = "2.0",
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

define a constant that represents 2.0 protocol, so it can be changed in one shot.

/// </remarks>
private static string ConvertToMcpLevel(LogLevel logLevel)
{
return logLevel switch
Copy link
Copy Markdown
Collaborator

@Aniruddh25 Aniruddh25 May 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesnt McpStdioServer or somewhere else we already define such conversion levels? Can we reuse that?

}

[TestMethod]
public void McpLogger_BeginScope_ReturnsNonNullDisposable()
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a trivial test can be skipped. since we dont even use the scope.

Aniruddh25
Aniruddh25 previously approved these changes May 1, 2026
Copy link
Copy Markdown
Collaborator

@Aniruddh25 Aniruddh25 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left some suggestions for code reuse.

Base automatically changed from dev/anushakolan/set-log-level to main May 2, 2026 00:28
@anushakolan anushakolan dismissed Aniruddh25’s stale review May 2, 2026 00:28

The base branch was changed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants