fastmcp-client 0.11.0

MCP client implementation for FastMCP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
//! Caller-driven catalog reconciliation while a core subscription remains live.
//!
//! CACHE-03/SUB-03: acknowledge the change feed before fetching a full catalog,
//! invalidate before observing a change, and cancel an obsolete page traversal.
//! Only observed invalidation permits a bounded new traversal. A transport,
//! protocol, authorization or arbitrary host failure never triggers a retry.
//!
//! Both futures are polled in the caller's task. No runtime, worker, unbounded
//! event queue, detached refresh, automatic reconnect or missed-event replay is
//! created. Snapshots are consistent with changes observed by this driver, not
//! an atomic server-side snapshot or a promise of perpetual freshness. The host
//! owns already-delivered snapshots and must retire them when the watch ends.

use std::fmt;
use std::future::{Future, poll_fn};
use std::sync::{
    Mutex,
    atomic::{AtomicBool, Ordering},
};
use std::task::{Poll, Waker};
use std::time::{Duration, Instant};

use asupersync::{Cx, types::Time};
use fastmcp_core::McpRequestCancellation;
use fastmcp_protocol::protocol_policy::ProtocolEra;
use fastmcp_protocol::{CoreRequest, RequestId, ServerNotification, SubscriptionFilter};

use super::super::BoundedWriter;
use super::{
    CatalogKind, CollectedCatalog, ManagedCatalogClient, ManagedCatalogError, ManagedCoreError,
    Traversal, bounded_wait, call_deadline, check_call, list_params, prepare, require_unrevoked,
};
use crate::http_auth::managed::subscriptions::{
    ManagedSubscriptionError, ManagedSubscriptionEvent, ManagedSubscriptionLimits,
};
use crate::http_auth::managed::{OAuthCredentialSnapshot, OAuthSessionError};

/// Whole-watch bounds. Each traversal additionally retains its collector's
/// page/item/payload/notification limits; the subscription retains native HTTP
/// deadlines and frame limits. The listen POST also consumes one request ID.
#[derive(Clone, Copy, Debug)]
pub struct ManagedCatalogWatchLimits {
    timeout: Duration,
    maximum_collections: usize,
    maximum_request_ids: usize,
    maximum_state_bytes: usize,
    subscription_records: usize,
}

impl Default for ManagedCatalogWatchLimits {
    fn default() -> Self {
        Self {
            timeout: Duration::from_mins(15),
            maximum_collections: 64,
            maximum_request_ids: 4096,
            maximum_state_bytes: 1024 * 1024,
            subscription_records: 4096,
        }
    }
}

impl ManagedCatalogWatchLimits {
    /// Collection attempts include discarded traversals. Reaching the limit
    /// prevents another GET-like list POST, rather than returning partial data.
    pub fn new(
        timeout: Duration,
        maximum_collections: usize,
        maximum_request_ids: usize,
        maximum_state_bytes: usize,
        subscription_records: usize,
    ) -> Result<Self, ManagedCatalogWatchError> {
        if timeout.is_zero()
            || timeout > Duration::from_secs(3600)
            || !(1..=1024).contains(&maximum_collections)
            || !(2..=4096).contains(&maximum_request_ids)
            || !(1..=8 * 1024 * 1024).contains(&maximum_state_bytes)
            || !(2..=4096).contains(&subscription_records)
        {
            return Err(ManagedCatalogWatchError::InvalidLimits);
        }
        Ok(Self {
            timeout,
            maximum_collections,
            maximum_request_ids,
            maximum_state_bytes,
            subscription_records,
        })
    }
}

/// Changes are delivered after invalidation and before any replacement snapshot.
/// Snapshot pages retain their exact typed payloads; no merged TTL is invented.
pub enum ManagedCatalogWatchEvent {
    Acknowledged { accepted_filter: SubscriptionFilter },
    Notification(Box<ServerNotification>),
    Snapshot(CollectedCatalog),
}

/// The callback controls this watch only, never the shared login or remote work.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum ManagedCatalogWatchControl {
    Continue,
    Stop,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum ManagedCatalogWatchOutcome {
    StoppedByHost,
    /// The peer sent a valid subscription terminal. A new watch must acquire a
    /// new acknowledgment and fetch again; this outcome is not gap recovery.
    SubscriptionEnded,
}

