Skip to content

Enhance HTTP and MCP session logging #608

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

theojiang25
Copy link

@theojiang25 theojiang25 commented Jul 10, 2025

Add debug and trace level log for:

  • McpSession lifecycle
  • Http request non-sensitive headers

Motivation and Context

The added logs will help with server debugging.

How Has This Been Tested?

Log changes, no tests needed.

Breaking Changes

No

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Checklist

  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

Additional context

"User-Agent",
McpSessionIdHeaderName,
"X-Request-ID",
"X-Correlation-ID"
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure if it should be the responsibility of the MCP SDK to log HTTP headers. Have you considered using ASP.NET Core's HTTP logging middleware instead? https://learn.microsoft.com/en-us/aspnet/core/fundamentals/http-logging/?view=aspnetcore-9.0#enable-http-logging

This allows each application developer to configure exactly what set of request and response headers to log, redact certain headers, log request and response bodies, and a lot more.

Copy link
Author

Choose a reason for hiding this comment

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

Thanks for the pointer, that makes a lot of sense. Removed this change.

@@ -774,4 +777,13 @@ private static TimeSpan GetElapsed(long startingTimestamp) =>

[LoggerMessage(Level = LogLevel.Trace, Message = "{EndpointName} sending message. Message: '{Message}'.")]
private partial void LogSendingMessageSensitive(string endpointName, string message);

[LoggerMessage(Level = LogLevel.Debug, Message = "{EndpointName} session {SessionId} created with transport {TransportKind}")]
private partial void LogSessionCreated(string endpointName, string sessionId, string transportKind);
Copy link
Contributor

@halter73 halter73 Jul 10, 2025

Choose a reason for hiding this comment

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

Can we make these trace level logs? The Streamable HTTP transport creates one of these per-request in Stateless mode, so this could get pretty noisy. I also don't think we need both LogSessionCreated and LogSessionConnected. Created and Disposed should be sufficient. "Connected" doesn't make much sense on the server side anyway.

Out of curiosity, what made you think that these events in particular were interesting? I wonder if moving these logs up a level to McpClient and McpServer would allow us to provide more informative log messages.

Copy link
Author

@theojiang25 theojiang25 Jul 10, 2025

Choose a reason for hiding this comment

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

I agree that this could be very noisy, moved it to TRACE level.

Out of curiosity, what made you think that these events in particular were interesting? I wonder if moving these logs up a level to McpClient and McpServer would allow us to provide more informative log messages.

We had encountered issues related to session (session ID not found IIRC). And it's very hard to debug without any logs related to session lifecycle.
I looked into McpServer and McpEndpoint where getting session and creating new sessions are involved, I'm wondering if there's any extra information we can get out of it.

@@ -18,7 +18,7 @@

namespace ModelContextProtocol.AspNetCore;

internal sealed class StreamableHttpHandler(
internal sealed partial class StreamableHttpHandler(
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't see why this was made into a partial class.

Copy link
Author

@theojiang25 theojiang25 Jul 10, 2025

Choose a reason for hiding this comment

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

Made it a partial class because I wanted to use the LoggerMessage generation. Removed the Http header logs per comment.

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