tools-kit 0.0.1

Portable Local and MCP tool execution with narrow extension traits.
Documentation
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
mod serving;
mod sessions;

use std::{sync::Arc, time::Duration};

use axum::{
    Router,
    extract::Request,
    http::{StatusCode, header},
    middleware::{self, Next},
    response::{IntoResponse, Response},
};
use rmcp::{
    ErrorData as McpError, RoleServer, ServerHandler,
    model::{
        CallToolRequestParams, CallToolResult, ClientResult, CreateElicitationRequestParams,
        ElicitationAction, ElicitationSchema, ListToolsResult, PaginatedRequestParams,
        ServerCapabilities, ServerInfo, ServerRequest,
    },
    service::{ElicitationMode, PeerRequestOptions, RequestContext},
    transport::streamable_http_server::{StreamableHttpServerConfig, StreamableHttpService},
};
use tokio::{net::TcpListener, time};
use url::{Host, Url};

use crate::{
    ToolError, ToolHandlerBackend, ToolsetBackend,
    declaration::ToolInfo,
    execution::{ToolHandlerInput, ToolHandlerOutput, ToolInvocation},
    interaction::{ToolInteractionKind, ToolInteractionRequest, ToolInteractionResponse},
    mcp::{McpServerConfig, McpServerInteractionPolicy},
    name::ToolName,
    sources::mcp::catalog::McpProtocol,
};

use self::{
    serving::Serving,
    sessions::{Protocol, Sessions},
};

const LIST_TOOLS_PAGE_SIZE: usize = 64;
const INTERACTION_TIMEOUT: Duration = Duration::from_secs(30);
const INTERACTION_CLEANUP_TIMEOUT: Duration = Duration::from_secs(1);

/// Independently owned MCP server serving any concrete [`ToolsetBackend`].
///
/// The server retains a shared reference to the caller's toolset. Server shutdown
/// never shuts down that toolset; the caller owns its cleanup separately.
/// This example binds a real loopback endpoint and requires an available port.
/// Keep the Tokio runtime driven until both server and toolset cleanup settle.
/// Dropping the server or its shutdown observation signals its retained cleanup owner;
/// only explicit toolset shutdown closes the separate executable source.
///
/// # Examples
///
/// ```no_run
/// use std::sync::Arc;
/// use tools_kit::{Toolset, ToolsetBackend, ToolHandlerBackend, ToolError};
/// use tools_kit::declaration::ToolName;
/// use tools_kit::execution::{ToolHandlerInput, ToolHandlerOutput, ToolInvocation};
/// use tools_kit::local::ToolRegistration;
/// use tools_kit::mcp::{McpServer, McpServerConfig, McpClientConfig, McpConnectionConfig};
/// // MCP requires object-shaped arguments; local typed tools may use scalars.
/// #[derive(serde::Deserialize, schemars::JsonSchema)]
/// #[serde(deny_unknown_fields)]
/// struct DoubleArgs { value: i64 }
/// #[derive(serde::Serialize, schemars::JsonSchema)]
/// struct DoubleResult { value: i64 }
/// # #[tokio::main(flavor = "current_thread")]
/// # async fn main() -> Result<(), ToolError> {
/// let source = Arc::new(Toolset::local(vec![ToolRegistration::typed(
///     "double", |input: DoubleArgs| async move {
///         let value = input.value.checked_mul(2).ok_or(ToolError::InvalidArguments {
///             tool: ToolName::new("double")?, message: "doubling would overflow i64".into(), issues: Vec::new(),
///         })?;
///         Ok(DoubleResult { value })
///     },
/// )?])?);
/// let config = McpServerConfig::new("127.0.0.1:8000".parse().unwrap(), "/mcp", "page")?;
/// let server = McpServer::start(Arc::clone(&source), config).await;
/// let observed = async { match server {
///     Ok(server) => {
///         let client_work = async {
///             let client = Toolset::mcp(McpClientConfig::new(McpConnectionConfig::StreamableHttp {
///                 endpoint: "http://127.0.0.1:8000/mcp".parse().unwrap(), headers: Default::default(),
///             })).await?;
///             let invoked = async {
///                 let mut handler = client.tool_handler(ToolName::new("double")?).await?;
///                 let output = handler.step(ToolHandlerInput::Invoke(ToolInvocation {
///                     call_id: Some("client-call".into()), arguments: serde_json::json!({"value":21}),
///                 })).await?;
///                 let ToolHandlerOutput::Completed(result) = output else {
///                     return Err(ToolError::Protocol { message: "unexpected interaction".into() });
///                 };
///                 assert!(!result.is_error);
///                 assert_eq!(result.structured_content, Some(serde_json::json!({"value":42})));
///                 Ok::<(), ToolError>(())
///             }.await;
///             let client_closed = client.shutdown().await;
///             invoked?;
///             client_closed
///         }.await;
///         let serving_closed = server.shutdown().await; // Consumes and joins serving.
///         client_work?;
///         serving_closed?;
///         assert_eq!(source.list_tools().await?.len(), 1); // Source remains usable.
///         Ok(())
///     }
///     Err(error) => Err(error),
/// }
/// }.await;
/// let source_closed = source.shutdown().await;
/// observed?;
/// source_closed?;
/// # Ok(())
/// # }
/// ```
///
pub struct McpServer {
    serving: Arc<Serving>,
}