/// Diagnostics retain no request IDs, filters, credentials or catalog payloads.
#[derive(Debug)]
pub enum ManagedCatalogWatchError {
    InvalidLimits,
    CursorNotAllowed,
    CoverageRefused,
    CollectionLimit,
    UnexpectedEvent,
    Catalog(ManagedCatalogError),
    Subscription(ManagedSubscriptionError),
}

impl fmt::Display for ManagedCatalogWatchError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::InvalidLimits => f.write_str("invalid catalog watch limits"),
            Self::CursorNotAllowed => f.write_str("catalog watch must start at the first page"),
            Self::CoverageRefused => {
                f.write_str("subscription did not accept the catalog change filter")
            }
            Self::CollectionLimit => f.write_str("catalog watch reconciliation limit exceeded"),
            Self::UnexpectedEvent => f.write_str("unexpected catalog subscription event"),
            Self::Catalog(error) => fmt::Display::fmt(error, f),
            Self::Subscription(error) => fmt::Display::fmt(error, f),
        }
    }
}
impl std::error::Error for ManagedCatalogWatchError {}
impl From<ManagedCatalogError> for ManagedCatalogWatchError {
    fn from(error: ManagedCatalogError) -> Self {
        Self::Catalog(error)
    }
}
impl From<ManagedCoreError> for ManagedCatalogWatchError {
    fn from(error: ManagedCoreError) -> Self {
        Self::Catalog(error.into())
    }
}
impl From<ManagedSubscriptionError> for ManagedCatalogWatchError {
    fn from(error: ManagedSubscriptionError) -> Self {
        Self::Subscription(error)
    }
}

impl ManagedCatalogClient {
    /// Watches one complete core catalog with subscription-driven reconciliation.
    ///
    /// The first page is fetched only after the peer acknowledges the relevant
    /// change category. Changes arriving while pages are fetched cancel that
    /// traversal and schedule a fresh one from the first page. Requests preserve
    /// the list's metadata and filters. A single ID history spans listen plus
    /// every traversal, including numerically equivalent ID representations.
    ///
    /// The callback is synchronous and must cooperate. It runs without the
    /// cache lock and may inspect/clear the shared cache. Stop, failure, deadline,
    /// cancellation and Drop invalidate this catalog's cache generation. Other
    /// catalogs are not cleared. No snapshot survives a stream gap by assertion.
    pub async fn watch<I, O>(
        &self,
        cx: &Cx,
        request: CoreRequest,
        limits: ManagedCatalogWatchLimits,
        next_id: I,
        observe: O,
    ) -> Result<ManagedCatalogWatchOutcome, ManagedCatalogWatchError>
    where
        I: FnMut() -> Result<RequestId, ManagedCatalogError>,
        O: FnMut(
            ManagedCatalogWatchEvent,
        ) -> Result<ManagedCatalogWatchControl, ManagedCatalogError>,
    {
        self.watch_with_cancellation(
            cx,
            &McpRequestCancellation::new(),
            request,
            limits,
            next_id,
            observe,
        )
        .await
    }

