fastmcp-client 0.12.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
//! High-level MCP calls under a shared, rotating OAuth grant.
//!
//! Each logical operation obtains a current managed credential before using the
//! ordinary `HttpClient`. A new credential generation creates a new discovery
//! connection and cache; old catalog entries cannot survive a scope change.
//! Renewal happens before the operation, never in response to an HTTP error.
//!
//! Discovery, schema repair, reverse handlers, and the completed response all
//! share the operation's deadline and the original token's lifetime. There is
//! deliberately no accessor for the raw client or an escaping response stream.
//! Dropping a polled operation drops its connection rather than returning
//! possibly half-consumed state to the next operation.

use std::collections::HashMap;
use std::fmt;
use std::time::Duration;

use asupersync::Cx;
use fastmcp_core::{CanonicalHttpUrl, McpErrorCode, McpRequestCancellation};
use fastmcp_protocol::CoreResult;
use fastmcp_protocol::http_headers::ParameterHeaderBinding;
use serde_json::Value;

use super::{ManagedOAuthSession, OAuthSessionError, deadline_after};
use crate::http_executor::parameter_headers::ReviewedToolHeaders;
use crate::{ClientBuilder, HttpClient, HttpClientError, ProtocolPolicy};

mod cursors;
use cursors::{CatalogKind, CursorLedger};

/// Sanitized high-level failures. The original session error remains available
/// for typed handling, but formatting never includes transport or peer data.
pub enum ManagedHttpClientError {
    InvalidPolicy,
    Closed,
    Session(OAuthSessionError),
    Connection,
    Request {
        code: Option<McpErrorCode>,
    },
    /// A cursor cannot cross a reconnect or a credential-generation change.
    CatalogGenerationChanged,
    /// The cursor was not issued by this client's current catalog traversal.
    InvalidCatalogCursor,
    /// A peer cursor exceeded the local retention bound or fresh custody failed.
    CatalogCursorUnavailable,
}

impl fmt::Debug for ManagedHttpClientError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::InvalidPolicy => f.write_str("ManagedHttpClientError::InvalidPolicy"),
            Self::Closed => f.write_str("ManagedHttpClientError::Closed"),
            Self::Session(_) => f.write_str("ManagedHttpClientError::Session(..)"),
            Self::Connection => f.write_str("ManagedHttpClientError::Connection"),
            Self::Request { code } => f
                .debug_struct("ManagedHttpClientError::Request")
                .field("code", code)
                .finish(),
            Self::CatalogGenerationChanged => {
                f.write_str("ManagedHttpClientError::CatalogGenerationChanged")
            }
            Self::InvalidCatalogCursor => {
                f.write_str("ManagedHttpClientError::InvalidCatalogCursor")
            }
            Self::CatalogCursorUnavailable => {
                f.write_str("ManagedHttpClientError::CatalogCursorUnavailable")
            }
        }
    }
}

impl fmt::Display for ManagedHttpClientError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(match self {
            Self::InvalidPolicy => "invalid managed HTTP client policy",
            Self::Closed => "managed HTTP client is closed",
            Self::Session(_) => "managed OAuth operation failed",
            Self::Connection => "authenticated MCP discovery failed",
            Self::Request { .. } => "authenticated MCP request failed; not replayed",
            Self::CatalogGenerationChanged => {
                "catalog credential generation changed; restart listing without a cursor"
            }
            Self::InvalidCatalogCursor => {
                "catalog cursor is stale, unissued, or belongs to another catalog"
            }
            Self::CatalogCursorUnavailable => {
                "catalog continuation could not be retained within local bounds"
            }
        })
    }
}

impl std::error::Error for ManagedHttpClientError {}

impl From<OAuthSessionError> for ManagedHttpClientError {
    fn from(error: OAuthSessionError) -> Self {
        Self::Session(error)
    }
}

fn request_error(error: HttpClientError) -> ManagedHttpClientError {
    let code = match error {
        HttpClientError::CoreResult(error) => Some(error.code),
        _ => None,
    };
    // Messages/data can reflect tokens, resource names, or private inputs.
    // Keep only the typed protocol error code, not the original diagnostics.
    ManagedHttpClientError::Request { code }
}