impl McpServer {
    /// Binds and starts the configured loopback endpoint eagerly.
    ///
    /// A failed start leaves the caller's toolset usable. Neither the toolset nor
    /// its associated handler needs to implement `Clone` or use supplied handles.
    ///
    /// This example binds a real loopback endpoint and requires an available port.
    /// Keep the Tokio runtime driven through both independently owned cleanup operations.
    ///
    /// ```no_run
    /// # #[tokio::main(flavor = "current_thread")]
    /// # async fn main() -> Result<(), tools_kit::ToolError> {
    /// use std::sync::Arc;
    /// use tools_kit::{Toolset, ToolsetBackend, mcp::{McpServer, McpServerConfig}};
    /// let tools = Arc::new(Toolset::local(Vec::new())?);
    /// let config = McpServerConfig::new("127.0.0.1:8000".parse().unwrap(), "/mcp", "page")?;
    /// let result = McpServer::start(Arc::clone(&tools), config).await;
    /// let stopped = match result {
    ///     Ok(server) => server.shutdown().await,
    ///     Err(error) => Err(error),
    /// };
    /// let source_closed = tools.shutdown().await;
    /// stopped?;
    /// source_closed?;
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// # Errors
    ///
    /// Returns [`ToolError::InvalidConfiguration`] for invalid server settings or [`ToolError::Transport`] when the endpoint cannot bind. Failed startup leaves source shutdown with the caller.
    ///
    pub async fn start<T: ToolsetBackend>(
        toolset: Arc<T>,
        config: McpServerConfig,
    ) -> Result<Self, ToolError> {
        config.validate()?;
        let listener =
            TcpListener::bind(config.bind())
                .await
                .map_err(|error| ToolError::Transport {
                    tool: None,
                    message: format!("failed to bind MCP server endpoint: {error}"),
                })?;
        let serving = Serving::new(config.shutdown_timeout());
        let protocol = Arc::new(Protocol::new(Arc::clone(&serving)));
        let handler = McpServeHandler {
            serving: Arc::clone(&serving),
            toolset,
            interaction_policy: config.interaction_policy(),
            cursor_prefix: config.cursor_prefix().to_owned(),
            _protocol: Arc::clone(&protocol),
        };
        let transport_config = StreamableHttpServerConfig::default();
        let stream_cancellation = transport_config.cancellation_token.clone();
        let service: StreamableHttpService<McpServeHandler<T>, Sessions> =
            StreamableHttpService::new(
                move || {
                    if handler.serving.closing() {
                        Err(std::io::Error::other("MCP server is closing"))
                    } else {
                        Ok(handler.clone())
                    }
                },
                Arc::new(Sessions::new(Arc::clone(&serving), protocol)),
                transport_config,
            );
        let router = Router::new()
            .nest_service(config.endpoint_path(), service)
            .layer(middleware::from_fn(McpOriginPolicy::enforce));
        let stopping = Arc::clone(&serving);
        serving.start(async move {
            axum::serve(listener, router)
                .with_graceful_shutdown(async move {
                    stopping.stopped().await;
                    stream_cancellation.cancel();
                })
                .await
        });
        Ok(Self { serving })
    }

