@@ -436,7 +436,8 @@ public async Task<JsonRpcResponse> SendRequestAsync(JsonRpcRequest request, Canc
436436 // Now that the request has been sent, register for cancellation. If we registered before,
437437 // a cancellation request could arrive before the server knew about that request ID, in which
438438 // case the server could ignore it.
439- LogRequestSentAwaitingResponse ( EndpointName , request . Method , request . Id ) ;
439+ string ? target = GetRequestTarget ( request ) ;
440+ LogRequestSentAwaitingResponse ( EndpointName , request . Method , request . Id , toolName : target ) ;
440441 JsonRpcMessage ? response ;
441442 using ( var registration = RegisterCancellation ( cancellationToken , request ) )
442443 {
@@ -445,7 +446,7 @@ public async Task<JsonRpcResponse> SendRequestAsync(JsonRpcRequest request, Canc
445446
446447 if ( response is JsonRpcError error )
447448 {
448- LogSendingRequestFailed ( EndpointName , request . Method , error . Error . Message , error . Error . Code ) ;
449+ LogSendingRequestFailed ( EndpointName , request . Method , error . Error . Message , error . Error . Code , toolName : target ) ;
449450 throw new McpException ( $ "Request failed (remote): { error . Error . Message } ", ( McpErrorCode ) error . Error . Code ) ;
450451 }
451452
@@ -458,11 +459,11 @@ public async Task<JsonRpcResponse> SendRequestAsync(JsonRpcRequest request, Canc
458459
459460 if ( _logger . IsEnabled ( LogLevel . Trace ) )
460461 {
461- LogRequestResponseReceivedSensitive ( EndpointName , request . Method , success . Result ? . ToJsonString ( ) ?? "null" ) ;
462+ LogRequestResponseReceivedSensitive ( EndpointName , request . Method , success . Result ? . ToJsonString ( ) ?? "null" , toolName : target ) ;
462463 }
463464 else
464465 {
465- LogRequestResponseReceived ( EndpointName , request . Method ) ;
466+ LogRequestResponseReceived ( EndpointName , request . Method , toolName : target ) ;
466467 }
467468
468469 return success ;
@@ -763,6 +764,29 @@ private static TimeSpan GetElapsed(long startingTimestamp) =>
763764 return null ;
764765 }
765766
767+ /// <summary>
768+ /// Extracts the target identifier (tool name, prompt name, or resource URI) from a request.
769+ /// </summary>
770+ /// <param name="request">The JSON-RPC request.</param>
771+ /// <returns>The target identifier if available; otherwise, null.</returns>
772+ private static string ? GetRequestTarget ( JsonRpcRequest request )
773+ {
774+ if ( request . Params is not JsonObject paramsObj )
775+ {
776+ return null ;
777+ }
778+
779+ return request . Method switch
780+ {
781+ RequestMethods . ToolsCall => GetStringProperty ( paramsObj , "name" ) ,
782+ RequestMethods . PromptsGet => GetStringProperty ( paramsObj , "name" ) ,
783+ RequestMethods . ResourcesRead => GetStringProperty ( paramsObj , "uri" ) ,
784+ RequestMethods . ResourcesSubscribe => GetStringProperty ( paramsObj , "uri" ) ,
785+ RequestMethods . ResourcesUnsubscribe => GetStringProperty ( paramsObj , "uri" ) ,
786+ _ => null
787+ } ;
788+ }
789+
766790 [ LoggerMessage ( Level = LogLevel . Information , Message = "{EndpointName} message processing canceled." ) ]
767791 private partial void LogEndpointMessageProcessingCanceled ( string endpointName ) ;
768792
@@ -778,8 +802,8 @@ private static TimeSpan GetElapsed(long startingTimestamp) =>
778802 [ LoggerMessage ( Level = LogLevel . Information , Message = "{EndpointName} received request for unknown request ID '{RequestId}'." ) ]
779803 private partial void LogNoRequestFoundForMessageWithId ( string endpointName , RequestId requestId ) ;
780804
781- [ LoggerMessage ( Level = LogLevel . Warning , Message = "{EndpointName} request failed for method '{Method}': {ErrorMessage} ({ErrorCode})." ) ]
782- private partial void LogSendingRequestFailed ( string endpointName , string method , string errorMessage , int errorCode ) ;
805+ [ LoggerMessage ( Level = LogLevel . Warning , Message = "{EndpointName} request failed for method '{Method}' (tool: '{ToolName}') : {ErrorMessage} ({ErrorCode})." ) ]
806+ private partial void LogSendingRequestFailed ( string endpointName , string method , string errorMessage , int errorCode , string ? toolName = null ) ;
783807
784808 [ LoggerMessage ( Level = LogLevel . Warning , Message = "{EndpointName} received invalid response for method '{Method}'." ) ]
785809 private partial void LogSendingRequestInvalidResponseType ( string endpointName , string method ) ;
@@ -793,11 +817,11 @@ private static TimeSpan GetElapsed(long startingTimestamp) =>
793817 [ LoggerMessage ( Level = LogLevel . Information , Message = "{EndpointName} canceled request '{RequestId}' per client notification. Reason: '{Reason}'." ) ]
794818 private partial void LogRequestCanceled ( string endpointName , RequestId requestId , string ? reason ) ;
795819
796- [ LoggerMessage ( Level = LogLevel . Debug , Message = "{EndpointName} Request response received for method {method}" ) ]
797- private partial void LogRequestResponseReceived ( string endpointName , string method ) ;
820+ [ LoggerMessage ( Level = LogLevel . Debug , Message = "{EndpointName} Request response received for method {method} (tool: '{ToolName}') " ) ]
821+ private partial void LogRequestResponseReceived ( string endpointName , string method , string ? toolName = null ) ;
798822
799- [ LoggerMessage ( Level = LogLevel . Trace , Message = "{EndpointName} Request response received for method {method}. Response: '{Response}'." ) ]
800- private partial void LogRequestResponseReceivedSensitive ( string endpointName , string method , string response ) ;
823+ [ LoggerMessage ( Level = LogLevel . Trace , Message = "{EndpointName} Request response received for method {method} (tool: '{ToolName}') . Response: '{Response}'." ) ]
824+ private partial void LogRequestResponseReceivedSensitive ( string endpointName , string method , string response , string ? toolName = null ) ;
801825
802826 [ LoggerMessage ( Level = LogLevel . Debug , Message = "{EndpointName} read {MessageType} message from channel." ) ]
803827 private partial void LogMessageRead ( string endpointName , string messageType ) ;
@@ -814,8 +838,8 @@ private static TimeSpan GetElapsed(long startingTimestamp) =>
814838 [ LoggerMessage ( Level = LogLevel . Warning , Message = "{EndpointName} received request for method '{Method}', but no handler is available." ) ]
815839 private partial void LogNoHandlerFoundForRequest ( string endpointName , string method ) ;
816840
817- [ LoggerMessage ( Level = LogLevel . Debug , Message = "{EndpointName} waiting for response to request '{RequestId}' for method '{Method}'." ) ]
818- private partial void LogRequestSentAwaitingResponse ( string endpointName , string method , RequestId requestId ) ;
841+ [ LoggerMessage ( Level = LogLevel . Debug , Message = "{EndpointName} waiting for response to request '{RequestId}' for method '{Method}' (tool: '{ToolName}') ." ) ]
842+ private partial void LogRequestSentAwaitingResponse ( string endpointName , string method , RequestId requestId , string ? toolName = null ) ;
819843
820844 [ LoggerMessage ( Level = LogLevel . Debug , Message = "{EndpointName} sending message." ) ]
821845 private partial void LogSendingMessage ( string endpointName ) ;
0 commit comments