#[tool_handler]Expand description
§tool_handler
This macro will generate the handler for tool_call and list_tools methods in the implementation block, by using an existing ToolRouter instance.
§Usage
| field | type | usage |
|---|---|---|
router | Expr | The expression to access the ToolRouter instance. Defaults to self.tool_router. |
§Example
ⓘ
#[tool_handler]
impl ServerHandler for MyToolHandler {
// ...implement other handler
}or using a custom router expression:
ⓘ
#[tool_handler(router = self.get_router().await)]
impl ServerHandler for MyToolHandler {
// ...implement other handler
}§Explain
This macro will be expended to something like this:
ⓘ
impl ServerHandler for MyToolHandler {
async fn call_tool(
&self,
request: CallToolRequestParams,
context: RequestContext<RoleServer>,
) -> Result<CallToolResult, rmcp::ErrorData> {
let tcc = ToolCallContext::new(self, request, context);
self.tool_router.call(tcc).await
}
async fn list_tools(
&self,
_request: Option<PaginatedRequestParams>,
_context: RequestContext<RoleServer>,
) -> Result<ListToolsResult, rmcp::ErrorData> {
let items = self.tool_router.list_all();
Ok(ListToolsResult::with_all_items(items))
}
}