    /// Seals admission and observes the retained server owner through cleanup.
    ///
    /// A timeout reports unsettled cleanup; it does not abort the cleanup owner.
    /// This example binds a real loopback endpoint and requires an available port.
    /// Keep the Tokio runtime driven until both server and toolset cleanup settle.
    /// Dropping the server or its shutdown observation signals its retained cleanup owner;
    /// only explicit toolset shutdown closes the separate executable source.
    ///
    /// # Errors
    ///
    /// Returns the retained server cleanup failure, including timeout or handler-cleanup failure. A timeout leaves cleanup owned rather than aborting it.
    ///
    /// # Examples
    ///
    /// ```no_run
    ///
    /// # #[tokio::main(flavor = "current_thread")]
    /// # async fn main() -> Result<(), tools_kit::ToolError> {
    /// use std::sync::Arc;
    /// use tools_kit::{Toolset, ToolsetBackend, mcp::{McpServer, McpServerConfig}};
    /// let tools = Arc::new(Toolset::local(Vec::new())?);
    /// let config = McpServerConfig::new("127.0.0.1:8000".parse().unwrap(), "/mcp", "page")?;
    /// let server = McpServer::start(Arc::clone(&tools), config).await?;
    /// let stopped = server.shutdown().await;
    /// let source_closed = tools.shutdown().await;
    /// stopped?;
    /// source_closed?;
    ///
    /// # Ok(())
    /// # }
    /// ```
    ///
    pub async fn shutdown(self) -> Result<(), ToolError> {
        self.serving.shutdown().await
    }
}

impl Drop for McpServer {
    fn drop(&mut self) {
        self.serving.release_observer();
    }
}

impl std::fmt::Debug for McpServer {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter.debug_struct("McpServer").finish_non_exhaustive()
    }
}

struct McpServeHandler<T: ToolsetBackend> {
    serving: Arc<Serving>,
    toolset: Arc<T>,
    interaction_policy: McpServerInteractionPolicy,
    cursor_prefix: String,
    // Final field: retire protocol ownership after caller resources.
    _protocol: Arc<Protocol>,
}

impl<T: ToolsetBackend> Clone for McpServeHandler<T> {
    fn clone(&self) -> Self {
        Self {
            serving: Arc::clone(&self.serving),
            toolset: Arc::clone(&self.toolset),
            interaction_policy: self.interaction_policy,
            cursor_prefix: self.cursor_prefix.clone(),
            _protocol: Arc::clone(&self._protocol),
        }
    }
}

impl<T: ToolsetBackend> ServerHandler for McpServeHandler<T> {
    fn get_info(&self) -> ServerInfo {
        ServerInfo::new(ServerCapabilities::builder().enable_tools().build())
    }

    async fn list_tools(
        &self,
        request: Option<PaginatedRequestParams>,
        context: RequestContext<RoleServer>,
    ) -> Result<ListToolsResult, McpError> {
        let handler = self.clone();
        let owner = Arc::clone(&self.serving);
        let cancellation = context.ct.clone();
        let observation = self.serving.admit(async move {
            let operation = handler.list_tools_owned(request, context);
            tokio::pin!(operation);
            tokio::select! {
                biased;
                _ = owner.stopped() => {
                    cancellation.cancel();
                    operation.await
                }
                result = &mut operation => result,
            }
        })?;
        observation
            .await
            .map_err(|_| McpError::internal_error("MCP server inventory task terminated", None))?
    }

    async fn call_tool(
        &self,
        request: CallToolRequestParams,
        context: RequestContext<RoleServer>,
    ) -> Result<CallToolResult, McpError> {
        let handler = self.clone();
        let owner = Arc::clone(&self.serving);
        let cancellation = context.ct.clone();
        let observation = self.serving.admit(async move {
            let operation = handler.call_tool_owned(request, context);
            tokio::pin!(operation);
            tokio::select! {
                biased;
                _ = owner.stopped() => {
                    cancellation.cancel();
                    operation.await
                }
                result = &mut operation => result,
            }
        })?;
        observation
            .await
            .map_err(|_| McpError::internal_error("MCP server invocation task terminated", None))?
    }
}

impl<T: ToolsetBackend> McpServeHandler<T> {
    async fn list_tools_owned(
        &self,
        request: Option<PaginatedRequestParams>,
        context: RequestContext<RoleServer>,
    ) -> Result<ListToolsResult, McpError> {
        if context.ct.is_cancelled() {
            return Err(McpError::internal_error(
                "MCP tools/list request was cancelled",
                None,
            ));
        }
        let tools = self
            .toolset
            .list_tools()
            .await
            .map_err(|error| McpError::internal_error(error.to_string(), None))?;
        let start = self.list_tools_offset(request, &context, tools.len())?;
        let page = tools
            .iter()
            .skip(start)
            .take(LIST_TOOLS_PAGE_SIZE)
            .map(McpProtocol::tool_info_to_mcp)
            .collect::<Result<Vec<_>, _>>()
            .map_err(|error| McpError::internal_error(error.to_string(), None))?;
        let next_offset = start + page.len();
        let mut result = ListToolsResult::with_all_items(page);
        result.next_cursor =
            (next_offset < tools.len()).then(|| format!("{}{next_offset}", self.cursor_prefix));
        Ok(result)
    }

