Skip to main content

flymcprust_core/
auto.rs

1//! Automatic registration (feature `auto-register`).
2//!
3//! `#[tool]`, `#[resource]` and `#[prompt]` submit an [`AutoTool`],
4//! [`AutoResource`] or [`AutoPrompt`] to a link-time registry provided by the
5//! `inventory` crate; [`ServerBuilder::auto_tools`](crate::ServerBuilder::auto_tools)
6//! and its siblings collect them. The registry relies on platform linker sections, which is
7//! why the feature is opt-in (see `docs/ARCHITECTURE.md` ยง8).
8
9use crate::prompt::PromptDef;
10use crate::resource::ResourceDef;
11use crate::tool::ToolDef;
12
13/// A tool registered by `#[tool]` for automatic discovery.
14#[derive(Debug)]
15pub struct AutoTool(fn() -> ToolDef);
16
17impl AutoTool {
18    /// Wraps a tool's definition function.
19    #[must_use]
20    pub const fn new(definition: fn() -> ToolDef) -> Self {
21        Self(definition)
22    }
23
24    pub(crate) fn definition(&self) -> ToolDef {
25        (self.0)()
26    }
27}
28
29inventory::collect!(AutoTool);
30
31/// A resource registered by `#[resource]` for automatic discovery.
32#[derive(Debug)]
33pub struct AutoResource(fn() -> ResourceDef);
34
35impl AutoResource {
36    /// Wraps a resource's definition function.
37    #[must_use]
38    pub const fn new(definition: fn() -> ResourceDef) -> Self {
39        Self(definition)
40    }
41
42    pub(crate) fn definition(&self) -> ResourceDef {
43        (self.0)()
44    }
45}
46
47inventory::collect!(AutoResource);
48
49/// A prompt registered by `#[prompt]` for automatic discovery.
50#[derive(Debug)]
51pub struct AutoPrompt(fn() -> PromptDef);
52
53impl AutoPrompt {
54    /// Wraps a prompt's definition function.
55    #[must_use]
56    pub const fn new(definition: fn() -> PromptDef) -> Self {
57        Self(definition)
58    }
59
60    pub(crate) fn definition(&self) -> PromptDef {
61        (self.0)()
62    }
63}
64
65inventory::collect!(AutoPrompt);