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
//! Schema-bound use of the explicitly configured one-shot header-repair path.
//!
//! The core repair owner proves only its configured pre-dispatch rejection,
//! preserves the invocation, collects the catalog, reviews disclosure and sends
//! at most one retry. This adapter admits the replacement input/output contract
//! before host approval, validates the unchanged invocation against it, and
//! validates successful output before publication. It never changes the original
//! client, revives a retired catalog, or manufactures another repair attempt.
//!
//! The original tool's validity remains necessary throughout, including while
//! refreshing and reading a repaired result. A schema change accepted here is
//! private to this call, not an update to an old catalog snapshot or its handles.

use std::fmt;
use std::sync::Arc;

use asupersync::Cx;
use fastmcp_core::McpRequestCancellation;
use fastmcp_protocol::http_headers::ParameterHeaderBinding;
use fastmcp_protocol::{CoreRequest, FinalTool, RequestId};

use super::super::interaction::ManagedToolInteraction;
use super::super::{
    ManagedToolCall, ManagedToolClient, ManagedToolError, ToolContract, await_validity,
    check_tool_call,
};
use crate::http_auth::rpc::catalog::ManagedCatalogError;
use crate::http_auth::rpc::tool_headers::repair::{
    RejectedToolHeaders, RepairedToolCall, ToolHeaderRepairContract, ToolHeaderRepairError,
    ToolHeaderRepairLimits, ToolHeaderRepairOutcome,
};
use crate::http_auth::rpc::{ManagedCoreCall, ManagedCoreEvent};

/// Fixed diagnostics do not retain schema, argument or peer-response content.
#[derive(Debug)]
pub enum ManagedToolRepairError {
    HeaderReviewRequired,
    Tool(ManagedToolError),
    Repair(ToolHeaderRepairError),
}

impl fmt::Display for ManagedToolRepairError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::HeaderReviewRequired => {
                f.write_str("schema-bound header repair requires an installed disclosure review")
            }
            Self::Tool(error) => fmt::Display::fmt(error, f),
            Self::Repair(error) => fmt::Display::fmt(error, f),
        }
    }
}
impl std::error::Error for ManagedToolRepairError {}
impl From<ManagedToolError> for ManagedToolRepairError {
    fn from(error: ManagedToolError) -> Self {
        Self::Tool(error)
    }
}
impl From<ToolHeaderRepairError> for ManagedToolRepairError {
    fn from(error: ToolHeaderRepairError) -> Self {
        Self::Repair(error)
    }
}

/// Only an admitted rejection permits the separate, consuming refresh action.
#[must_use = "drive the checked call or explicitly decide whether to repair its rejection"]
pub enum ManagedToolHeaderRepairOutcome {
    Call(ManagedToolRepairCall),
    Rejected(RejectedManagedToolHeaders),
}

/// Single-use repair custody with the original schema-bound validity owner.
/// Dropping this value performs no refresh, retry or shared-session cancellation.
pub struct RejectedManagedToolHeaders {
    rejected: RejectedToolHeaders,
    original: CoreRequest,
    contract: Arc<ToolContract>,
    cancellation: McpRequestCancellation,
}

impl fmt::Debug for RejectedManagedToolHeaders {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("RejectedManagedToolHeaders")
            .field("invalidated", &self.contract.is_invalidated())
            .finish_non_exhaustive()
    }
}

/// Incremental output checked against the approved replacement (or initial)
/// schema. The original client/catalog validity remains a separate requirement.
/// Neither the unchecked core call nor a replacement reusable client is exposed.
pub struct ManagedToolRepairCall {
    call: ManagedToolCall,
    source: Arc<ToolContract>,
}

impl ManagedToolRepairCall {
    pub fn close(&mut self) {
        self.call.close();
    }

    /// Invalidation wakes a pending read and refuses even a simultaneously ready
    /// result. A failed or abandoned read closes the owned call, not the login.
    /// An input-required result is returned intact, never automatically resumed.
    pub async fn next_event(
        &mut self,
        cx: &Cx,
    ) -> Result<Option<ManagedCoreEvent>, ManagedToolError> {
        if self.call.finished {
            return Ok(None);
        }
        let cancellation = self.call.cancellation.clone();
        let outcome = Box::pin(await_validity(
            cx,
            &cancellation,
            &self.source,
            self.call.next_event(cx),
        ))
        .await;
        match outcome {
            Ok(result) => result,
            Err(error) => {
                self.call.close();
                // The inner call may have completed in the same poll that
                // invalidated the source. Its result was not published here.
                self.call.finished = false;
                Err(error)
            }
        }
    }
}