    async fn call_tool_owned(
        &self,
        request: CallToolRequestParams,
        context: RequestContext<RoleServer>,
    ) -> Result<CallToolResult, McpError> {
        let name = ToolName::new(request.name.to_string())
            .map_err(|error| McpError::invalid_params(error.to_string(), None))?;
        self.check_cancellation(&name, &context)
            .map_err(McpProtocol::tool_error_to_mcp_error)?;
        let info = self
            .declaration(&name)
            .await
            .map_err(McpProtocol::tool_error_to_mcp_error)?;
        self.check_cancellation(&name, &context)
            .map_err(McpProtocol::tool_error_to_mcp_error)?;
        let arguments = serde_json::Value::Object(request.arguments.unwrap_or_default());
        let issues = info
            .input_schema
            .validate(&arguments)
            .map_err(McpProtocol::tool_error_to_mcp_error)?;
        if !issues.is_empty() {
            return Err(McpProtocol::tool_error_to_mcp_error(
                ToolError::InvalidArguments {
                    tool: name,
                    message: "invocation failed the declared input schema".to_owned(),
                    issues,
                },
            ));
        }
        let mut handler = self
            .toolset
            .tool_handler(name.clone())
            .await
            .map_err(McpProtocol::tool_error_to_mcp_error)?;
        // Construction may acquire caller-owned resources. Retain it even when
        // cancelled, then cancel the returned handler before its first Invoke.
        let mut output = self
            .step(
                &mut handler,
                ToolHandlerInput::Invoke(ToolInvocation {
                    call_id: None,
                    arguments,
                }),
                &name,
                &context,
            )
            .await
            .map_err(McpProtocol::tool_error_to_mcp_error)?;
        loop {
            match output {
                ToolHandlerOutput::Completed(result) => {
                    info.validate_output(&result)
                        .map_err(McpProtocol::tool_error_to_mcp_error)?;
                    return McpProtocol::tool_result_to_mcp(result)
                        .map_err(|error| McpError::internal_error(error.to_string(), None));
                }
                ToolHandlerOutput::Interaction(request) => {
                    let response = self
                        .interaction_policy
                        .resolve(request, &context, &self.serving)
                        .await;
                    let response = match response {
                        Ok(response) => response,
                        Err(primary) => {
                            return Err(McpProtocol::tool_error_to_mcp_error(
                                self.cancel(&mut handler, &name, primary).await,
                            ));
                        }
                    };
                    output = self
                        .step(
                            &mut handler,
                            ToolHandlerInput::Interaction(response),
                            &name,
                            &context,
                        )
                        .await
                        .map_err(McpProtocol::tool_error_to_mcp_error)?;
                }
            }
        }
    }

    fn check_cancellation(
        &self,
        name: &ToolName,
        context: &RequestContext<RoleServer>,
    ) -> Result<(), ToolError> {
        if context.ct.is_cancelled() {
            Err(ToolError::Cancelled {
                tool: name.clone(),
                reason: Some("MCP invocation was cancelled".to_owned()),
            })
        } else {
            Ok(())
        }
    }

    async fn step(
        &self,
        handler: &mut T::Handler,
        input: ToolHandlerInput,
        name: &ToolName,
        context: &RequestContext<RoleServer>,
    ) -> Result<ToolHandlerOutput, ToolError> {
        if let Err(primary) = self.check_cancellation(name, context) {
            return Err(self.cancel(handler, name, primary).await);
        }
        let output = {
            let step = handler.step(input);
            tokio::pin!(step);
            tokio::select! {
                biased;
                _ = context.ct.cancelled() => None,
                output = &mut step => Some(output),
            }
        };
        // End the pending step's mutable borrow before issuing Cancel on the
        // same unique handler. Await cleanup rather than cancelling its waiter.
        match output {
            Some(output) => output,
            None => Err(self
                .cancel(
                    handler,
                    name,
                    ToolError::Cancelled {
                        tool: name.clone(),
                        reason: Some("MCP invocation was cancelled".to_owned()),
                    },
                )
                .await),
        }
    }

