@@ -21,6 +21,7 @@ internal sealed partial class McpServerImpl : McpServer
2121 } ;
2222
2323 private readonly ILogger _logger ;
24+ private readonly Func < Exception , string > ? _exceptionSummarizer ;
2425 private readonly ITransport _sessionTransport ;
2526 private readonly bool _servicesScopePerRequest ;
2627 private readonly List < Action > _disposables = [ ] ;
@@ -88,6 +89,7 @@ public McpServerImpl(ITransport transport, McpServerOptions options, ILoggerFact
8889 _endpointName = _serverOnlyEndpointName ;
8990 _servicesScopePerRequest = options . ScopeRequests ;
9091 _logger = loggerFactory ? . CreateLogger < McpServer > ( ) ?? NullLogger < McpServer > . Instance ;
92+ _exceptionSummarizer = options . ExceptionSummarizer ;
9193
9294 _clientInfo = options . KnownClientInfo ;
9395 _clientCapabilities = options . KnownClientCapabilities ;
@@ -161,7 +163,8 @@ void Register<TPrimitive>(McpServerPrimitiveCollection<TPrimitive>? collection,
161163 _notificationHandlers ,
162164 incomingMessageFilter ,
163165 outgoingMessageFilter ,
164- _logger ) ;
166+ _logger ,
167+ _exceptionSummarizer ) ;
165168 }
166169
167170 /// <summary>
@@ -1230,7 +1233,7 @@ await originalListResourceTemplatesHandler(request, cancellationToken).Configure
12301233 }
12311234 catch ( Exception e )
12321235 {
1233- ReadResourceError ( request . Params ? . Uri ?? string . Empty , e ) ;
1236+ LogReadResourceError ( request . Params ? . Uri ?? string . Empty , e ) ;
12341237 throw ;
12351238 }
12361239 } ) ;
@@ -1344,7 +1347,7 @@ await originalListPromptsHandler(request, cancellationToken).ConfigureAwait(fals
13441347 }
13451348 catch ( Exception e )
13461349 {
1347- GetPromptError ( request . Params ? . Name ?? string . Empty , e ) ;
1350+ LogGetPromptError ( request . Params ? . Name ?? string . Empty , e ) ;
13481351 throw ;
13491352 }
13501353 } ) ;
@@ -1597,7 +1600,7 @@ private McpRequestInvocationFilter<CallToolRequestParams, ResultOrAlternate<Call
15971600 // not an error (tools throw it to signal an InputRequiredResult).
15981601 if ( ! ( e is OperationCanceledException && cancellationToken . IsCancellationRequested ) && e is not InputRequiredException )
15991602 {
1600- ToolCallError ( request . Params ? . Name ?? string . Empty , e ) ;
1603+ LogToolCallError ( request . Params ? . Name ?? string . Empty , e ) ;
16011604 }
16021605
16031606 if ( ( e is OperationCanceledException && cancellationToken . IsCancellationRequested ) || e is McpProtocolException || e is InputRequiredException )
@@ -1648,7 +1651,7 @@ private void LogToolCallLifecycles(
16481651 else if ( ! ( lifecycle . Exception is OperationCanceledException && lifecycle . CancellationRequested ) &&
16491652 lifecycle . Exception is not InputRequiredException )
16501653 {
1651- ToolCallError ( toolName , lifecycle . Exception ! ) ;
1654+ LogToolCallError ( toolName , lifecycle . Exception ! ) ;
16521655 }
16531656 }
16541657 }
@@ -2380,7 +2383,7 @@ private async Task ObserveHandlerCompletionAsync(Task<JsonNode?> handlerTask)
23802383 }
23812384 catch ( Exception ex )
23822385 {
2383- MrtrHandlerError ( ex ) ;
2386+ LogMrtrHandlerError ( ex ) ;
23842387 }
23852388 finally
23862389 {
@@ -2407,21 +2410,78 @@ private async Task ObserveHandlerCompletionAsync(Task<JsonNode?> handlerTask)
24072410 }
24082411 }
24092412
2413+ private void LogToolCallError ( string toolName , Exception exception )
2414+ {
2415+ if ( ExceptionSummaryHelper . TrySummarize ( _exceptionSummarizer , exception , out string ? exceptionSummary ) )
2416+ {
2417+ ToolCallErrorSummarized ( toolName , exceptionSummary ) ;
2418+ }
2419+ else
2420+ {
2421+ ToolCallError ( toolName , exception ) ;
2422+ }
2423+ }
2424+
2425+ private void LogGetPromptError ( string promptName , Exception exception )
2426+ {
2427+ if ( ExceptionSummaryHelper . TrySummarize ( _exceptionSummarizer , exception , out string ? exceptionSummary ) )
2428+ {
2429+ GetPromptErrorSummarized ( promptName , exceptionSummary ) ;
2430+ }
2431+ else
2432+ {
2433+ GetPromptError ( promptName , exception ) ;
2434+ }
2435+ }
2436+
2437+ private void LogReadResourceError ( string resourceUri , Exception exception )
2438+ {
2439+ if ( ExceptionSummaryHelper . TrySummarize ( _exceptionSummarizer , exception , out string ? exceptionSummary ) )
2440+ {
2441+ ReadResourceErrorSummarized ( resourceUri , exceptionSummary ) ;
2442+ }
2443+ else
2444+ {
2445+ ReadResourceError ( resourceUri , exception ) ;
2446+ }
2447+ }
2448+
2449+ private void LogMrtrHandlerError ( Exception exception )
2450+ {
2451+ if ( ExceptionSummaryHelper . TrySummarize ( _exceptionSummarizer , exception , out string ? exceptionSummary ) )
2452+ {
2453+ MrtrHandlerErrorSummarized ( exceptionSummary ) ;
2454+ }
2455+ else
2456+ {
2457+ MrtrHandlerError ( exception ) ;
2458+ }
2459+ }
2460+
24102461 [ LoggerMessage ( Level = LogLevel . Error , Message = "\" {ToolName}\" threw an unhandled exception." ) ]
24112462 private partial void ToolCallError ( string toolName , Exception exception ) ;
24122463
2464+ [ LoggerMessage ( Level = LogLevel . Error , Message = "\" {ToolName}\" threw an unhandled exception: {ExceptionSummary}." ) ]
2465+ private partial void ToolCallErrorSummarized ( string toolName , string exceptionSummary ) ;
2466+
24132467 [ LoggerMessage ( Level = LogLevel . Information , Message = "\" {ToolName}\" completed. IsError = {IsError}." ) ]
24142468 private partial void ToolCallCompleted ( string toolName , bool isError ) ;
24152469
24162470 [ LoggerMessage ( Level = LogLevel . Error , Message = "GetPrompt \" {PromptName}\" threw an unhandled exception." ) ]
24172471 private partial void GetPromptError ( string promptName , Exception exception ) ;
24182472
2473+ [ LoggerMessage ( Level = LogLevel . Error , Message = "GetPrompt \" {PromptName}\" threw an unhandled exception: {ExceptionSummary}." ) ]
2474+ private partial void GetPromptErrorSummarized ( string promptName , string exceptionSummary ) ;
2475+
24192476 [ LoggerMessage ( Level = LogLevel . Information , Message = "GetPrompt \" {PromptName}\" completed." ) ]
24202477 private partial void GetPromptCompleted ( string promptName ) ;
24212478
24222479 [ LoggerMessage ( Level = LogLevel . Error , Message = "ReadResource \" {ResourceUri}\" threw an unhandled exception." ) ]
24232480 private partial void ReadResourceError ( string resourceUri , Exception exception ) ;
24242481
2482+ [ LoggerMessage ( Level = LogLevel . Error , Message = "ReadResource \" {ResourceUri}\" threw an unhandled exception: {ExceptionSummary}." ) ]
2483+ private partial void ReadResourceErrorSummarized ( string resourceUri , string exceptionSummary ) ;
2484+
24252485 [ LoggerMessage ( Level = LogLevel . Information , Message = "ReadResource \" {ResourceUri}\" completed." ) ]
24262486 private partial void ReadResourceCompleted ( string resourceUri ) ;
24272487
@@ -2431,6 +2491,9 @@ private async Task ObserveHandlerCompletionAsync(Task<JsonNode?> handlerTask)
24312491 [ LoggerMessage ( Level = LogLevel . Debug , Message = "An MRTR handler threw an unhandled exception." ) ]
24322492 private partial void MrtrHandlerError ( Exception exception ) ;
24332493
2494+ [ LoggerMessage ( Level = LogLevel . Debug , Message = "An MRTR handler threw an unhandled exception: {ExceptionSummary}." ) ]
2495+ private partial void MrtrHandlerErrorSummarized ( string exceptionSummary ) ;
2496+
24342497 [ LoggerMessage ( Level = LogLevel . Debug , Message = "Failed to deliver \" {NotificationMethod}\" to subscription \" {SubscriptionId}\" ." ) ]
24352498 private partial void SubscriptionNotificationFailed ( string notificationMethod , string subscriptionId , Exception exception ) ;
24362499}
0 commit comments