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
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
//! Blocking completion callbacks on the same bounded caller-owned lane.

use std::sync::Arc;
use std::time::Duration;

use asupersync::Cx;
use fastmcp_core::{McpContext, McpError, McpOutcome, McpResult};
use fastmcp_protocol::{
    CompletionValues, FinalCompletionParams, FinalCompletionValues, LegacyCompletionParams,
};

use super::{BlockingHandlerLane, outcome};
use crate::handler::{BoxFuture, CompletionHandler};

/// Runs explicitly selected synchronous completion callbacks on the caller pool.
///
/// Register through the existing prompt/resource-template completion APIs, or
/// the explicitly configured fallback. Reference routing and authorization stay
/// with the router; this adapter neither adds a route nor rewrites references,
/// titles, metadata, argument values, or completion context. The exact legacy
/// and modern callback/result types remain separate.
///
/// Construction deliberately selects synchronous hooks. It does not detect
/// async overrides, and must not wrap an async-only provider or another blocking
/// adapter. There is no inline execution or privately created runtime. Share the
/// lane with other blocking handlers to bound their combined outstanding work.
pub struct BlockingCompletion<H> {
    handler: Arc<H>,
    lane: BlockingHandlerLane,
}

impl<H: CompletionHandler + 'static> BlockingCompletion<H> {
    /// Selects `complete_legacy` and `complete_final`; performs no I/O.
    pub fn from_sync_hooks(handler: H, lane: BlockingHandlerLane) -> Self {
        Self {
            handler: Arc::new(handler),
            lane,
        }
    }
}

fn async_required() -> McpError {
    McpError::invalid_request("blocking completion requires asynchronous caller-owned dispatch")
}

impl<H: CompletionHandler + 'static> CompletionHandler for BlockingCompletion<H> {
    fn timeout(&self) -> Option<Duration> {
        self.handler.timeout()
    }
    fn complete_legacy(
        &self,
        _ctx: &McpContext,
        _params: LegacyCompletionParams,
    ) -> McpResult<CompletionValues> {
        Err(async_required())
    }
    fn complete_final(
        &self,
        _ctx: &McpContext,
        _params: FinalCompletionParams,
    ) -> McpResult<FinalCompletionValues> {
        Err(async_required())
    }
    fn complete_legacy_async<'a>(
        &'a self,
        ctx: &'a McpContext,
        params: LegacyCompletionParams,
    ) -> BoxFuture<'a, McpOutcome<CompletionValues>> {
        self.complete_legacy_async_in_request(ctx, ctx.cx(), params)
    }
    fn complete_legacy_async_in_request<'a>(
        &'a self,
        ctx: &'a McpContext,
        cx: &'a Cx,
        params: LegacyCompletionParams,
    ) -> BoxFuture<'a, McpOutcome<CompletionValues>> {
        let handler = Arc::clone(&self.handler);
        Box::pin(async move {
            outcome(
                self.lane
                    .execute(ctx, cx, move |ctx| handler.complete_legacy(ctx, params))
                    .await,
            )
        })
    }
    fn complete_final_async<'a>(
        &'a self,
        ctx: &'a McpContext,
        params: FinalCompletionParams,
    ) -> BoxFuture<'a, McpOutcome<FinalCompletionValues>> {
        self.complete_final_async_in_request(ctx, ctx.cx(), params)
    }
    fn complete_final_async_in_request<'a>(
        &'a self,
        ctx: &'a McpContext,
        cx: &'a Cx,
        params: FinalCompletionParams,
    ) -> BoxFuture<'a, McpOutcome<FinalCompletionValues>> {
        let handler = Arc::clone(&self.handler);
        Box::pin(async move {
            outcome(
                self.lane
                    .execute(ctx, cx, move |ctx| handler.complete_final(ctx, params))
                    .await,
            )
        })
    }
}

#[cfg(test)]
mod tests {
    use super::super::{BlockingPrompt, BlockingResource};
    use super::*;
    use crate::handler::{PromptHandler, ResourceHandler};
    use asupersync::channel::oneshot;
    use fastmcp_core::Outcome;
    use fastmcp_protocol::{Prompt, PromptMessage, Resource, ResourceContent};
    use serde_json::{Value, json};
    use std::collections::HashMap;
    use std::future::{Future, poll_fn};
    use std::sync::Mutex;
    use std::sync::atomic::{AtomicUsize, Ordering};
    use std::task::Poll;