    /// Cancellation spans listen admission, acknowledgment, page reads, and idle
    /// waits between changes. Dropping this consuming future releases both live
    /// responses. Local invalidation cancels only the obsolete traversal, never
    /// the caller's context, subscription, shared session, or another collector.
    #[allow(clippy::too_many_arguments)]
    pub async fn watch_with_cancellation<I, O>(
        &self,
        cx: &Cx,
        cancellation: &McpRequestCancellation,
        request: CoreRequest,
        limits: ManagedCatalogWatchLimits,
        next_id: I,
        observe: O,
    ) -> Result<ManagedCatalogWatchOutcome, ManagedCatalogWatchError>
    where
        I: FnMut() -> Result<RequestId, ManagedCatalogError>,
        O: FnMut(
            ManagedCatalogWatchEvent,
        ) -> Result<ManagedCatalogWatchControl, ManagedCatalogError>,
    {
        let deadline = call_deadline(cx, cancellation, limits.timeout)?;
        let kind = CatalogKind::of(&request)?;
        let listen = listen_request(&request)?;
        let _ = prepare(
            self.session.resource().as_str(),
            request.clone(),
            RequestId::Number(0),
            self.limits.core,
        )?;
        let request_bytes = self.limits.core.request_bytes.min(64 * 1024);
        let subscription_limits = ManagedSubscriptionLimits::new(
            request_bytes,
            self.limits.core.frame_bytes.min(64 * 1024),
            limits.subscription_records,
            limits.timeout,
        )?;
        admit_listen_size(&listen, &RequestId::Number(0), request_bytes)?;
        let ids = Mutex::new(WatchIds {
            next: next_id,
            history: Traversal::default(),
            limits,
        });
        let observer = Observer {
            callback: Mutex::new(observe),
            stopped: AtomicBool::new(false),
        };
        let listen_id = issue_id(&ids, cx, cancellation, deadline)?;
        admit_listen_size(&listen, &listen_id, request_bytes)?;
        // Install cleanup before any I/O. It also fences fills from a sibling
        // traversal when this watch is abandoned during acknowledgment/read.
        let _gap = InvalidateOnExit { client: self, kind };
        self.cache()?.invalidate_result_set(&kind.result_set());
        Box::pin(bounded_wait(cx, cancellation, deadline, async {
            Ok(async {
                let binding = self
                    .session
                    .credential_with_cancellation(cx, cancellation)
                    .await
                    .map_err(ManagedCoreError::from)?;
                check_binding(cx, cancellation, deadline, &binding)?;
                let mut subscription = Box::pin(self.session.subscribe_core_with_cancellation(
                    cx,
                    cancellation,
                    listen,
                    listen_id,
                    subscription_limits,
                ))
                .await?;
                if subscription.credential_generation() != binding.generation() {
                    return Err(ManagedCatalogError::CredentialChanged.into());
                }
                let Some(ManagedSubscriptionEvent::Acknowledged { accepted_filter }) =
                    subscription.next_event(cx).await?
                else {
                    return Err(ManagedCatalogWatchError::UnexpectedEvent);
                };
                if !covers(kind, &accepted_filter) {
                    return Err(ManagedCatalogWatchError::CoverageRefused);
                }
                check_binding(cx, cancellation, deadline, &binding)?;
                // An old cache cannot bridge the pre-acknowledgment blind window.
                self.cache()?.invalidate_result_set(&kind.result_set());
                let continuing =
                    observer.emit(ManagedCatalogWatchEvent::Acknowledged { accepted_filter })?;
                check_binding(cx, cancellation, deadline, &binding)?;
                if !continuing {
                    return Ok(ManagedCatalogWatchOutcome::StoppedByHost);
                }
                let signal = ChangeSignal::default();
                let monitor = async {
                    loop {
                        check_binding(cx, cancellation, deadline, &binding)?;
                        let event = subscription.next_event(cx).await?;
                        check_binding(cx, cancellation, deadline, &binding)?;
                        match event {
                            Some(ManagedSubscriptionEvent::Notification(notification)) => {
                                self.invalidate_notification(&notification)?;
                                if relevant(kind, &notification) {
                                    signal.changed()?;
                                }
                                let continuing = observer
                                    .emit(ManagedCatalogWatchEvent::Notification(notification))?;
                                check_binding(cx, cancellation, deadline, &binding)?;
                                if !continuing {
                                    return Ok(ManagedCatalogWatchOutcome::StoppedByHost);
                                }
                            }
                            Some(ManagedSubscriptionEvent::Terminal { .. }) => {
                                return Ok(ManagedCatalogWatchOutcome::SubscriptionEnded);
                            }
                            _ => return Err(ManagedCatalogWatchError::UnexpectedEvent),
                        }
                    }
                };
                let reconcile = async {
                    let mut last_published = None;
                    let mut attempts = 0;
                    loop {
                        let revision = signal.wait_after(last_published).await?;
                        check_binding(cx, cancellation, deadline, &binding)?;
                        if attempts >= limits.maximum_collections {
                            return Err(ManagedCatalogWatchError::CollectionLimit);
                        }
                        let Some(local_cancel) = signal.begin(revision)? else {
                            continue;
                        };
                        attempts += 1;
                        let result = Box::pin(self.collect_with_cancellation(
                            cx,
                            &local_cancel,
                            request.clone(),
                            || {
                                check_binding(cx, cancellation, deadline, &binding)?;
                                let id = issue_id(&ids, cx, cancellation, deadline)?;
                                check_binding(cx, cancellation, deadline, &binding)?;
                                Ok(id)
                            },
                            |notification| {
                                // collect has already invalidated before calling
                                // this observer. Coalesce scheduling, not delivery.
                                if relevant(kind, &notification) {
                                    signal.changed()?;
                                }
                                check_binding(cx, cancellation, deadline, &binding)?;
                                let continuing = observer
                                    .emit(ManagedCatalogWatchEvent::Notification(notification))?;
                                check_binding(cx, cancellation, deadline, &binding)?;
                                if continuing {
                                    Ok(())
                                } else {
                                    Err(ManagedCatalogError::AbortedByHost)
                                }
                            },
                        ))
                        .await;
                        signal.finish()?;
                        check_binding(cx, cancellation, deadline, &binding)?;
                        if observer.stopped.load(Ordering::Acquire) {
                            return Ok(ManagedCatalogWatchOutcome::StoppedByHost);
                        }
                        let changed = signal.revision()? != revision;
                        let catalog = match result {
                            Ok(catalog) => catalog,
                            Err(error)
                                if changed && is_invalidation_stop(&error, &local_cancel) =>
                            {
                                // Retry only a locally superseded read traversal.
                                // No failed mutation or arbitrary I/O is replayed.
                                yield_once().await;
                                continue;
                            }
                            Err(error) => return Err(error.into()),
                        };
                        if catalog.credential_generation() != binding.generation() {
                            return Err(ManagedCatalogError::CredentialChanged.into());
                        }
                        // The collector may deliberately rebuild its cache
                        // generation under the selected whole-catalog policy.
                        // Retain its admitted generation, rather than assuming
                        // the generation from before collection must survive.
                        // Another clone's clear after admission still fences
                        // publication, even if it happens before this yield.
                        let cache_generation = catalog.cache_generation;
                        // Let the monitor drain currently ready notifications
                        // before publishing a candidate. Crucially, its pending
                        // read is never dropped just because a list completed.
                        yield_once().await;
                        check_binding(cx, cancellation, deadline, &binding)?;
                        if signal.revision()? != revision {
                            continue;
                        }
                        // An external clear during the publication yield is
                        // terminal, not a reason to override host policy by retry.
                        self.require_generation(&kind.result_set(), cache_generation)?;
                        last_published = Some(revision);
                        let continuing =
                            observer.emit(ManagedCatalogWatchEvent::Snapshot(catalog))?;
                        check_binding(cx, cancellation, deadline, &binding)?;
                        if !continuing {
                            return Ok(ManagedCatalogWatchOutcome::StoppedByHost);
                        }
                    }
                };
                // Monitor-first polling makes an already-ready gap/notification
                // win over a ready snapshot. Both losing futures remain owned
                // until the WHOLE watch finishes, not merely until a page wins.
                // Box the independently large HTTP state machines rather than
                // multiplying their stack footprint inside the caller's future.
                let mut monitor = Box::pin(monitor);
                let mut reconcile = Box::pin(reconcile);
                poll_fn(|task| {
                    check_binding(cx, cancellation, deadline, &binding)?;
                    if let Poll::Ready(result) = monitor.as_mut().poll(task) {
                        return Poll::Ready(result);
                    }
                    reconcile.as_mut().poll(task)
                })
                .await
            }
            .await)
        }))
        .await?
    }
}

