pub struct GuardLayer<G> { /* private fields */ }Expand description
A tower Layer that applies a guard function before the inner service.
Guards run before the tool handler and can short-circuit with an error message.
Use via ToolBuilderWithHandler::guard or Tool::with_guard rather than
constructing directly.
§Example
use tower_mcp::{ToolBuilder, ToolRequest, CallToolResult};
use schemars::JsonSchema;
use serde::Deserialize;
#[derive(Debug, Deserialize, JsonSchema)]
struct DeleteInput { id: String, confirm: bool }
let tool = ToolBuilder::new("delete")
.description("Delete a record")
.handler(|input: DeleteInput| async move {
Ok(CallToolResult::text(format!("deleted {}", input.id)))
})
.guard(|req: &ToolRequest| {
let confirm = req.args.get("confirm").and_then(|v| v.as_bool()).unwrap_or(false);
if !confirm {
return Err("Must set confirm=true to delete".to_string());
}
Ok(())
})
.build();Implementations§
Source§impl<G> GuardLayer<G>
impl<G> GuardLayer<G>
Trait Implementations§
Source§impl<G: Clone> Clone for GuardLayer<G>
impl<G: Clone> Clone for GuardLayer<G>
Source§fn clone(&self) -> GuardLayer<G>
fn clone(&self) -> GuardLayer<G>
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreAuto Trait Implementations§
impl<G> Freeze for GuardLayer<G>where
G: Freeze,
impl<G> RefUnwindSafe for GuardLayer<G>where
G: RefUnwindSafe,
impl<G> Send for GuardLayer<G>where
G: Send,
impl<G> Sync for GuardLayer<G>where
G: Sync,
impl<G> Unpin for GuardLayer<G>where
G: Unpin,
impl<G> UnwindSafe for GuardLayer<G>where
G: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more