    type Calls = Arc<Mutex<Vec<(&'static str, Value)>>>;
    struct Completer {
        calls: Calls,
        poller: std::thread::ThreadId,
    }
    impl Completer {
        fn record(&self, ctx: &McpContext, era: &'static str, params: Value) {
            assert_ne!(std::thread::current().id(), self.poller);
            assert_eq!(ctx.request_id(), 97);
            assert_eq!(Cx::current().unwrap().task_id(), ctx.task_id());
            self.calls.lock().unwrap().push((era, params));
        }
    }
    impl CompletionHandler for Completer {
        fn timeout(&self) -> Option<Duration> {
            Some(Duration::from_secs(2))
        }
        fn complete_legacy(
            &self,
            ctx: &McpContext,
            params: LegacyCompletionParams,
        ) -> McpResult<CompletionValues> {
            self.record(ctx, "legacy", serde_json::to_value(params).unwrap());
            Ok(serde_json::from_value(json!({
                "values": ["legacy-value"],
                "total": 1,
                "hasMore": false
            }))
            .unwrap())
        }
        fn complete_final(
            &self,
            ctx: &McpContext,
            params: FinalCompletionParams,
        ) -> McpResult<FinalCompletionValues> {
            let cancel = params.argument.value == "cancel";
            self.record(ctx, "final", serde_json::to_value(params).unwrap());
            if cancel {
                ctx.request_cancellation().cancel();
            }
            Ok(serde_json::from_value(json!({
                "values": ["final-value"],
                "total": 7,
                "hasMore": true
            }))
            .unwrap())
        }
    }
    fn legacy_params() -> LegacyCompletionParams {
        serde_json::from_value(json!({
            "ref": {"type": "ref/prompt", "name": "render"},
            "argument": {"name": "body", "value": "alpha"},
            "_meta": {"progressToken": "p-97"}
        }))
        .unwrap()
    }
    fn final_params() -> FinalCompletionParams {
        serde_json::from_value(json!({
            "ref": {"type": "ref/prompt", "name": "render", "title": "Exact title"},
            "argument": {"name": "body", "value": "α\nβ"},
            "context": {"arguments": {"style": "formal", "_meta": "ordinary context value"}},
            "_meta": {"com.example/context": "kept"}
        }))
        .unwrap()
    }
    fn runtime(pool: bool) -> asupersync::runtime::Runtime {
        let builder = asupersync::runtime::RuntimeBuilder::current_thread()
            .with_reactor(asupersync::runtime::reactor::create_reactor().unwrap());
        if pool {
            builder.blocking_threads(0, 2).build().unwrap()
        } else {
            builder.build().unwrap()
        }
    }
    fn fixture(lane: BlockingHandlerLane) -> (BlockingCompletion<Completer>, Calls) {
        let calls = Arc::new(Mutex::new(Vec::new()));
        (
            BlockingCompletion::from_sync_hooks(
                Completer {
                    calls: Arc::clone(&calls),
                    poller: std::thread::current().id(),
                },
                lane,
            ),
            calls,
        )
    }

    #[test]
    fn completion_preserves_both_eras_and_all_async_entry_points_on_the_pool() {
        runtime(true).block_on(async {
            let ctx = McpContext::new(Cx::current().unwrap(), 97);
            let (completion, calls) = fixture(BlockingHandlerLane::new(1).unwrap());
            let completion: &dyn CompletionHandler = &completion;
            assert_eq!(completion.timeout(), Some(Duration::from_secs(2)));
            assert!(completion.complete_legacy(&ctx, legacy_params()).is_err());
            assert!(completion.complete_final(&ctx, final_params()).is_err());
            assert!(calls.lock().unwrap().is_empty());
            for result in [
                completion
                    .complete_legacy_async(&ctx, legacy_params())
                    .await,
                completion
                    .complete_legacy_async_in_request(&ctx, ctx.cx(), legacy_params())
                    .await,
            ] {
                let Outcome::Ok(result) = result else {
                    panic!("legacy completion expected")
                };
                assert_eq!(
                    serde_json::to_value(result).unwrap(),
                    json!({"values": ["legacy-value"], "total": 1, "hasMore": false})
                );
            }
            for result in [
                completion.complete_final_async(&ctx, final_params()).await,
                completion
                    .complete_final_async_in_request(&ctx, ctx.cx(), final_params())
                    .await,
            ] {
                let Outcome::Ok(result) = result else {
                    panic!("final completion expected")
                };
                assert_eq!(
                    serde_json::to_value(result).unwrap(),
                    json!({"values": ["final-value"], "total": 7, "hasMore": true})
                );
            }
            let calls = calls.lock().unwrap();
            assert_eq!(calls.len(), 4);
            assert_eq!(
                calls[0],
                ("legacy", serde_json::to_value(legacy_params()).unwrap())
            );
            assert_eq!(calls[1], calls[0]);
            assert_eq!(
                calls[2],
                ("final", serde_json::to_value(final_params()).unwrap())
            );
            assert_eq!(calls[3], calls[2]);
        });
    }

    #[test]
    fn completion_keeps_context_absence_empty_and_populated_states_distinct() {
        runtime(true).block_on(async {
            let ctx = McpContext::new(Cx::current().unwrap(), 97);
            let (completion, calls) = fixture(BlockingHandlerLane::new(1).unwrap());
            for context in [
                None,
                Some(json!({})),
                Some(json!({"arguments": {}})),
                Some(json!({"arguments": {"body": "x"}})),
            ] {
                let mut wire = serde_json::to_value(final_params()).unwrap();
                wire.as_object_mut().unwrap().remove("context");
                if let Some(context) = context {
                    wire["context"] = context;
                }
                let params: FinalCompletionParams = serde_json::from_value(wire.clone()).unwrap();
                assert!(matches!(
                    completion.complete_final_async(&ctx, params).await,
                    Outcome::Ok(_)
                ));
                assert_eq!(calls.lock().unwrap().last().unwrap().1, wire);
            }
            assert_eq!(calls.lock().unwrap().len(), 4);
        });
    }

    #[test]
    fn unavailable_pool_closed_lane_and_precancellation_never_invoke_completions() {
        for case in 0..3 {
            runtime(case != 0).block_on(async {
                let ctx = McpContext::new(Cx::current().unwrap(), 97);
                let lane = BlockingHandlerLane::new(1).unwrap();
                if case == 1 {
                    lane.close().unwrap();
                }
                if case == 2 {
                    ctx.request_cancellation().cancel();
                }
                let (completion, calls) = fixture(lane.clone());
                assert!(matches!(
                    completion
                        .complete_legacy_async(&ctx, legacy_params())
                        .await,
                    Outcome::Err(_)
                ));
                assert!(matches!(
                    completion.complete_final_async(&ctx, final_params()).await,
                    Outcome::Err(_)
                ));
                assert!(calls.lock().unwrap().is_empty());
                assert_eq!(lane.in_flight().unwrap(), 0);
            });
        }
    }

    #[test]
    fn completion_deadline_before_dispatch_and_cancellation_during_return_withhold_results() {
        runtime(true).block_on(async {
            let ctx = McpContext::new(Cx::current().unwrap(), 97);
            let lane = BlockingHandlerLane::new(1).unwrap();
            let (completion, calls) = fixture(lane.clone());
            let expired =
                McpContext::new(ctx.cx().clone(), 97).with_operation_deadline(Some(ctx.cx().now()));
            assert!(matches!(
                completion
                    .complete_final_async(&expired, final_params())
                    .await,
                Outcome::Err(_)
            ));
            assert!(calls.lock().unwrap().is_empty());
            let mut params = final_params();
            params.argument.value = "cancel".into();
            assert!(matches!(
                completion.complete_final_async(&ctx, params).await,
                Outcome::Err(_)
            ));
            assert_eq!(calls.lock().unwrap().len(), 1);
            let peer = McpContext::new(ctx.cx().clone(), 97);
            let drain = peer
                .clone()
                .with_operation_deadline(Some(peer.cx().now().saturating_add_nanos(5_000_000_000)));
            lane.wait_idle(&drain).await.unwrap();
            assert!(matches!(
                completion.complete_final_async(&peer, final_params()).await,
                Outcome::Ok(_)
            ));
            assert_eq!(calls.lock().unwrap().len(), 2);
        });
    }

    #[test]
    fn abandoned_prompt_keeps_the_shared_resource_and_completion_lane_charged() {
        struct HeldPrompt {
            started: Mutex<Option<oneshot::Sender<()>>>,
            release: Mutex<std::sync::mpsc::Receiver<()>>,
        }
        impl PromptHandler for HeldPrompt {
            fn definition(&self) -> Prompt {
                Prompt {
                    name: "held".into(),
                    description: None,
                    arguments: vec![],
                    icon: None,
                    version: None,
                    tags: vec![],
                }
            }
            fn get(
                &self,
                _ctx: &McpContext,
                _arguments: HashMap<String, String>,
            ) -> McpResult<Vec<PromptMessage>> {
                self.started
                    .lock()
                    .unwrap()
                    .take()
                    .unwrap()
                    .send_blocking(())
                    .unwrap();
                self.release
                    .lock()
                    .unwrap()
                    .recv_timeout(Duration::from_secs(5))
                    .unwrap();
                Ok(Vec::new())
            }
        }
        struct ReadyResource(Arc<AtomicUsize>);
        impl ResourceHandler for ReadyResource {
            fn definition(&self) -> Resource {
                Resource {
                    uri: "report://ready".into(),
                    name: "Ready".into(),
                    description: None,
                    mime_type: None,
                    icon: None,
                    version: None,
                    tags: vec![],
                }
            }
            fn read(&self, _ctx: &McpContext) -> McpResult<Vec<ResourceContent>> {
                self.0.fetch_add(1, Ordering::SeqCst);
                Ok(vec![
                    serde_json::from_value(json!({"uri": "report://ready", "text": "ready"}))
                        .unwrap(),
                ])
            }
        }
        runtime(true).block_on(async {
            let ctx = McpContext::new(Cx::current().unwrap(), 97);
            let peer = McpContext::new(ctx.cx().clone(), 97);
            let lane = BlockingHandlerLane::new(1).unwrap();
            let (started, mut entered) = oneshot::channel();
            let (release, blocked) = std::sync::mpsc::sync_channel(1);
            let prompt = BlockingPrompt::from_sync_hooks(
                HeldPrompt {
                    started: Mutex::new(Some(started)),
                    release: Mutex::new(blocked),
                },
                lane.clone(),
            )
            .unwrap();
            let observed = Arc::new(AtomicUsize::new(0));
            let resource = BlockingResource::from_sync_hooks(
                ReadyResource(Arc::clone(&observed)),
                lane.clone(),
            )
            .unwrap();
            let (completion, calls) = fixture(lane.clone());
            let mut call = prompt.get_async(&ctx, HashMap::new());
            poll_fn(|task| {
                assert!(Future::poll(call.as_mut(), task).is_pending());
                Poll::Ready(())
            })
            .await;
            entered.recv(ctx.cx()).await.unwrap();
            drop(call);
            assert_eq!(
                lane.in_flight().unwrap(),
                1,
                "dropping the waiter is not worker completion"
            );
            assert!(matches!(resource.read_async(&peer).await, Outcome::Err(_)));
            assert!(matches!(
                completion.complete_final_async(&peer, final_params()).await,
                Outcome::Err(_)
            ));
            assert_eq!(observed.load(Ordering::SeqCst), 0);
            assert!(calls.lock().unwrap().is_empty());
            assert!(peer.ensure_live().is_ok());
            release.send(()).unwrap();
            let drain = peer
                .clone()
                .with_operation_deadline(Some(peer.cx().now().saturating_add_nanos(5_000_000_000)));
            lane.wait_idle(&drain).await.unwrap();
            let Outcome::Ok(contents) = resource.read_async(&peer).await else {
                panic!("resource must recover capacity")
            };
            assert_eq!(serde_json::to_value(contents).unwrap()[0]["text"], "ready");
            assert!(matches!(
                completion.complete_final_async(&peer, final_params()).await,
                Outcome::Ok(_)
            ));
            assert_eq!(observed.load(Ordering::SeqCst), 1);
            assert_eq!(calls.lock().unwrap().len(), 1);
            lane.close().unwrap();
            assert!(matches!(
                completion
                    .complete_legacy_async(&peer, legacy_params())
                    .await,
                Outcome::Err(_)
            ));
            assert_eq!(calls.lock().unwrap().len(), 1);
        });
    }
}