    async fn cancel(
        &self,
        handler: &mut T::Handler,
        name: &ToolName,
        primary: ToolError,
    ) -> ToolError {
        match handler
            .step(ToolHandlerInput::Cancel {
                reason: Some(primary.to_string()),
            })
            .await
        {
            Ok(ToolHandlerOutput::Completed(_)) | Err(ToolError::Cancelled { .. }) => primary,
            Ok(ToolHandlerOutput::Interaction(_)) => {
                let error = ToolError::Transport {
                    tool: Some(name.clone()),
                    message: format!("{primary}; handler cancellation left an interaction pending"),
                };
                self.serving.record(error.clone());
                error
            }
            Err(cleanup) => {
                let error = ToolError::Transport {
                    tool: Some(name.clone()),
                    message: format!("{primary}; handler cancellation also failed: {cleanup}"),
                };
                self.serving.record(error.clone());
                error
            }
        }
    }

    async fn declaration(&self, name: &ToolName) -> Result<ToolInfo, ToolError> {
        let mut matching = self
            .toolset
            .list_tools()
            .await?
            .into_iter()
            .filter(|info| &info.name == name);
        let info = matching
            .next()
            .ok_or_else(|| ToolError::UnknownTool { name: name.clone() })?;
        if matching.next().is_some() {
            return Err(ToolError::DuplicateName { name: name.clone() });
        }
        Ok(info)
    }

    fn list_tools_offset(
        &self,
        request: Option<PaginatedRequestParams>,
        context: &RequestContext<RoleServer>,
        tool_count: usize,
    ) -> Result<usize, McpError> {
        if context.ct.is_cancelled() {
            return Err(McpError::internal_error(
                "MCP tools/list request was cancelled".to_owned(),
                None,
            ));
        }
        let Some(cursor) = request.and_then(|params| params.cursor) else {
            return Ok(0);
        };
        let Some(raw_offset) = cursor.strip_prefix(&self.cursor_prefix) else {
            return Err(McpError::invalid_params(
                format!("invalid MCP tools/list cursor `{cursor}`"),
                None,
            ));
        };
        let offset = raw_offset.parse::<usize>().map_err(|_| {
            McpError::invalid_params(format!("invalid MCP tools/list cursor `{cursor}`"), None)
        })?;
        if offset == 0 || offset >= tool_count || offset % LIST_TOOLS_PAGE_SIZE != 0 {
            return Err(McpError::invalid_params(
                format!("invalid MCP tools/list cursor `{cursor}`"),
                None,
            ));
        }
        Ok(offset)
    }
}