struct Connection {
    client: HttpClient,
    generation: u64,
    cursors: CursorLedger,
}

/// A reusable high-level HTTP client whose authentication is caller-owned.
///
/// `new` performs no I/O. The first operation performs authenticated discovery.
/// Further operations reuse its connection only while the credential generation
/// is unchanged. Acquisition uses the session's existing bounded single-flight
/// renewal, so independent clients sharing that session share one refresh.
///
/// Resource TLS trust belongs to the supplied `ClientBuilder`; configure
/// `http_resource_root_certificate` there for private-resource deployments.
/// Token-issuer roots do not grant resource trust. The builder's bearer, if any,
/// is replaced by this explicitly selected session before every connection.
/// `Auto` and legacy plans are rejected before acquiring a grant.
///
/// The wrapper accepts completed core calls only. Subscription streams, Tasks
/// response owners, and arbitrary callbacks into a raw `HttpClient` are not
/// exposed, because those could escape the credential lifetime guard.
///
/// Returned catalog cursors are opaque, single-use LOCAL handles, not the
/// upstream cursor bytes. Pass them back only to the same client's same list
/// method. One pending continuation is retained per catalog kind; starting or
/// advancing that list supersedes its previous handle. Renewal, disconnection,
/// and failed or abandoned operations retire all handles. An unrelated call
/// cannot make an old cursor valid again, even if a peer reuses its cursor text.
pub struct ManagedHttpClient {
    session: ManagedOAuthSession,
    builder: ClientBuilder,
    operation_timeout: Duration,
    connection: Option<Connection>,
    closed: bool,
}

impl fmt::Debug for ManagedHttpClient {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("ManagedHttpClient")
            .field("closed", &self.closed)
            .field("cached_generation", &self.cached_credential_generation())
            .field("operation_timeout", &self.operation_timeout)
            .finish_non_exhaustive()
    }
}

fn admit_builder(
    builder: &ClientBuilder,
    resource: &CanonicalHttpUrl,
    timeout: Duration,
) -> Result<(), ManagedHttpClientError> {
    let plan = builder.selected_protocol_plan();
    if timeout.is_zero()
        || timeout > Duration::from_mins(15)
        || resource.scheme() != "https"
        || plan.policy() != ProtocolPolicy::ModernOnly
        || plan.modern_post_target() != Some(resource.as_str())
    {
        return Err(ManagedHttpClientError::InvalidPolicy);
    }
    // The public side-effect-free admission path also checks the compiled
    // protocol feature surface. No discovery, refresh, or callback runs here.
    builder
        .http_negotiation()
        .map_err(|_| ManagedHttpClientError::InvalidPolicy)?;
    Ok(())
}

impl ManagedHttpClient {
    /// Selects one managed login and exact ModernOnly resource before any I/O.
    /// The finite timeout covers acquisition, discovery, and the whole logical
    /// operation, including MRTR and reviewed parameter-header repair.
    pub fn new(
        session: ManagedOAuthSession,
        builder: ClientBuilder,
        operation_timeout: Duration,
    ) -> Result<Self, ManagedHttpClientError> {
        admit_builder(&builder, session.resource(), operation_timeout)?;
        Ok(Self {
            session,
            builder,
            operation_timeout,
            connection: None,
            closed: false,
        })
    }

    pub fn resource(&self) -> &CanonicalHttpUrl {
        self.session.resource()
    }

    /// Introspection only: generation is local to the selected OAuth session.
    pub fn cached_credential_generation(&self) -> Option<u64> {
        self.connection
            .as_ref()
            .map(|connection| connection.generation)
    }

    /// Permanently closes this client, not sibling clients sharing its login.
    /// To revoke the shared login as well, close its `ManagedOAuthSession`.
    pub fn close(&mut self) {
        self.closed = true;
        self.connection = None;
    }