impl ManagedToolClient {
    /// Attempts one schema-checked initial call with explicit header-repair
    /// eligibility. The installed header review and original input schema are
    /// checked before any credential acquisition or POST. Ordinary `request`
    /// and interaction APIs do not gain retries by using this module.
    ///
    /// The endpoint contract is independently trusted deployment configuration;
    /// authenticated TLS or a peer error code alone cannot prove non-execution.
    /// This opt-in schema-bound API intentionally validates complete arguments.
    /// Use the core header API for transport-only null-omission semantics.
    pub async fn request_with_header_repair(
        &self,
        cx: &Cx,
        request: CoreRequest,
        request_id: RequestId,
        endpoint: ToolHeaderRepairContract,
        limits: ToolHeaderRepairLimits,
    ) -> Result<ManagedToolHeaderRepairOutcome, ManagedToolRepairError> {
        Box::pin(self.request_with_header_repair_and_cancellation(
            cx,
            &McpRequestCancellation::new(),
            request,
            request_id,
            endpoint,
            limits,
        ))
        .await
    }

    /// Request-local cancellation and tool/catalog invalidation both fence the
    /// initial exchange and its later repair custody. No caller or sibling is
    /// cancelled. Abandoning credential renewal retains the managed session's
    /// existing fail-closed treatment of an uncertain refresh-token lineage.
    #[allow(clippy::too_many_arguments)]
    pub async fn request_with_header_repair_and_cancellation(
        &self,
        cx: &Cx,
        cancellation: &McpRequestCancellation,
        request: CoreRequest,
        request_id: RequestId,
        endpoint: ToolHeaderRepairContract,
        limits: ToolHeaderRepairLimits,
    ) -> Result<ManagedToolHeaderRepairOutcome, ManagedToolRepairError> {
        check_tool_call(cx, cancellation, &self.contract)?;
        let reviewed = self
            .header_review
            .as_deref()
            .ok_or(ManagedToolRepairError::HeaderReviewRequired)?;
        self.contract.validate_request(&request)?;
        self.contract
            .admit_headers(self.session.resource(), reviewed)?;
        check_tool_call(cx, cancellation, &self.contract)?;
        let original = request.clone();
        let outcome = Box::pin(await_validity(
            cx,
            cancellation,
            &self.contract,
            self.session
                .request_tool_with_header_repair_and_cancellation(
                    cx,
                    cancellation,
                    request,
                    request_id,
                    reviewed,
                    endpoint,
                    limits,
                ),
        ))
        .await??;
        check_tool_call(cx, cancellation, &self.contract)?;
        Ok(match outcome {
            ToolHeaderRepairOutcome::Call(call) => ManagedToolHeaderRepairOutcome::Call(bind_call(
                call,
                Arc::clone(&self.contract),
                Arc::clone(&self.contract),
                cancellation.clone(),
            )),
            ToolHeaderRepairOutcome::Rejected(rejected) => {
                ManagedToolHeaderRepairOutcome::Rejected(RejectedManagedToolHeaders {
                    rejected,
                    original,
                    contract: Arc::clone(&self.contract),
                    cancellation: cancellation.clone(),
                })
            }
        })
    }
}

impl RejectedManagedToolHeaders {
    /// Collects and approves a complete replacement, then retries the unchanged
    /// invocation once. The new input/output schemas must be admitted and the
    /// original arguments must satisfy the new input schema before `approve`
    /// runs. Returning true authorizes that new definition for this call only.
    /// `review` separately approves each newly projected header binding.
    ///
    /// Every callback is guarded before and after entry. Invalidation inside a
    /// ready callback prevents subsequent host callbacks and the retry POST in
    /// that same poll. Pending refresh, dispatch and output reads also wake on
    /// invalidation. Failure, cancellation or dropping this future is terminal.
    ///
    /// The core owner retains one ID ledger, the original/narrowed deadline and
    /// cumulative byte accounting. A second rejection never creates another
    /// repair owner. Old clients remain unchanged and are never reactivated.
    pub async fn refresh_and_retry<I, A, R>(
        self,
        cx: &Cx,
        next_id: I,
        approve: A,
        review: R,
    ) -> Result<ManagedToolRepairCall, ManagedToolRepairError>
    where
        I: FnMut() -> Result<RequestId, ManagedCatalogError>,
        A: FnOnce(&FinalTool) -> bool,
        R: FnMut(&ParameterHeaderBinding) -> bool,
    {
        let bound = Box::pin(self.refresh_bound(cx, next_id, approve, review)).await?;
        Ok(bind_call(
            bound.call.into_call(),
            bound.contract,
            bound.source,
            bound.cancellation,
        ))
    }

