bedrock-agentcore 0.1.0

Amazon Bedrock AgentCore SDK for Rust
Documentation
//! An agent that answers its input. The Strands Agents SDK writes the agent and this
//! SDK hosts it.
//!
//! The agent calls Amazon Bedrock, so it needs AWS credentials and a Region.
//! Run it with `cargo run --example echo`, then:
//!
//! ```text
//! curl -s localhost:8080/invocations \
//!   -H 'X-Amzn-Bedrock-AgentCore-Runtime-Session-Id: local-session' \
//!   -d '{"prompt": "What is the weather in Seattle?"}'
//! ```

use bedrock_agentcore::{RequestContext, runtime::BedrockAgentCoreApp};
use serde::Deserialize;
use serde_json::{Value, json};
use strands::{Agent, ToolError, models::bedrock::BedrockModel, tool};

#[derive(Deserialize)]
struct Request {
    prompt: String,
}

/// Get the current weather for a city.
#[tool]
async fn get_weather(
    /// City name, for example "Seattle".
    city: String,
) -> Result<String, ToolError> {
    Ok(format!("Sunny and 21°C in {city}"))
}

#[tokio::main]
async fn main() -> Result<(), bedrock_agentcore::Error> {
    // The SDK emits tracing events. The application chooses where they go.
    tracing_subscriber::fmt().with_ansi(false).init();
    // One model for all invocations.
    let model = BedrockModel::builder().model_id("global.anthropic.claude-sonnet-4-6").max_tokens(4096).build();

    let handle = move |request: Request, ctx: RequestContext| {
        let model = model.clone();
        async move {
            // `invoke` changes the agent, so each invocation builds its own. The agent
            // prints its reply to stdout unless the printer is off.
            let mut agent = Agent::builder().model(model).tools([GetWeather]).printer(false).build()?;
            let result = agent.invoke(request.prompt).await?;
            agent.shutdown().await?;
            Ok::<Value, strands::Error>(json!({
                "result": result.to_string(),
                "session_id": ctx.session_id(),
                "request_id": ctx.request_id(),
            }))
        }
    };
    BedrockAgentCoreApp::builder().invocation(handle).build()?.run().await
}