impl McpServerInteractionPolicy {
    async fn resolve(
        self,
        request: ToolInteractionRequest,
        context: &RequestContext<RoleServer>,
        serving: &Serving,
    ) -> Result<ToolInteractionResponse, ToolError> {
        match (self, &request.kind) {
            (Self::RejectWithToolError, _) => Err(ToolError::Unsupported {
                operation: "MCP server tool interaction".to_owned(),
            }),
            (Self::ElicitFormIfClientSupports, ToolInteractionKind::Form { schema, .. })
                if context
                    .peer
                    .supported_elicitation_modes()
                    .contains(&ElicitationMode::Form) =>
            {
                let schema = self.elicitation_schema(schema.clone())?;
                let deadline = time::Instant::now() + INTERACTION_TIMEOUT;
                let admission = context.peer.send_cancellable_request(
                    ServerRequest::CreateElicitationRequest(rmcp::model::Request::new(
                        CreateElicitationRequestParams::FormElicitationParams {
                            meta: None,
                            message: request.prompt.clone(),
                            requested_schema: schema,
                        },
                    )),
                    PeerRequestOptions::no_options(),
                );
                // rmcp returns its handle synchronously after queue admission.
                // Cancelling this wait cannot discard an already admitted handle.
                let mut elicitation = tokio::select! {
                    biased;
                    _ = context.ct.cancelled() => return Err(ToolError::Cancelled {
                        tool: request.tool_name.clone(),
                        reason: Some("MCP invocation was cancelled before elicitation admission".to_owned()),
                    }),
                    _ = time::sleep_until(deadline) => return Err(ToolError::Timeout {
                        tool: request.tool_name.clone(),
                        timeout_secs: INTERACTION_TIMEOUT.as_secs(),
                    }),
                    result = admission => result,
                }
                .map_err(|error| ToolError::Transport {
                        tool: Some(request.tool_name.clone()),
                        message: format!("MCP client elicitation failed: {error}"),
                    })?;
                // Keep the native handle until observation or explicit cancellation
                // completes. Dropping rmcp's response future sends no cancellation.
                let observed = tokio::select! {
                    biased;
                    _ = context.ct.cancelled() => Err(ToolError::Cancelled {
                        tool: request.tool_name.clone(),
                        reason: Some("MCP invocation was cancelled during elicitation".to_owned()),
                    }),
                    _ = time::sleep_until(deadline) => Err(ToolError::Timeout {
                        tool: request.tool_name.clone(),
                        timeout_secs: INTERACTION_TIMEOUT.as_secs(),
                    }),
                    result = &mut elicitation.rx => Ok(result),
                };
                let response = match observed {
                    Ok(response) => response,
                    Err(primary) => {
                        return Err(match time::timeout(
                            INTERACTION_CLEANUP_TIMEOUT,
                            elicitation.cancel(Some(primary.to_string())),
                        )
                        .await
                        {
                            Ok(Ok(())) => primary,
                            Ok(Err(cleanup)) => {
                                let error = ToolError::Transport {
                                    tool: Some(request.tool_name.clone()),
                                    message: format!(
                                        "{primary}; elicitation cancellation also failed: {cleanup}"
                                    ),
                                };
                                serving.record(error.clone());
                                error
                            }
                            Err(_) => {
                                let error = ToolError::Transport {
                                    tool: Some(request.tool_name.clone()),
                                    message: format!(
                                        "{primary}; elicitation cancellation acknowledgement timed out; client request settlement is unconfirmed"
                                    ),
                                };
                                serving.record(error.clone());
                                error
                            }
                        });
                    }
                }
                .map_err(|error| ToolError::Transport {
                    tool: Some(request.tool_name.clone()),
                    message: format!("MCP client elicitation response was lost: {error}"),
                })?
                .map_err(|error| ToolError::Transport {
                    tool: Some(request.tool_name.clone()),
                    message: format!("MCP client elicitation failed: {error}"),
                })?;
                let ClientResult::CreateElicitationResult(result) = response else {
                    return Err(ToolError::Protocol {
                        message: "MCP client returned an unexpected elicitation response"
                            .to_owned(),
                    });
                };
                let id = request.id.clone();
                let response = match result.action {
                    ElicitationAction::Accept => ToolInteractionResponse::submit(
                        id,
                        result.content.unwrap_or(serde_json::Value::Null),
                    ),
                    ElicitationAction::Decline => ToolInteractionResponse::decline(id, None),
                    ElicitationAction::Cancel => ToolInteractionResponse::cancel(id, None),
                };
                // A rejected response can leave the unique handler pending.
                // Validate before advancing it so the owning call cancels and
                // awaits that handler through its existing resolution-error path.
                request.validate_response(&response)?;
                Ok(response)
            }
            (Self::ElicitFormIfClientSupports, ToolInteractionKind::Form { .. }) => {
                Err(ToolError::Unsupported {
                    operation: "MCP client form elicitation".to_owned(),
                })
            }
            (Self::ElicitFormIfClientSupports, ToolInteractionKind::Text { .. }) => {
                Err(ToolError::Unsupported {
                    operation: "MCP server text interaction".to_owned(),
                })
            }
        }
    }

    fn elicitation_schema(self, schema: serde_json::Value) -> Result<ElicitationSchema, ToolError> {
        let serde_json::Value::Object(object) = schema else {
            return Err(ToolError::Unsupported {
                operation: "non-object MCP form schema".to_owned(),
            });
        };
        ElicitationSchema::from_json_schema(object).map_err(|error| ToolError::Unsupported {
            operation: format!("MCP form schema: {error}"),
        })
    }
}

struct McpOriginPolicy;

impl McpOriginPolicy {
    async fn enforce(request: Request, next: Next) -> Response {
        if let Some(origin) = request.headers().get(header::ORIGIN)
            && !Self::is_loopback(origin)
        {
            return StatusCode::FORBIDDEN.into_response();
        }
        next.run(request).await
    }

    fn is_loopback(origin: &axum::http::HeaderValue) -> bool {
        let Ok(origin) = origin.to_str() else {
            return false;
        };
        let Ok(url) = Url::parse(origin) else {
            return false;
        };
        match url.host() {
            Some(Host::Domain("localhost")) => true,
            Some(Host::Ipv4(address)) => address.is_loopback(),
            Some(Host::Ipv6(address)) => address.is_loopback(),
            _ => false,
        }
    }
}