Skip to main content

strands/tools/
mod.rs

1//! Tools: the [`Tool`] trait and its stream, [`ToolContext`], [`ToolError`], [`FunctionTool`], the
2//! [`ToolRegistry`], structured output, input schemas and the [`executors`].
3//!
4//! # Converting errors
5//!
6//! A tool body fails with a [`ToolError`]. `?` converts any [`std::error::Error`] into one, and
7//! the interrupt [`ToolContext::interrupt`] returns as well. A [`BoxError`](crate::BoxError), and
8//! error types without a `std::error::Error` impl such as `anyhow::Error`, do not convert with
9//! `?`; wrap them with [`ToolError::from_boxed`] (or [`ToolError::msg`] for any `Display`). A
10//! `#[tool]` function may instead return `Result<T, E>` for any `E: Into<BoxError>`, and the macro
11//! converts the error.
12//!
13//! ```
14//! use strands::{BoxError, Tool, ToolError, tool};
15//!
16//! fn lookup(sku: &str) -> Result<u32, BoxError> {
17//!     if sku == "sku-1" { Ok(3) } else { Err(format!("unknown sku {sku}").into()) }
18//! }
19//!
20//! /// Look up stock for a product, plus a reserve.
21//! #[tool]
22//! async fn check_stock(sku: String, reserve: String) -> Result<u32, ToolError> {
23//!     let reserve: u32 = reserve.parse()?;
24//!     let stock = lookup(&sku).map_err(ToolError::from_boxed)?;
25//!     Ok(stock + reserve)
26//! }
27//!
28//! /// Look up stock for a product.
29//! #[tool]
30//! async fn check_stock_boxed(sku: String) -> Result<u32, BoxError> {
31//!     lookup(&sku)
32//! }
33//!
34//! assert_eq!(CheckStock.name(), "check_stock");
35//! assert_eq!(CheckStockBoxed.name(), "check_stock_boxed");
36//! ```
37
38mod error;
39pub mod executors;
40mod function_tool;
41mod load_tools;
42mod noop_tool;
43mod registry;
44mod schema;
45mod source;
46mod structured_output_tool;
47mod tool;
48
49pub use error::ToolError;
50pub use executors::{ConcurrentToolExecutor, SequentialToolExecutor, ToolExecutorStrategy};
51pub(crate) use function_tool::wrap_in_tool_result;
52pub use function_tool::{FunctionTool, FunctionToolBuilder, FunctionToolItem};
53pub use load_tools::LOAD_TOOLS_TOOL_NAME;
54pub(crate) use load_tools::{DeferredToolsFn, create_load_tools};
55#[cfg(feature = "bedrock")]
56pub(crate) use noop_tool::NOOP_TOOL_SPEC;
57pub use registry::ToolRegistry;
58pub use schema::{SchemaValidator, normalize_schema, schema_for};
59pub(crate) use source::SourceParts;
60pub use source::ToolSource;
61pub use structured_output_tool::{STRUCTURED_OUTPUT_TOOL_NAME, StructuredOutputTool};
62#[doc(hidden)]
63pub use tool::SdkToolRole;
64pub(crate) use tool::{DelegationToken, InterruptFn, has_sdk_tool_role};
65pub use tool::{
66    Tool, ToolContext, ToolOutcome, ToolStream, ToolStreamEvent, ToolStreamItem, create_error_result,
67    is_valid_tool_name,
68};