    pub async fn list_tools(
        &mut self,
        cx: &Cx,
        cursor: Option<&str>,
    ) -> Result<CoreResult, ManagedHttpClientError> {
        self.list_tools_with_cancellation(cx, &McpRequestCancellation::new(), cursor)
            .await
    }

    pub async fn list_tools_with_cancellation(
        &mut self,
        cx: &Cx,
        cancellation: &McpRequestCancellation,
        cursor: Option<&str>,
    ) -> Result<CoreResult, ManagedHttpClientError> {
        self.perform(cx, cancellation, Call::Tools(cursor)).await
    }

    pub async fn list_resources(
        &mut self,
        cx: &Cx,
        cursor: Option<&str>,
    ) -> Result<CoreResult, ManagedHttpClientError> {
        self.list_resources_with_cancellation(cx, &McpRequestCancellation::new(), cursor)
            .await
    }

    pub async fn list_resources_with_cancellation(
        &mut self,
        cx: &Cx,
        cancellation: &McpRequestCancellation,
        cursor: Option<&str>,
    ) -> Result<CoreResult, ManagedHttpClientError> {
        self.perform(cx, cancellation, Call::Resources(cursor))
            .await
    }

    pub async fn list_resource_templates(
        &mut self,
        cx: &Cx,
        cursor: Option<&str>,
    ) -> Result<CoreResult, ManagedHttpClientError> {
        self.list_resource_templates_with_cancellation(cx, &McpRequestCancellation::new(), cursor)
            .await
    }

    pub async fn list_resource_templates_with_cancellation(
        &mut self,
        cx: &Cx,
        cancellation: &McpRequestCancellation,
        cursor: Option<&str>,
    ) -> Result<CoreResult, ManagedHttpClientError> {
        self.perform(cx, cancellation, Call::Templates(cursor))
            .await
    }

    pub async fn list_prompts(
        &mut self,
        cx: &Cx,
        cursor: Option<&str>,
    ) -> Result<CoreResult, ManagedHttpClientError> {
        self.list_prompts_with_cancellation(cx, &McpRequestCancellation::new(), cursor)
            .await
    }

    pub async fn list_prompts_with_cancellation(
        &mut self,
        cx: &Cx,
        cancellation: &McpRequestCancellation,
        cursor: Option<&str>,
    ) -> Result<CoreResult, ManagedHttpClientError> {
        self.perform(cx, cancellation, Call::Prompts(cursor)).await
    }

    pub async fn call_tool(
        &mut self,
        cx: &Cx,
        name: &str,
        arguments: Value,
    ) -> Result<CoreResult, ManagedHttpClientError> {
        self.call_tool_with_cancellation(cx, &McpRequestCancellation::new(), name, arguments)
            .await
    }

    pub async fn call_tool_with_cancellation(
        &mut self,
        cx: &Cx,
        cancellation: &McpRequestCancellation,
        name: &str,
        arguments: Value,
    ) -> Result<CoreResult, ManagedHttpClientError> {
        self.perform(cx, cancellation, Call::Tool { name, arguments })
            .await
    }

    pub async fn call_tool_with_reviewed_headers(
        &mut self,
        cx: &Cx,
        arguments: Value,
        reviewed: &ReviewedToolHeaders,
        review: &(dyn Fn(&ParameterHeaderBinding) -> bool + Send + Sync),
    ) -> Result<CoreResult, ManagedHttpClientError> {
        self.call_tool_with_reviewed_headers_and_cancellation(
            cx,
            &McpRequestCancellation::new(),
            arguments,
            reviewed,
            review,
        )
        .await
    }

    pub async fn call_tool_with_reviewed_headers_and_cancellation(
        &mut self,
        cx: &Cx,
        cancellation: &McpRequestCancellation,
        arguments: Value,
        reviewed: &ReviewedToolHeaders,
        review: &(dyn Fn(&ParameterHeaderBinding) -> bool + Send + Sync),
    ) -> Result<CoreResult, ManagedHttpClientError> {
        // A mismatching disclosure plan is not a reason to spend a refresh
        // token or open a connection before the ordinary client rejects it.
        if reviewed.resource() != self.session.resource() {
            return Err(ManagedHttpClientError::InvalidPolicy);
        }
        self.perform(
            cx,
            cancellation,
            Call::Reviewed {
                arguments,
                reviewed,
                review,
            },
        )
        .await
    }

