Skip to content

[Feature Request] Add Delegate for Logging Request, Response, and Errors in MCP Server #359

Closed as not planned
@ramonduraes

Description

@ramonduraes

Summary

I would like to suggest adding support for a single delegate in the ModelContextProtocol C# SDK to allow developers to capture structured logs of incoming requests, outgoing responses, and errors occurring during Tool execution.

Motivation

Today, the MCP Server processes and handles Tool calls internally, but there is no built-in way to observe:

  • The exact JSON received in the request.
  • The exact JSON sent in the response.
  • Any exceptions that occurred during the Tool execution.

Having a logging delegate would provide much better visibility for developers, facilitating debugging, observability, auditing, and integration with external monitoring systems.

Proposed Solution

Introduce a single delegate that would be triggered in three situations:

  • When a request is received.
  • When a response is sent.
  • When an error occurs.

Suggested delegate signature

public delegate void McpLogHandler(McpLogContext context);

public class McpLogContext
{
    public McpStatus Status { get; set; } // RequestReceived, ResponseSent, or ErrorOccurred
    public string Json { get; set; } // The raw JSON for request or response
    public Exception? Exception { get; set; } // In case of errors
    public string? ToolName { get; set; } // Optional, for Tool-related logs
}

public enum McpStatus
{
    RequestReceived,
    ResponseSent,
    ErrorOccurred
}

Example of usage when initializing the MCP server

builder.Services.AddMcpServer(options =>
{
    options.OnLog = (context) =>
    {
        Console.WriteLine($"[{context.Status}]");

        if (!string.IsNullOrWhiteSpace(context.ToolName))
            Console.WriteLine($"Tool: {context.ToolName}");

        Console.WriteLine("Payload:");
        Console.WriteLine(context.Json);

        if (context.Exception != null)
        {
            Console.WriteLine("Exception:");
            Console.WriteLine(context.Exception);
        }
    };
});

Benefits

  • Full transparency of server operations.
  • Easier debugging of Tool contracts and input/output.
  • Integration with external log systems like Application Insights, OpenTelemetry, Datadog, etc.
  • Ability to audit and trace individual operations without modifying core server logic.
  • Keeps the MCP Server minimalistic for those who do not require additional logging (opt-in feature).

Final Note

Thank you very much for the great work on the MCP C# SDK.
This small addition would greatly improve the developer experience without introducing complexity to the existing usage.
It would make the SDK even more attractive and production-ready for real-world scenarios involving observability and auditing requirements.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions