fastmcp-server 0.12.0

MCP server 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
//! Machine-authenticated catalogs using the existing exact managed handlers.
//!
//! This implementation shares the private catalog constructors with dynamic
//! routing. Applications use `providers::ClientCredentialsProvider`.

mod interaction;
#[cfg(test)]
mod tests;

use std::sync::Arc;
use std::sync::atomic::AtomicU64;

use asupersync::Cx;
use fastmcp_client::http_auth::discovery::OAuthDiscoveryError;
use fastmcp_client::http_auth::discovery::client_credentials::rpc::catalog::{
    ClientCredentialsCatalogClient, ClientCredentialsCatalogError, ClientCredentialsCatalogLimits,
};
use fastmcp_client::http_auth::discovery::client_credentials::rpc::{
    ClientCredentialsCoreCall, ClientCredentialsCoreError,
};
use fastmcp_client::http_auth::discovery::client_credentials::{
    ClientCredentialsClient, ClientCredentialsError,
};
use fastmcp_client::http_auth::rpc::catalog::ManagedCatalogError;
use fastmcp_client::http_auth::rpc::{ManagedCoreEvent, ManagedCoreLimits};
use fastmcp_core::{McpContext, McpError, McpResult};
use fastmcp_protocol::{CoreRequest, CoreResult, FinalCoreResult, RequestId};
use serde_json::json;

use super::super::{
    BoxFuture, CoreBackend, Forwarder, ManagedOAuthPrompt, ManagedOAuthResource, ManagedOAuthTool,
    UNEXPECTED_RESULT, allocate_request_id, build_prompts, build_resources, build_tools, check_cx,
    core_request, forward_notification, upstream_error,
};
use super::{ManagedOAuthResourceTemplate, build_templates};
use interaction::{InteractiveMachineResponse, MachineInputs, interaction_error};

const MACHINE_FAILURE: &str = "Machine-authenticated upstream request failed";
const CATALOG_FAILURE: &str = "Machine-authenticated upstream catalog acquisition failed";

/// Expose an explicitly provisioned machine identity through ordinary MCP handlers.
///
/// Unlike `ManagedOAuthProvider`, this does not require an interactive login.
/// Supply an already configured `ClientCredentialsClient`: its issuer, endpoint,
/// scopes, authentication method, credential expiry and revocation remain its
/// own authority. All supported machine authentication methods use the same path.
/// Each upstream operation performs that client's same-token authenticated
/// discovery before dispatch; neither this provider nor its catalog bypasses it.
///
/// This is service-account delegation, NOT on-behalf-of authentication. Protect
/// the gateway with its own authorization policy and register only capabilities
/// downstream callers may exercise as this machine. Downstream credentials and
/// metadata are never used as upstream HTTP headers. Nested tool arguments stay
/// application data. A provider/handler clone keeps the credential owner alive;
/// the application's explicit client close still retires that shared owner.
///
/// Catalog acquisition returns a whole collection or an error, never partial
/// handlers. Each category has its own bounded collection; separate acquisitions
/// are not an atomic multi-category snapshot. Namespace rewriting, schema
/// authority, exact results, resource URI matching and completion routing reuse
/// the same handlers as the interactive provider, rather than a parallel router.
///
/// By default only complete modern core results are forwarded. Explicitly
/// installing `with_input_handler` enables bounded host-resolved input-required
/// rounds, not transparent downstream continuation state. Neither path installs
/// legacy execution, Tasks or subscriptions. No failed POST is retried and no
/// runtime is created.
///
/// ```ignore
/// use fastmcp_server::providers::ClientCredentialsProvider;
/// let provider = ClientCredentialsProvider::new(machine_client).with_namespace("service")?;
/// for tool in provider.tools(cx).await? {
///     builder = builder.tool(tool);
/// }
/// for prompt in provider.prompts(cx).await? {
///     let name = prompt.catalog_definition().name.clone();
///     let completion = prompt.completion_handler()?;
///     builder = builder.prompt(prompt).prompt_completion_handler(name, completion);
/// }
/// ```
#[derive(Clone)]
pub struct ClientCredentialsProvider {
    source: Arc<dyn MachineSource>,
    forwarder: Arc<Forwarder>,
    catalog_limits: ClientCredentialsCatalogLimits,
    namespace: Option<String>,
}

impl ClientCredentialsProvider {
    /// Construction performs no discovery, grant acquisition or network work.
    pub fn new(client: ClientCredentialsClient) -> Self {
        Self::from_source(Arc::new(NativeMachineSource(client)))
    }

    fn from_source(source: Arc<dyn MachineSource>) -> Self {
        let next_id = Arc::new(AtomicU64::new(1));
        Self {
            forwarder: Arc::new(Forwarder {
                backend: Arc::new(MachineBackend {
                    source: Arc::clone(&source),
                    next_id: Arc::clone(&next_id),
                    inputs: None,
                }),
                next_id,
                limits: ManagedCoreLimits::default(),
            }),
            source,
            catalog_limits: ClientCredentialsCatalogLimits::default(),
            namespace: None,
        }
    }

    /// Policies are already validated by the native client. Existing clones and
    /// registered handlers retain their policy. All clones still share one ID
    /// allocator for BOTH discovery and operation IDs, including catalog pages.
    /// An explicitly installed input handler is retained with the new limits.
    pub fn with_limits(
        mut self,
        calls: ManagedCoreLimits,
        catalogs: ClientCredentialsCatalogLimits,
    ) -> Self {
        self.forwarder = Arc::new(Forwarder {
            backend: Arc::clone(&self.forwarder.backend),
            next_id: Arc::clone(&self.forwarder.next_id),
            limits: calls,
        });
        self.catalog_limits = catalogs;
        self
    }

    /// Prefix local tool/prompt names only. Resource identities and upstream
    /// request names are unchanged. Invalid namespaces fail before any grant.
    pub fn with_namespace(mut self, namespace: impl Into<String>) -> McpResult<Self> {
        let namespace = namespace.into();
        if namespace.is_empty()
            || namespace.len() > 64
            || !namespace
                .bytes()
                .all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'_' | b'-' | b'.'))
        {
            return Err(McpError::invalid_params(
                "Invalid machine provider namespace",
            ));
        }
        self.namespace = Some(namespace);
        Ok(self)
    }

    /// Fully collect and validate the tool catalog before exposing handlers.
    /// Exact upstream input/output schemas remain the normal proxy authority.
    pub async fn tools(&self, cx: &Cx) -> McpResult<Vec<ManagedOAuthTool>> {
        let mut entries = Vec::new();
        for page in self.catalog(cx, "tools/list").await? {
            let CoreResult::Final(FinalCoreResult::ToolsList { result, .. }) = page else {
                return Err(McpError::invalid_request(UNEXPECTED_RESULT));
            };
            entries.extend(result.payload.tools);
        }
        build_tools(
            Arc::clone(&self.forwarder),
            entries,
            self.namespace.as_deref(),
        )
    }

    /// Fully collect concrete resources. Duplicate URIs reject the whole result.
    /// Reads remain MCP operations against the configured endpoint, not URL fetches.
    pub async fn resources(&self, cx: &Cx) -> McpResult<Vec<ManagedOAuthResource>> {
        let mut entries = Vec::new();
        for page in self.catalog(cx, "resources/list").await? {
            let CoreResult::Final(FinalCoreResult::ResourcesList { result, .. }) = page else {
                return Err(McpError::invalid_request(UNEXPECTED_RESULT));
            };
            entries.extend(result.payload.resources);
        }
        build_resources(Arc::clone(&self.forwarder), entries)
    }

    /// Fully collect prompts, retaining exact argument and catalog metadata.
    /// Returned prompts also provide their route-bound `completion_handler()`.
    pub async fn prompts(&self, cx: &Cx) -> McpResult<Vec<ManagedOAuthPrompt>> {
        let mut entries = Vec::new();
        for page in self.catalog(cx, "prompts/list").await? {
            let CoreResult::Final(FinalCoreResult::PromptsList { result, .. }) = page else {
                return Err(McpError::invalid_request(UNEXPECTED_RESULT));
            };
            entries.extend(result.payload.prompts);
        }
        build_prompts(
            Arc::clone(&self.forwarder),
            entries,
            self.namespace.as_deref(),
        )
    }

    /// Fully collect reversible resource templates, rejecting duplicate,
    /// client-direct HTTPS and non-reversible routes before exposing any handler.
    /// Returned templates also provide their route-bound completion handler.
    pub async fn resource_templates(
        &self,
        cx: &Cx,
    ) -> McpResult<Vec<ManagedOAuthResourceTemplate>> {
        let mut entries = Vec::new();
        for page in self.catalog(cx, "resources/templates/list").await? {
            let CoreResult::Final(FinalCoreResult::ResourceTemplatesList { result, .. }) = page
            else {
                return Err(McpError::invalid_request(UNEXPECTED_RESULT));
            };
            entries.extend(result.payload.resource_templates);
        }
        build_templates(Arc::clone(&self.forwarder), entries)
    }

    async fn catalog(&self, cx: &Cx, method: &'static str) -> McpResult<Vec<CoreResult>> {
        check_cx(cx)?;
        let result = self
            .source
            .collect(cx, method, &self.forwarder.next_id, self.catalog_limits)
            .await;
        check_cx(cx)?;
        result
    }
}

// Both seams are private. The only production constructor installs the native
// machine client; synthetic catalogs cannot mint public schema registrations.
struct MachineCall {
    request: CoreRequest,
    discovery_id: RequestId,
    request_id: RequestId,
    limits: ManagedCoreLimits,
    inputs: Option<Arc<MachineInputs>>,
}

trait MachineSource: Send + Sync {
    fn collect<'a>(
        &'a self,
        cx: &'a Cx,
        method: &'static str,
        ids: &'a AtomicU64,
        limits: ClientCredentialsCatalogLimits,
    ) -> BoxFuture<'a, McpResult<Vec<CoreResult>>>;

    fn start<'a>(
        &'a self,
        ctx: &'a McpContext,
        cx: &'a Cx,
        call: MachineCall,
    ) -> BoxFuture<'a, McpResult<Box<dyn MachineResponse>>>;
}

trait MachineResponse: Send {
    fn next_event<'a>(
        &'a mut self,
        cx: &'a Cx,
    ) -> BoxFuture<'a, McpResult<Option<ManagedCoreEvent>>>;
}

struct NativeMachineSource(ClientCredentialsClient);

impl MachineSource for NativeMachineSource {
    fn collect<'a>(
        &'a self,
        cx: &'a Cx,
        method: &'static str,
        ids: &'a AtomicU64,
        limits: ClientCredentialsCatalogLimits,
    ) -> BoxFuture<'a, McpResult<Vec<CoreResult>>> {
        Box::pin(async move {
            let request = core_request(method, json!({}), None)?;
            let collector = ClientCredentialsCatalogClient::new(self.0.clone(), limits);
            let pages = collector
                .collect(
                    cx,
                    request,
                    || {
                        next_pair(ids).map_err(|_| {
                            ClientCredentialsCatalogError::Catalog(
                                ManagedCatalogError::AbortedByHost,
                            )
                        })
                    },
                    // Empty capability metadata grants no notification stream.
                    // Invalidation during collection must not publish a partial catalog.
                    |_| {
                        Err(ClientCredentialsCatalogError::Catalog(
                            ManagedCatalogError::AbortedByHost,
                        ))
                    },
                )
                .await
                .map_err(catalog_error)?;
            Ok(pages.into_pages())
        })
    }

    fn start<'a>(
        &'a self,
        ctx: &'a McpContext,
        cx: &'a Cx,
        call: MachineCall,
    ) -> BoxFuture<'a, McpResult<Box<dyn MachineResponse>>> {
        Box::pin(async move {
            let MachineCall {
                request,
                discovery_id,
                request_id,
                limits,
                inputs,
            } = call;
            let cancellation = ctx.request_cancellation();
            if let Some(inputs) = inputs {
                let operation = self
                    .0
                    .start_core_interaction_with_cancellation(
                        cx,
                        &cancellation,
                        request,
                        discovery_id,
                        request_id,
                        inputs.policy.limits(limits)?,
                    )
                    .await
                    .map_err(interaction_error)?;
                // Preserve request identity/auth/quota/lease while giving host
                // callbacks the exact Cx selected by request-owned dispatch.
                return Ok(Box::new(InteractiveMachineResponse::new(
                    operation,
                    ctx.clone().with_request_cx(cx.clone()),
                    inputs,
                )) as Box<dyn MachineResponse>);
            }
            let call = self
                .0
                .request_core_with_cancellation(
                    cx,
                    &cancellation,
                    request,
                    discovery_id,
                    request_id,
                    limits,
                )
                .await
                .map_err(machine_error)?;
            Ok(Box::new(NativeMachineResponse(call)) as Box<dyn MachineResponse>)
        })
    }
}