    pub async fn read_resource(
        &mut self,
        cx: &Cx,
        uri: &str,
    ) -> Result<CoreResult, ManagedHttpClientError> {
        self.read_resource_with_cancellation(cx, &McpRequestCancellation::new(), uri)
            .await
    }

    pub async fn read_resource_with_cancellation(
        &mut self,
        cx: &Cx,
        cancellation: &McpRequestCancellation,
        uri: &str,
    ) -> Result<CoreResult, ManagedHttpClientError> {
        self.perform(cx, cancellation, Call::Read(uri)).await
    }

    pub async fn get_prompt(
        &mut self,
        cx: &Cx,
        name: &str,
        arguments: HashMap<String, String>,
    ) -> Result<CoreResult, ManagedHttpClientError> {
        self.get_prompt_with_cancellation(cx, &McpRequestCancellation::new(), name, arguments)
            .await
    }

    pub async fn get_prompt_with_cancellation(
        &mut self,
        cx: &Cx,
        cancellation: &McpRequestCancellation,
        name: &str,
        arguments: HashMap<String, String>,
    ) -> Result<CoreResult, ManagedHttpClientError> {
        self.perform(cx, cancellation, Call::Prompt { name, arguments })
            .await
    }

    async fn perform(
        &mut self,
        cx: &Cx,
        cancellation: &McpRequestCancellation,
        call: Call<'_>,
    ) -> Result<CoreResult, ManagedHttpClientError> {
        if self.closed {
            return Err(ManagedHttpClientError::Closed);
        }
        self.session.check(cx, cancellation)?;
        let deadline = deadline_after(cx, self.operation_timeout)?;
        let catalog = call.catalog();
        let continuation = catalog.is_some_and(|(_, cursor)| cursor.is_some());
        if let Some((kind, Some(cursor))) = catalog {
            let previous = self
                .connection
                .as_ref()
                .ok_or(ManagedHttpClientError::CatalogGenerationChanged)?;
            // Refuse unknown/cross-catalog handles before acquiring a token.
            // A newer connection alone cannot establish a cursor's provenance.
            if !previous.cursors.admits(kind, cursor) {
                return Err(ManagedHttpClientError::InvalidCatalogCursor);
            }
        }

        // Taking custody before the first await makes abandonment terminal for
        // this connection. A subsequent explicit operation must discover anew.
        let previous = self.connection.take();
        let session = self.session.clone();
        let builder = self.builder.clone();
        let guarded = session.await_active(
            cx,
            cancellation,
            deadline,
            None,
            Box::pin(async {
                let snapshot = session
                    .credential_with_cancellation(cx, cancellation)
                    .await?;
                let generation = snapshot.generation();
                if continuation
                    && previous
                        .as_ref()
                        .is_none_or(|old| old.generation != generation)
                {
                    return Ok(Err(ManagedHttpClientError::CatalogGenerationChanged));
                }
                // Scope/credential changes invalidate the entire ordinary client,
                // including its discovery capabilities and response/catalog caches.
                let reusable = previous.filter(|old| old.generation == generation);
                session
                    .await_credential(
                        cx,
                        cancellation,
                        deadline,
                        snapshot.expires_at(),
                        &snapshot.credential.revoked,
                        Box::pin(async {
                            let (mut client, mut cursors) = match reusable {
                                Some(connection) => (connection.client, connection.cursors),
                                None => match builder
                                    .http_bearer_credential(snapshot.credential().clone())
                                    .connect_http_client_with_cx(cx)
                                    .await
                                {
                                    Ok(client) => (client, CursorLedger::default()),
                                    Err(_) => return Ok(Err(ManagedHttpClientError::Connection)),
                                },
                            };
                            let wire_cursor = match catalog {
                                Some((kind, cursor)) => match cursors.take(kind, cursor) {
                                    Ok(cursor) => cursor,
                                    Err(error) => return Ok(Err(error)),
                                },
                                None => None,
                            };
                            let result = call
                                .dispatch(cx, cancellation, &mut client, wire_cursor.as_deref())
                                .await;
                            let mut value = match result {
                                Ok(value) => value,
                                Err(error) => return Ok(Err(request_error(error))),
                            };
                            if let Some((kind, _)) = catalog {
                                if let Err(error) = cursors.publish(kind, &mut value) {
                                    return Ok(Err(error));
                                }
                            }
                            Ok(Ok((
                                value,
                                Connection {
                                    client,
                                    generation,
                                    cursors,
                                },
                            )))
                        }),
                    )
                    .await
            }),
        );
        // Heap the outer guard rather than awaiting it inline. `await_active`
        // wraps the whole two-layer credential guard, and `perform` holds its
        // frame inline -- which every one of the fourteen public verbs then
        // paid at its own `.await` (bd-y2xoc). Boxing here keeps both this fn
        // and those verbs private/unchanged in signature.
        //
        // Note for anyone tempted to box `Call::dispatch` instead: that was
        // measured and is a NO-OP, because dispatch already runs inside the
        // `Box::pin(async { .. })` closures above, so boxing it just nests one
        // allocation inside another. Every future size came back byte-identical.
        let guarded = Box::pin(guarded).await?;
        let (value, connection) = guarded?;
        // Both managed guards have made their final cancellation/expiry checks.
        // Failed or abandoned requests never restore their connection or cache.
        self.connection = Some(connection);
        Ok(value)
    }
}