fn listen_request(request: &CoreRequest) -> Result<CoreRequest, ManagedCatalogWatchError> {
    let kind = CatalogKind::of(request)?;
    if list_params(request)?.cursor.is_some() {
        return Err(ManagedCatalogWatchError::CursorNotAllowed);
    }
    let params = request
        .encode_params()
        .map_err(|_| ManagedCoreError::InvalidRequest)?
        .ok_or(ManagedCoreError::InvalidRequest)?;
    CoreRequest::decode(
        ProtocolEra::Modern2026,
        "subscriptions/listen",
        Some(&serde_json::json!({
            "_meta": params["_meta"], "notifications": requested_filter(kind),
        })),
    )
    .map_err(|_| ManagedCoreError::InvalidRequest.into())
}

fn requested_filter(kind: CatalogKind) -> SubscriptionFilter {
    let mut filter = SubscriptionFilter::default();
    match kind {
        CatalogKind::Tools => filter.tools_list_changed = Some(true),
        CatalogKind::Resources | CatalogKind::Templates => {
            filter.resources_list_changed = Some(true);
        }
        CatalogKind::Prompts => filter.prompts_list_changed = Some(true),
    }
    filter
}

fn covers(kind: CatalogKind, filter: &SubscriptionFilter) -> bool {
    match kind {
        CatalogKind::Tools => filter.tools_list_changed == Some(true),
        CatalogKind::Resources | CatalogKind::Templates => {
            filter.resources_list_changed == Some(true)
        }
        CatalogKind::Prompts => filter.prompts_list_changed == Some(true),
    }
}

