Skip to main content

strands/vended_tools/
mod.rs

1//! Ready-made tools.
2//!
3//! Each module holds a vended tool: a `make_*` factory that takes options (name,
4//! description, limits, and for the sandbox-routed tools an optional bound
5//! [`Sandbox`]), and for most tools a function returning the default
6//! instance. The tool specs, descriptions and result texts are shared with the other Strands SDKs.
7//!
8//! - Sandbox-routed: [`shell`], [`read`], [`write`](mod@write), [`edit`] and [`python_repl`] run through the bound sandbox, or the calling agent's
9//!   [`sandbox`](crate::ToolAgent::sandbox).
10//! - Host: [`bash`] keeps a persistent `bash` process per agent; [`http_request`] and
11//!   [`web_fetch`] make HTTP requests.
12//! - Agent loop: [`sleep`], [`stop`], [`handoff_to_user`], [`notebook`] and [`subagent`].
13//! - `mcp_router` (feature `mcp`): connects to allowlisted MCP servers at runtime.
14
15pub mod bash;
16pub mod edit;
17pub mod handoff_to_user;
18pub mod http_request;
19mod input;
20#[cfg(feature = "mcp")]
21pub mod mcp_router;
22pub mod notebook;
23pub mod paths;
24pub mod python_repl;
25pub mod read;
26pub mod shell;
27pub mod sleep;
28pub mod stop;
29pub mod subagent;
30pub mod web_fetch;
31pub mod write;
32
33#[cfg(test)]
34mod conformance_tests;
35#[cfg(test)]
36pub(crate) mod test_support;
37
38use std::sync::Arc;
39use std::time::Duration;
40
41use crate::sandbox::Sandbox;
42use crate::tools::ToolContext;
43use crate::types::JsonValue;
44
45/// Declares a vended tool's error type: a message, displayed as is, and the error class name.
46macro_rules! tool_error {
47    ($(#[$doc:meta])* $name:ident) => {
48        $(#[$doc])*
49        #[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
50        #[error("{message}")]
51        #[non_exhaustive]
52        pub struct $name {
53            message: String,
54        }
55
56        impl $name {
57            /// The error class name.
58            pub const NAME: &'static str = stringify!($name);
59
60            /// An error with `message`.
61            pub fn new(message: impl Into<String>) -> Self {
62                Self { message: message.into() }
63            }
64
65            /// What failed.
66            #[must_use]
67            pub fn message(&self) -> &str {
68                &self.message
69            }
70        }
71    };
72}
73pub(crate) use tool_error;
74
75/// The bound sandbox, or the calling agent's.
76fn resolve_sandbox(bound: Option<&Arc<dyn Sandbox>>, context: &ToolContext) -> Arc<dyn Sandbox> {
77    bound.unwrap_or_else(|| context.agent().sandbox()).clone()
78}
79
80/// A number as JSON, with whole numbers written without a fraction.
81fn js_number(value: f64) -> JsonValue {
82    if value.fract() == 0.0 && value.abs() < 9_007_199_254_740_992.0 {
83        #[allow(clippy::cast_possible_truncation, reason = "whole and within the safe integer range")]
84        return JsonValue::from(value as i64);
85    }
86    serde_json::Number::from_f64(value).map_or(JsonValue::Null, JsonValue::Number)
87}
88
89/// A number as text, with whole numbers written without a fraction.
90fn js_number_text(value: f64) -> String {
91    match js_number(value) {
92        JsonValue::Number(number) => number.to_string(),
93        _ => value.to_string(),
94    }
95}
96
97/// A timeout in seconds; one too large for a [`Duration`] is no timeout.
98fn seconds(value: f64) -> Option<Duration> {
99    Duration::try_from_secs_f64(value).ok()
100}