pub trait Plugin:
Send
+ Sync
+ 'static {
// Required methods
fn name(&self) -> &str;
fn init_agent<'life0, 'life1, 'async_trait>(
&'life0 self,
agent: &'life1 mut LocalAgent,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait;
// Provided method
fn get_tools(&self) -> Vec<ToolSource> { ... }
}Expand description
Extends an agent with hooks, tools and setup (Plugin).
init_agent runs once when the agent initializes and typically
registers hooks; get_tools runs after it, so the tools can depend on
what initialization set up. Plugin names are unique per agent.
use strands::agent::LocalAgent;
use strands::hooks::BeforeToolCallEvent;
use strands::plugins::Plugin;
struct NoShell;
#[strands::async_trait]
impl Plugin for NoShell {
fn name(&self) -> &str {
"no-shell"
}
async fn init_agent(&self, agent: &mut LocalAgent) -> strands::Result<()> {
agent.add_hook(|event: &mut BeforeToolCallEvent| {
if event.tool_use().name == "shell" {
event.set_cancel("shell is disabled");
}
});
Ok(())
}
}Required Methods§
Sourcefn init_agent<'life0, 'life1, 'async_trait>(
&'life0 self,
agent: &'life1 mut LocalAgent,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn init_agent<'life0, 'life1, 'async_trait>(
&'life0 self,
agent: &'life1 mut LocalAgent,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Sets the plugin up on the agent: register hooks, read configuration.
§Errors
Any error fails agent initialization.
Provided Methods§
Sourcefn get_tools(&self) -> Vec<ToolSource>
fn get_tools(&self) -> Vec<ToolSource>
The tools to register on the agent after init_agent:
anything that converts into a ToolSource, such as vec![MyTool.into()]. An MCP client
is rejected; pass it to the agent’s tools instead.
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".
Implementations on Foreign Types§
Source§impl<P: Plugin + ?Sized> Plugin for Arc<P>
A shared plugin is a plugin, so Arc<dyn Plugin> and Arc<P> go wherever plugins do.
impl<P: Plugin + ?Sized> Plugin for Arc<P>
A shared plugin is a plugin, so Arc<dyn Plugin> and Arc<P> go wherever plugins do.