File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 99using Microsoft . Extensions . AI ;
1010using Microsoft . Extensions . Logging ;
1111using Polly ;
12+ using Polly . CircuitBreaker ;
1213using Serilog ;
1314using Serilog . Formatting . Compact ;
1415
6263builder . Services . AddSingleton < McpToolDispatcher > ( ) ;
6364
6465// ── Polly resilience pipeline ────────────────────────────────────────────────────────────
66+ // AddCircuitBreaker MUST come before AddRetry — outermost strategy trips first,
67+ // preventing retry budget waste when MCP is unavailable (D-07, D-08).
6568builder . Services . AddResiliencePipeline ( "mcp-tools" , pipelineBuilder => pipelineBuilder
69+ . AddCircuitBreaker ( new CircuitBreakerStrategyOptions
70+ {
71+ // "5 consecutive failures within 60s" expressed as ratio-based (Polly v8 only has ratio-based CB)
72+ FailureRatio = 1.0 ,
73+ SamplingDuration = TimeSpan . FromSeconds ( 60 ) ,
74+ MinimumThroughput = 5 ,
75+ BreakDuration = TimeSpan . FromSeconds ( 30 ) ,
76+ ShouldHandle = new PredicateBuilder ( )
77+ . Handle < McpException > ( ex => ex . IsTransient && ! ex . IsSecondaryRateLimit )
78+ } )
6679 . AddRetry ( new Polly . Retry . RetryStrategyOptions
6780 {
6881 MaxRetryAttempts = 3 ,
You can’t perform that action at this time.
0 commit comments