Skip to content

Commit a15163d

Browse files
committed
add debug log for McpSession
1 parent 984aa9a commit a15163d

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

src/ModelContextProtocol.Core/McpSession.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ public McpSession(
9595
_requestHandlers = requestHandlers;
9696
_notificationHandlers = notificationHandlers;
9797
_logger = logger ?? NullLogger.Instance;
98+
LogSessionCreated(EndpointName, _sessionId, _transportKind);
9899
}
99100

100101
/// <summary>
@@ -108,6 +109,7 @@ public McpSession(
108109
/// </summary>
109110
public async Task ProcessMessagesAsync(CancellationToken cancellationToken)
110111
{
112+
LogSessionConnected(EndpointName, _sessionId, _transportKind);
111113
try
112114
{
113115
await foreach (var message in _transport.MessageReader.ReadAllAsync(cancellationToken).ConfigureAwait(false))
@@ -609,9 +611,9 @@ private static void AddExceptionTags(ref TagList tags, Activity? activity, Excep
609611
e = ae.InnerException;
610612
}
611613

612-
int? intErrorCode =
614+
int? intErrorCode =
613615
(int?)((e as McpException)?.ErrorCode) is int errorCode ? errorCode :
614-
e is JsonException ? (int)McpErrorCode.ParseError :
616+
e is JsonException ? (int)McpErrorCode.ParseError :
615617
null;
616618

617619
string? errorType = intErrorCode?.ToString() ?? e.GetType().FullName;
@@ -692,6 +694,7 @@ public void Dispose()
692694
}
693695

694696
_pendingRequests.Clear();
697+
LogSessionDisposed(EndpointName, _sessionId, _transportKind);
695698
}
696699

697700
#if !NET
@@ -774,4 +777,13 @@ private static TimeSpan GetElapsed(long startingTimestamp) =>
774777

775778
[LoggerMessage(Level = LogLevel.Trace, Message = "{EndpointName} sending message. Message: '{Message}'.")]
776779
private partial void LogSendingMessageSensitive(string endpointName, string message);
780+
781+
[LoggerMessage(Level = LogLevel.Debug, Message = "{EndpointName} session {SessionId} created with transport {TransportKind}")]
782+
private partial void LogSessionCreated(string endpointName, string sessionId, string transportKind);
783+
784+
[LoggerMessage(Level = LogLevel.Debug, Message = "{EndpointName} session {SessionId} connected and processing messages with transport {TransportKind}")]
785+
private partial void LogSessionConnected(string endpointName, string sessionId, string transportKind);
786+
787+
[LoggerMessage(Level = LogLevel.Debug, Message = "{EndpointName} session {SessionId} disposed with transport {TransportKind}")]
788+
private partial void LogSessionDisposed(string endpointName, string sessionId, string transportKind);
777789
}

0 commit comments

Comments
 (0)