#[tool_handler]Available on crate features
macros and server only.Expand description
§tool_handler
This macro generates the call_tool, list_tools, get_tool, and (optionally)
get_info methods for a ServerHandler implementation, using a ToolRouter.
§Usage
| field | type | usage |
|---|---|---|
router | Expr | The expression to access the ToolRouter instance. Defaults to Self::tool_router(). |
meta | Expr | Optional metadata for ListToolsResult. |
name | String | Custom server name. Defaults to CARGO_CRATE_NAME. |
version | String | Custom server version. Defaults to CARGO_PKG_VERSION. |
instructions | String | Optional human-readable instructions about using this server. |
§Minimal example (no boilerplate)
The macro automatically generates get_info() with tools capability enabled
and reads the server name/version from Cargo.toml:
ⓘ
struct TimeServer;
#[tool_router]
impl TimeServer {
#[tool(description = "Get current time")]
async fn get_time(&self) -> String { "12:00".into() }
}
#[tool_handler]
impl ServerHandler for TimeServer {}§Custom server info
ⓘ
#[tool_handler(name = "my-server", version = "1.0.0", instructions = "A helpful server")]
impl ServerHandler for MyToolHandler {}§Custom router expression
ⓘ
#[tool_handler(router = self.tool_router)]
impl ServerHandler for MyToolHandler {
// ...implement other handler
}§Manual get_info()
If you provide your own get_info(), the macro will not generate one:
ⓘ
#[tool_handler]
impl ServerHandler for MyToolHandler {
fn get_info(&self) -> ServerInfo {
ServerInfo::new(ServerCapabilities::builder().enable_tools().build())
}
}