    /// Repairs once and retains the actual retry response as a schema-bound
    /// interaction. Its first event is still unread; making it resumable sends
    /// no additional request. Use the ordinary next_event/resume/drive APIs.
    ///
    /// The approved replacement schemas and header review govern every later
    /// round. Original tool/catalog invalidation remains effective during host
    /// callbacks, pending reads and explicit continuation recovery, even after
    /// the original client handle is dropped. This does not revive that client.
    ///
    /// Only continuation and answer counts are newly configured. Original
    /// request IDs, cancellation, deadline and charged bytes cannot be reset.
    #[allow(clippy::too_many_arguments)]
    pub async fn refresh_and_start_interaction<I, A, R>(
        self,
        cx: &Cx,
        maximum_continuations: usize,
        maximum_input_responses: usize,
        next_id: I,
        approve: A,
        review: R,
    ) -> Result<ManagedToolInteraction, ManagedToolRepairError>
    where
        I: FnMut() -> Result<RequestId, ManagedCatalogError>,
        A: FnOnce(&FinalTool) -> bool,
        R: FnMut(&ParameterHeaderBinding) -> bool,
    {
        check_tool_call(cx, &self.cancellation, &self.contract)?;
        self.rejected
            .admit_interaction_limits(maximum_continuations, maximum_input_responses)?;
        let bound = Box::pin(self.refresh_bound(cx, next_id, approve, review)).await?;
        let operation =
            bound
                .call
                .into_interaction(cx, maximum_continuations, maximum_input_responses)?;
        Ok(ManagedToolInteraction::from_repaired_call(
            cx,
            operation,
            bound.contract,
            bound.cancellation,
        )?)
    }

    async fn refresh_bound<I, A, R>(
        self,
        cx: &Cx,
        mut next_id: I,
        approve: A,
        mut review: R,
    ) -> Result<BoundRepairedCall, ManagedToolRepairError>
    where
        I: FnMut() -> Result<RequestId, ManagedCatalogError>,
        A: FnOnce(&FinalTool) -> bool,
        R: FnMut(&ParameterHeaderBinding) -> bool,
    {
        let Self {
            rejected,
            original,
            contract,
            cancellation,
        } = self;
        check_tool_call(cx, &cancellation, &contract)?;
        let mut replacement = None;
        let mut admission_error = None;
        let guarded_approve = |definition: &FinalTool| match approve_definition(
            cx,
            &cancellation,
            &contract,
            &original,
            definition,
            approve,
        ) {
            Ok(Some(admitted)) => {
                replacement = Some(admitted);
                true
            }
            Ok(None) => false,
            Err(error) => {
                admission_error = Some(error);
                false
            }
        };
        let guarded_id = || supply_id(cx, &cancellation, &contract, &mut next_id);
        let guarded_review = |binding: &ParameterHeaderBinding| {
            review_binding(cx, &cancellation, &contract, &mut review, binding)
        };
        let outcome = Box::pin(await_validity(
            cx,
            &cancellation,
            &contract,
            rejected.refresh_owned(cx, guarded_id, guarded_approve, guarded_review),
        ))
        .await?;
        check_tool_call(cx, &cancellation, &contract)?;
        // A core policy refusal is only the internal stop signal for failed
        // schema admission. Preserve the actual fixed validation error.
        if let Some(error) = admission_error {
            return Err(error.into());
        }
        let call = outcome?;
        let replacement = replacement.ok_or(ManagedToolError::InvalidDefinition)?;
        let replacement = inherit_source(replacement, &contract)?;
        Ok(BoundRepairedCall {
            call,
            contract: replacement,
            source: contract,
            cancellation,
        })
    }
}

struct BoundRepairedCall {
    call: RepairedToolCall,
    contract: Arc<ToolContract>,
    source: Arc<ToolContract>,
    cancellation: McpRequestCancellation,
}

fn inherit_source(
    mut replacement: Arc<ToolContract>,
    source: &Arc<ToolContract>,
) -> Result<Arc<ToolContract>, ManagedToolError> {
    source.check()?;
    // Only a freshly admitted per-operation contract may inherit. This also
    // excludes self-cycles, shared mutation, and unbounded validity chains.
    let unique = Arc::get_mut(&mut replacement).ok_or(ManagedToolError::InvalidDefinition)?;
    if source.source_contract.is_some() || unique.source_contract.is_some() {
        return Err(ManagedToolError::InvalidDefinition);
    }
    unique.source_contract = Some(Arc::clone(source));
    unique.invalidation = source.invalidation.clone();
    replacement.check()?;
    Ok(replacement)
}

fn bind_call(
    call: ManagedCoreCall,
    contract: Arc<ToolContract>,
    source: Arc<ToolContract>,
    cancellation: McpRequestCancellation,
) -> ManagedToolRepairCall {
    ManagedToolRepairCall {
        call: ManagedToolCall {
            call: Some(call),
            contract,
            cancellation,
            finished: false,
        },
        source,
    }
}