fn relevant(kind: CatalogKind, notification: &ServerNotification) -> bool {
    matches!(
        (kind, notification),
        (CatalogKind::Tools, ServerNotification::ToolsListChanged(_))
            | (
                CatalogKind::Resources | CatalogKind::Templates,
                ServerNotification::ResourcesListChanged(_)
            )
            | (
                CatalogKind::Prompts,
                ServerNotification::PromptsListChanged(_)
            )
    )
}

fn admit_listen_size(
    request: &CoreRequest,
    id: &RequestId,
    maximum: usize,
) -> Result<(), ManagedCoreError> {
    let params = request
        .encode_params()
        .map_err(|_| ManagedCoreError::InvalidRequest)?;
    let envelope =
        serde_json::json!({"jsonrpc":"2.0","id":id,"method":request.method(),"params":params});
    let mut writer = BoundedWriter {
        bytes: Vec::new(),
        maximum,
    };
    serde_json::to_writer(&mut writer, &envelope).map_err(|_| ManagedCoreError::RequestTooLarge)
}

fn check_binding(
    cx: &Cx,
    cancellation: &McpRequestCancellation,
    deadline: Time,
    binding: &OAuthCredentialSnapshot,
) -> Result<(), ManagedCatalogError> {
    check_call(
        cx,
        cancellation,
        cx.budget()
            .deadline
            .map_or(deadline, |parent| parent.min(deadline)),
    )?;
    require_unrevoked(binding.credential())?;
    if Instant::now() >= binding.expires_at() {
        return Err(ManagedCoreError::Session(OAuthSessionError::LoginRequired).into());
    }
    Ok(())
}

struct WatchIds<I> {
    next: I,
    history: Traversal,
    limits: ManagedCatalogWatchLimits,
}
fn issue_id<I>(
    ids: &Mutex<WatchIds<I>>,
    cx: &Cx,
    cancellation: &McpRequestCancellation,
    deadline: Time,
) -> Result<RequestId, ManagedCatalogError>
where
    I: FnMut() -> Result<RequestId, ManagedCatalogError>,
{
    check_call(cx, cancellation, deadline)?;
    let mut ids = ids
        .lock()
        .map_err(|_| ManagedCatalogError::CacheUnavailable)?;
    if ids.history.ids.len() >= ids.limits.maximum_request_ids {
        return Err(ManagedCatalogError::StateLimit);
    }
    let id = (ids.next)()?;
    check_call(cx, cancellation, deadline)?;
    let maximum = ids.limits.maximum_state_bytes;
    ids.history.reserve_id(&id, maximum)?;
    Ok(id)
}

struct Observer<O> {
    callback: Mutex<O>,
    stopped: AtomicBool,
}
impl<O> Observer<O>
where
    O: FnMut(ManagedCatalogWatchEvent) -> Result<ManagedCatalogWatchControl, ManagedCatalogError>,
{
    fn emit(&self, event: ManagedCatalogWatchEvent) -> Result<bool, ManagedCatalogError> {
        let control = (self
            .callback
            .lock()
            .map_err(|_| ManagedCatalogError::CacheUnavailable)?)(event)?;
        let continuing = control == ManagedCatalogWatchControl::Continue;
        if !continuing {
            self.stopped.store(true, Ordering::Release);
        }
        Ok(continuing)
    }
}