struct NativeMachineResponse(ClientCredentialsCoreCall);
impl MachineResponse for NativeMachineResponse {
    fn next_event<'a>(
        &'a mut self,
        cx: &'a Cx,
    ) -> BoxFuture<'a, McpResult<Option<ManagedCoreEvent>>> {
        Box::pin(async move { self.0.next_event(cx).await.map_err(machine_error) })
    }
}

struct MachineBackend {
    source: Arc<dyn MachineSource>,
    next_id: Arc<AtomicU64>,
    inputs: Option<Arc<MachineInputs>>,
}

impl CoreBackend for MachineBackend {
    fn execute<'a>(
        &'a self,
        ctx: &'a McpContext,
        cx: &'a Cx,
        request: CoreRequest,
        id: RequestId,
        limits: ManagedCoreLimits,
    ) -> BoxFuture<'a, McpResult<FinalCoreResult>> {
        Box::pin(async move {
            ctx.checkpoint()?;
            check_cx(cx)?;
            let (request, inputs) = match &self.inputs {
                Some(inputs) => match inputs.policy.select_request(&request)? {
                    Some(selected) => (selected, Some(Arc::clone(inputs))),
                    // Completion is not an input-required method. Do not send
                    // extra capabilities or disable it when a host is installed.
                    None => (request, None),
                },
                None => (request, None),
            };
            // The operation's ID already came from Forwarder. Discovery needs
            // another ID from that same allocator, never a fixed or reused ID.
            let discovery_id = allocate_request_id(&self.next_id)?;
            let mut response = self
                .source
                .start(
                    ctx,
                    cx,
                    MachineCall {
                        request,
                        discovery_id,
                        request_id: id,
                        limits,
                        inputs,
                    },
                )
                .await?;
            loop {
                ctx.checkpoint()?;
                check_cx(cx)?;
                let event = response.next_event(cx).await?;
                // Do not publish progress or terminal data after cancellation.
                ctx.checkpoint()?;
                check_cx(cx)?;
                match event {
                    Some(ManagedCoreEvent::Notification(notification)) => {
                        forward_notification(ctx, *notification)?;
                    }
                    Some(ManagedCoreEvent::Result(result)) => match *result {
                        CoreResult::Final(result) => return Ok(result),
                        _ => return Err(McpError::invalid_request(UNEXPECTED_RESULT)),
                    },
                    None => return Err(McpError::invalid_request(MACHINE_FAILURE)),
                }
            }
        })
    }
}

fn next_pair(ids: &AtomicU64) -> McpResult<(RequestId, RequestId)> {
    // If the second allocation fails, the first is deliberately not reusable.
    Ok((allocate_request_id(ids)?, allocate_request_id(ids)?))
}

fn machine_error(error: ClientCredentialsCoreError) -> McpError {
    match error {
        ClientCredentialsCoreError::Protocol(error) => upstream_error(error),
        ClientCredentialsCoreError::Authentication(ClientCredentialsError::Discovery(
            OAuthDiscoveryError::Cancelled | OAuthDiscoveryError::TimedOut,
        )) => McpError::request_cancelled(),
        ClientCredentialsCoreError::Authentication(_) => McpError::invalid_request(MACHINE_FAILURE),
    }
}

fn catalog_error(error: ClientCredentialsCatalogError) -> McpError {
    match error {
        ClientCredentialsCatalogError::Core(error) => machine_error(error),
        ClientCredentialsCatalogError::Catalog(_) => McpError::invalid_request(CATALOG_FAILURE),
    }
}