fn approve_definition<A>(
    cx: &Cx,
    cancellation: &McpRequestCancellation,
    source: &ToolContract,
    original: &CoreRequest,
    definition: &FinalTool,
    approve: A,
) -> Result<Option<Arc<ToolContract>>, ManagedToolError>
where
    A: FnOnce(&FinalTool) -> bool,
{
    check_tool_call(cx, cancellation, source)?;
    let replacement = Arc::new(ToolContract::admit(definition.clone())?);
    replacement.validate_request(original)?;
    check_tool_call(cx, cancellation, source)?;
    let approved = approve(definition);
    check_tool_call(cx, cancellation, source)?;
    Ok(approved.then_some(replacement))
}

fn supply_id<I>(
    cx: &Cx,
    cancellation: &McpRequestCancellation,
    source: &ToolContract,
    next: &mut I,
) -> Result<RequestId, ManagedCatalogError>
where
    I: FnMut() -> Result<RequestId, ManagedCatalogError>,
{
    check_tool_call(cx, cancellation, source).map_err(|_| ManagedCatalogError::AbortedByHost)?;
    let id = next();
    check_tool_call(cx, cancellation, source).map_err(|_| ManagedCatalogError::AbortedByHost)?;
    id
}

fn review_binding<R>(
    cx: &Cx,
    cancellation: &McpRequestCancellation,
    source: &ToolContract,
    review: &mut R,
    binding: &ParameterHeaderBinding,
) -> bool
where
    R: FnMut(&ParameterHeaderBinding) -> bool,
{
    if check_tool_call(cx, cancellation, source).is_err() {
        return false;
    }
    let approved = review(binding);
    approved && check_tool_call(cx, cancellation, source).is_ok()
}

#[cfg(test)]
mod tests;

#[cfg(test)]
mod interaction_contract_tests {
    use super::*;
    use serde_json::json;
    use std::future::Future;
    use std::sync::atomic::{AtomicBool, Ordering};
    use std::task::{Context, Waker};

    fn contract() -> Arc<ToolContract> {
        Arc::new(
            ToolContract::admit(
                serde_json::from_value(json!({
                    "name":"lookup", "inputSchema":{"type":"object"}
                }))
                .unwrap(),
            )
            .unwrap(),
        )
    }

    #[test]
    fn inherited_invalidation_wakes_waiters_and_retains_weak_catalog_targets() {
        let source = contract();
        let weak = Arc::downgrade(&source);
        let replacement = inherit_source(contract(), &source).unwrap();
        let mut wait = std::pin::pin!(replacement.invalidation.cancelled());
        let mut task = Context::from_waker(Waker::noop());
        assert!(wait.as_mut().poll(&mut task).is_pending());
        drop(source);
        let retained = weak
            .upgrade()
            .expect("catalog's weak target must stay alive");
        retained.invalidate();
        assert!(wait.as_mut().poll(&mut task).is_ready());
        assert!(matches!(
            replacement.check(),
            Err(ManagedToolError::Invalidated)
        ));
        assert!(replacement.is_invalidated());
    }

    #[test]
    fn inherited_checks_observe_catalog_and_same_poll_source_retirement() {
        let flag = Arc::new(AtomicBool::new(false));
        let mut source = contract();
        Arc::get_mut(&mut source).unwrap().catalog_invalidated = Some(Arc::clone(&flag));
        let replacement = inherit_source(contract(), &source).unwrap();
        flag.store(true, Ordering::Release);
        assert!(matches!(
            replacement.check(),
            Err(ManagedToolError::Invalidated)
        ));
        let source = contract();
        let replacement = inherit_source(contract(), &source).unwrap();
        source.invalidated.store(true, Ordering::Release);
        assert!(matches!(
            replacement.check(),
            Err(ManagedToolError::Invalidated)
        ));
    }

    #[test]
    fn inheritance_refuses_shared_replacements_cycles_chains_and_stale_sources() {
        let source = contract();
        assert!(matches!(
            inherit_source(Arc::clone(&source), &source),
            Err(ManagedToolError::InvalidDefinition)
        ));
        let first = inherit_source(contract(), &source).unwrap();
        assert!(matches!(
            inherit_source(contract(), &first),
            Err(ManagedToolError::InvalidDefinition)
        ));
        source.invalidate();
        assert!(matches!(
            inherit_source(contract(), &source),
            Err(ManagedToolError::Invalidated)
        ));
        let independent = contract();
        independent.check().unwrap();
        assert!(!independent.invalidation.is_cancel_requested());
    }
}