#[derive(Default)]
struct ChangeState {
    revision: u64,
    active: Option<McpRequestCancellation>,
    waiter: Option<Waker>,
}
#[derive(Default)]
struct ChangeSignal(Mutex<ChangeState>);
impl ChangeSignal {
    fn revision(&self) -> Result<u64, ManagedCatalogError> {
        Ok(self
            .0
            .lock()
            .map_err(|_| ManagedCatalogError::CacheUnavailable)?
            .revision)
    }
    fn changed(&self) -> Result<(), ManagedCatalogError> {
        let (active, waiter) = {
            let mut state = self
                .0
                .lock()
                .map_err(|_| ManagedCatalogError::CacheUnavailable)?;
            state.revision = state
                .revision
                .checked_add(1)
                .ok_or(ManagedCatalogError::StateLimit)?;
            (state.active.take(), state.waiter.take())
        };
        if let Some(active) = active {
            active.cancel();
        }
        if let Some(waiter) = waiter {
            waiter.wake();
        }
        Ok(())
    }
    fn begin(&self, revision: u64) -> Result<Option<McpRequestCancellation>, ManagedCatalogError> {
        let mut state = self
            .0
            .lock()
            .map_err(|_| ManagedCatalogError::CacheUnavailable)?;
        if state.revision != revision {
            return Ok(None);
        }
        let cancellation = McpRequestCancellation::new();
        state.active = Some(cancellation.clone());
        Ok(Some(cancellation))
    }
    fn finish(&self) -> Result<(), ManagedCatalogError> {
        self.0
            .lock()
            .map_err(|_| ManagedCatalogError::CacheUnavailable)?
            .active = None;
        Ok(())
    }
    async fn wait_after(&self, previous: Option<u64>) -> Result<u64, ManagedCatalogError> {
        poll_fn(|task| {
            let mut state = self
                .0
                .lock()
                .map_err(|_| ManagedCatalogError::CacheUnavailable)?;
            if previous != Some(state.revision) {
                return Poll::Ready(Ok(state.revision));
            }
            state.waiter = Some(task.waker().clone());
            Poll::Pending
        })
        .await
    }
}

fn is_invalidation_stop(
    error: &ManagedCatalogError,
    local_cancel: &McpRequestCancellation,
) -> bool {
    matches!(error, ManagedCatalogError::Invalidated)
        || (local_cancel.is_cancel_requested()
            && matches!(
                error,
                ManagedCatalogError::Core(
                    ManagedCoreError::Cancelled
                        | ManagedCoreError::Session(OAuthSessionError::Cancelled)
                )
            ))
}

async fn yield_once() {
    let mut yielded = false;
    poll_fn(|task| {
        if yielded {
            Poll::Ready(())
        } else {
            yielded = true;
            task.waker().wake_by_ref();
            Poll::Pending
        }
    })
    .await;
}