enum Call<'a> {
    Tools(Option<&'a str>),
    Resources(Option<&'a str>),
    Templates(Option<&'a str>),
    Prompts(Option<&'a str>),
    Tool {
        name: &'a str,
        arguments: Value,
    },
    Reviewed {
        arguments: Value,
        reviewed: &'a ReviewedToolHeaders,
        review: &'a (dyn Fn(&ParameterHeaderBinding) -> bool + Send + Sync),
    },
    Read(&'a str),
    Prompt {
        name: &'a str,
        arguments: HashMap<String, String>,
    },
}

impl<'a> Call<'a> {
    fn catalog(&self) -> Option<(CatalogKind, Option<&'a str>)> {
        match self {
            Self::Tools(cursor) => Some((CatalogKind::Tools, *cursor)),
            Self::Resources(cursor) => Some((CatalogKind::Resources, *cursor)),
            Self::Templates(cursor) => Some((CatalogKind::Templates, *cursor)),
            Self::Prompts(cursor) => Some((CatalogKind::Prompts, *cursor)),
            _ => None,
        }
    }

    async fn dispatch(
        self,
        cx: &Cx,
        cancellation: &McpRequestCancellation,
        client: &mut HttpClient,
        wire_cursor: Option<&str>,
    ) -> Result<CoreResult, HttpClientError> {
        match self {
            Self::Tools(_) => client.list_tools(cx, wire_cursor).await,
            Self::Resources(_) => client.list_resources(cx, wire_cursor).await,
            Self::Templates(_) => client.list_resource_templates(cx, wire_cursor).await,
            Self::Prompts(_) => client.list_prompts(cx, wire_cursor).await,
            Self::Tool { name, arguments } => {
                client
                    .call_tool_with_cancellation(cx, cancellation, name, arguments)
                    .await
            }
            Self::Reviewed {
                arguments,
                reviewed,
                review,
            } => {
                client
                    .call_tool_with_reviewed_headers_and_cancellation(
                        cx,
                        cancellation,
                        arguments,
                        reviewed,
                        review,
                    )
                    .await
            }
            Self::Read(uri) => client.read_resource(cx, uri).await,
            Self::Prompt { name, arguments } => client.get_prompt(cx, name, arguments).await,
        }
    }
}

#[cfg(test)]
mod tests;