tool_router

Attribute Macro tool_router 

Source
#[tool_router]
Available on crate features macros and server only.
Expand description

§tool_router

This macro is used to generate a tool router based on functions marked with #[rmcp::tool] in an implementation block.

It creates a function that returns a ToolRouter instance.

In most case, you need to add a field for handler to store the router information and initialize it when creating handler, or store it with a static variable.

§Usage

fieldtypeusage
routerIdentThe name of the router function to be generated. Defaults to tool_router.
visVisibilityThe visibility of the generated router function. Defaults to empty.

§Example

#[tool_router]
impl MyToolHandler {
    #[tool]
    pub fn my_tool() {
         
    }

    pub fn new() -> Self {
        Self {
            // the default name of tool router will be `tool_router`
            tool_router: Self::tool_router(),
        }
    }
}

Or specify the visibility and router name, which would be helpful when you want to combine multiple routers into one:

mod a {
    #[tool_router(router = tool_router_a, vis = pub)]
    impl MyToolHandler {
        #[tool]
        fn my_tool_a() {
             
        }
    }
}

mod b {
    #[tool_router(router = tool_router_b, vis = pub)]
    impl MyToolHandler {
        #[tool]
        fn my_tool_b() {
             
        }
    }
}

impl MyToolHandler {
    fn new() -> Self {
        Self {
            tool_router: self::tool_router_a() + self::tool_router_b(),
        }
    }
}