struct InvalidateOnExit<'a> {
    client: &'a ManagedCatalogClient,
    kind: CatalogKind,
}
impl Drop for InvalidateOnExit<'_> {
    fn drop(&mut self) {
        // A poisoned cache already fails closed on all reads. Drop never
        // recovers it permissively or panics while unwinding a host callback.
        if let Ok(mut cache) = self.client.cache() {
            cache.invalidate_result_set(&self.kind.result_set());
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use fastmcp_protocol::{ClientCapabilities, FinalRequestMeta, JsonRpcRequest};
    use serde_json::json;

    fn list(method: &str) -> CoreRequest {
        CoreRequest::decode(
            ProtocolEra::Modern2026,
            method,
            Some(&json!({
                "_meta": FinalRequestMeta::new(ClientCapabilities::default()),
                "includeTags":["selected"], "excludeTags":[],
            })),
        )
        .unwrap()
    }

    #[test]
    fn all_catalogs_subscribe_to_their_own_change_category_and_preserve_metadata() {
        for (method, key) in [
            ("tools/list", "toolsListChanged"),
            ("resources/list", "resourcesListChanged"),
            ("resources/templates/list", "resourcesListChanged"),
            ("prompts/list", "promptsListChanged"),
        ] {
            let request = list(method);
            let listen = listen_request(&request).unwrap();
            let params = listen.encode_params().unwrap().unwrap();
            assert_eq!(listen.method(), "subscriptions/listen");
            assert_eq!(
                params["_meta"],
                request.encode_params().unwrap().unwrap()["_meta"]
            );
            assert_eq!(params["notifications"], json!({key:true}));
            assert!(params.get("cursor").is_none());
            assert!(params.get("includeTags").is_none());
            let kind = CatalogKind::of(&request).unwrap();
            assert!(covers(kind, &requested_filter(kind)));
            assert!(!covers(kind, &SubscriptionFilter::default()));
        }
    }

    #[test]
    fn a_suffix_is_not_silently_presented_as_a_complete_watched_catalog() {
        for cursor in ["", "opaque"] {
            let mut request = list("tools/list");
            super::super::list_params_mut(&mut request).unwrap().cursor = Some(cursor.to_owned());
            assert!(matches!(
                listen_request(&request),
                Err(ManagedCatalogWatchError::CursorNotAllowed)
            ));
        }
    }

    #[test]
    fn only_the_matching_change_schedules_catalog_reconciliation() {
        let tools = ServerNotification::decode(&JsonRpcRequest::notification(
            "notifications/tools/list_changed",
            None,
        ))
        .unwrap();
        let resources = ServerNotification::decode(&JsonRpcRequest::notification(
            "notifications/resources/list_changed",
            None,
        ))
        .unwrap();
        assert!(relevant(CatalogKind::Tools, &tools));
        assert!(!relevant(CatalogKind::Prompts, &tools));
        assert!(relevant(CatalogKind::Templates, &resources));
        assert!(!relevant(CatalogKind::Tools, &resources));
    }

    #[test]
    fn invalidation_cancels_only_the_obsolete_traversal_and_coalesces_pending_changes() {
        let signal = ChangeSignal::default();
        let first = signal.begin(0).unwrap().unwrap();
        let sibling = McpRequestCancellation::new();
        signal.changed().unwrap();
        signal.changed().unwrap();
        assert!(first.is_cancel_requested());
        assert!(!sibling.is_cancel_requested());
        assert_eq!(signal.revision().unwrap(), 2);
        assert!(signal.begin(0).unwrap().is_none());
        let replacement = signal.begin(2).unwrap().unwrap();
        assert!(!replacement.is_cancel_requested());
        signal.finish().unwrap();
        signal.changed().unwrap();
        assert!(!replacement.is_cancel_requested());
    }

    #[test]
    fn pending_change_wait_is_woken_without_a_polling_timer() {
        use std::sync::Arc;
        use std::task::Wake;
        struct Flag(AtomicBool);
        impl Wake for Flag {
            fn wake(self: Arc<Self>) {
                self.0.store(true, Ordering::Release);
            }
        }
        let flag = Arc::new(Flag(AtomicBool::new(false)));
        let waker = Waker::from(flag.clone());
        let mut task = std::task::Context::from_waker(&waker);
        let signal = ChangeSignal::default();
        let mut wait = Box::pin(signal.wait_after(Some(0)));
        assert!(wait.as_mut().poll(&mut task).is_pending());
        signal.changed().unwrap();
        assert!(flag.0.load(Ordering::Acquire));
        assert!(matches!(wait.as_mut().poll(&mut task), Poll::Ready(Ok(1))));
    }

    #[test]
    fn unrelated_errors_cannot_be_laundered_into_invalidation_retries() {
        let cancelled = McpRequestCancellation::new();
        assert!(!is_invalidation_stop(
            &ManagedCoreError::Cancelled.into(),
            &cancelled
        ));
        cancelled.cancel();
        assert!(is_invalidation_stop(
            &ManagedCoreError::Cancelled.into(),
            &cancelled
        ));
        for error in [
            ManagedCatalogError::InvalidPage,
            ManagedCatalogError::AbortedByHost,
            ManagedCoreError::InvalidResponse.into(),
            ManagedCoreError::HttpStatus { status: 500 }.into(),
        ] {
            assert!(!is_invalidation_stop(&error, &cancelled));
        }
    }

    #[test]
    fn watch_budget_and_listen_bytes_have_real_boundaries() {
        let defaults = ManagedCatalogWatchLimits::default();
        assert!(ManagedCatalogWatchLimits::new(Duration::ZERO, 1, 2, 1, 2).is_err());
        assert!(ManagedCatalogWatchLimits::new(defaults.timeout, 0, 2, 1, 2).is_err());
        assert!(ManagedCatalogWatchLimits::new(defaults.timeout, 1, 1, 1, 2).is_err());
        assert!(ManagedCatalogWatchLimits::new(defaults.timeout, 1, 2, 0, 2).is_err());
        let request = listen_request(&list("tools/list")).unwrap();
        assert!(admit_listen_size(&request, &RequestId::Number(1), 4096).is_ok());
        assert!(matches!(
            admit_listen_size(&request, &RequestId::Number(1), 1),
            Err(ManagedCoreError::RequestTooLarge)
        ));
    }
}