use std::fmt;
use std::sync::Arc;
use async_trait::async_trait;
use serde::de::{self, Visitor};
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use serde_json::json;
use strands::tools::{FunctionTool, ToolError};
use strands::types::{JsonSchema, JsonValue};
#[cfg(feature = "mcp")]
pub(crate) const EXA_MCP_URL: &str = "https://mcp.exa.ai/mcp";
#[cfg(feature = "mcp")]
pub(crate) const EXA_TOOL: &str = "web_search_exa";
const MAX_RESULTS: i64 = 10;
const DEFAULT_RESULTS: i64 = 5;
#[cfg_attr(
not(any(feature = "mcp", feature = "bedrock", feature = "mantle")),
allow(dead_code, reason = "used by the Exa and AgentCore backends")
)]
const SNIPPET_CHARS: usize = 500;
pub const WEB_SEARCH_DESCRIPTION: &str =
"Search the web and return the top results as numbered sources with a short excerpt each.";
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum WebSearchSetting {
Off,
#[default]
On,
Exa,
}
impl From<bool> for WebSearchSetting {
fn from(enabled: bool) -> Self {
if enabled { Self::On } else { Self::Off }
}
}
impl Serialize for WebSearchSetting {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
match self {
Self::Off => serializer.serialize_bool(false),
Self::On => serializer.serialize_bool(true),
Self::Exa => serializer.serialize_str("exa"),
}
}
}
struct SettingVisitor;
impl Visitor<'_> for SettingVisitor {
type Value = WebSearchSetting;
fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("a boolean or 'exa'")
}
fn visit_bool<E: de::Error>(self, value: bool) -> Result<Self::Value, E> {
Ok(value.into())
}
fn visit_str<E: de::Error>(self, value: &str) -> Result<Self::Value, E> {
if value == "exa" {
return Ok(WebSearchSetting::Exa);
}
Err(E::custom("must be a boolean or 'exa'."))
}
}
impl<'de> Deserialize<'de> for WebSearchSetting {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
deserializer.deserialize_any(SettingVisitor)
}
}
#[must_use]
pub fn anthropic_web_search_tool() -> JsonValue {
json!({"type": "web_search_20260318", "name": "web_search", "allowed_callers": ["direct"]})
}
#[must_use]
pub fn openai_web_search_tool() -> JsonValue {
json!({"type": "web_search"})
}
#[must_use]
pub fn bedrock_mantle_web_search_tool() -> JsonValue {
json!({"type": "web_search", "external_web_access": true})
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct SearchResult {
pub title: String,
pub url: String,
pub snippet: String,
}
impl SearchResult {
pub fn new(title: impl Into<String>, url: impl Into<String>, snippet: impl Into<String>) -> Self {
Self {
title: title.into(),
url: url.into(),
snippet: snippet.into(),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
#[error("{message}")]
#[non_exhaustive]
pub struct WebSearchError {
message: String,
}
impl WebSearchError {
pub fn new(message: impl Into<String>) -> Self {
Self {
message: message.into(),
}
}
#[must_use]
pub fn message(&self) -> &str {
&self.message
}
}
#[async_trait]
pub trait SearchBackend: Send + Sync + 'static {
async fn search(&self, query: &str, max_results: usize) -> Result<Vec<SearchResult>, WebSearchError>;
}
fn input_schema() -> JsonSchema {
json!({
"type": "object",
"properties": {
"query": {
"type": "string",
"description": "What to search for. A descriptive sentence works better than bare keywords."
},
"max_results": {
"description": "How many results to return, 1-10.",
"type": "integer",
"minimum": -9_007_199_254_740_991_i64,
"maximum": 9_007_199_254_740_991_i64
}
},
"required": ["query"],
"additionalProperties": false
})
}
fn one_line(text: &str) -> String {
text.split_whitespace().collect::<Vec<_>>().join(" ")
}
#[allow(
clippy::cast_possible_truncation,
clippy::cast_sign_loss,
reason = "clamped to 1..=10"
)]
fn max_results(input: &JsonValue) -> usize {
let requested = input
.get("max_results")
.and_then(JsonValue::as_i64)
.unwrap_or(DEFAULT_RESULTS);
requested.clamp(1, MAX_RESULTS) as usize
}
fn format_results(results: &[SearchResult]) -> String {
if results.is_empty() {
return "No results.".to_owned();
}
let mut lines = vec!["Sources:".to_owned()];
for (index, result) in results.iter().enumerate() {
lines.push(format!(
"{}. {} \u{2014} {}",
index + 1,
one_line(&result.title),
result.url
));
if !result.snippet.is_empty() {
lines.push(format!(" {}", one_line(&result.snippet)));
}
}
lines.join("\n")
}
#[must_use]
pub fn search_tool(backend: Arc<dyn SearchBackend>) -> FunctionTool {
FunctionTool::builder("web_search", WEB_SEARCH_DESCRIPTION)
.input_schema(input_schema())
.callback(move |input, _context| {
let backend = backend.clone();
async move {
let query = input
.get("query")
.and_then(JsonValue::as_str)
.ok_or_else(|| ToolError::msg("Invalid input: expected string at \"query\"."))?;
let text = match backend.search(query, max_results(&input)).await {
Ok(results) => format_results(&results),
Err(error) => {
let reason = if error.message().is_empty() {
"Error"
} else {
error.message()
};
format!("web_search failed: {reason}")
}
};
Ok::<JsonValue, ToolError>(text.into())
}
})
}
#[cfg_attr(not(feature = "mcp"), allow(dead_code, reason = "used by the Exa backend"))]
fn parse_exa(text: &str) -> Vec<SearchResult> {
let mut blocks = Vec::new();
let mut current = String::new();
for line in text.split_inclusive('\n') {
let bare = line.trim_end_matches(['\n', '\r']);
if bare.starts_with("---") && bare[3..].chars().all(char::is_whitespace) {
blocks.push(std::mem::take(&mut current));
continue;
}
current.push_str(line);
}
blocks.push(current);
blocks.iter().filter_map(|block| parse_block(block)).collect()
}
#[cfg_attr(not(feature = "mcp"), allow(dead_code, reason = "used by the Exa backend"))]
fn parse_block(block: &str) -> Option<SearchResult> {
let rest = block.trim_start();
let rest = rest.strip_prefix("Title: ")?;
let (title, rest) = rest.split_once('\n')?;
let title = title.strip_suffix('\r').unwrap_or(title);
let rest = rest.strip_prefix("URL: ")?;
let url: String = rest.chars().take_while(|c| !c.is_whitespace()).collect();
if url.is_empty() {
return None;
}
let highlights = block
.find("Highlights:")
.map_or("", |index| &block[index + "Highlights:".len()..]);
let snippet: String = one_line(highlights).chars().take(SNIPPET_CHARS).collect();
Some(SearchResult::new(title.trim(), url, snippet))
}
#[cfg(any(feature = "bedrock", feature = "mantle"))]
pub(crate) use agentcore::agent_core_web_search;
#[cfg(any(feature = "bedrock", feature = "mantle"))]
pub use agentcore::{AgentCoreBackend, MakeAgentCoreWebSearchOptions, make_agent_core_web_search};
#[cfg(feature = "mcp")]
pub use exa::{ExaBackend, web_search_tool};
#[cfg(any(feature = "bedrock", feature = "mantle"))]
mod agentcore;
#[cfg(feature = "mcp")]
mod exa {
use std::collections::BTreeMap;
use std::fmt;
use std::sync::Arc;
use std::time::Duration;
use async_trait::async_trait;
use serde_json::json;
use strands::Tool;
use strands::mcp::{McpCallToolOptions, McpClient, McpClientConfig, McpClientOptions, McpToolFilters};
use strands::tools::FunctionTool;
use strands::types::JsonValue;
use super::{EXA_MCP_URL, EXA_TOOL, SearchBackend, SearchResult, WebSearchError, parse_exa, search_tool};
const TIMEOUT: Duration = Duration::from_secs(30);
type EnvLookup = Arc<dyn Fn() -> Option<String> + Send + Sync>;
#[derive(Clone)]
pub struct ExaBackend {
api_key: Option<String>,
url: String,
env: EnvLookup,
}
impl fmt::Debug for ExaBackend {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("ExaBackend")
.field("api_key", &self.api_key.as_ref().map(|_| "<redacted>"))
.field("url", &self.url)
.finish_non_exhaustive()
}
}
impl Default for ExaBackend {
fn default() -> Self {
Self::new(None)
}
}
impl ExaBackend {
#[must_use]
pub fn new(api_key: Option<String>) -> Self {
Self {
api_key,
url: EXA_MCP_URL.to_owned(),
env: Arc::new(|| std::env::var("EXA_API_KEY").ok()),
}
}
#[must_use]
pub fn with_url(mut self, url: impl Into<String>) -> Self {
self.url = url.into();
self
}
#[cfg(test)]
pub(super) fn with_env(mut self, env: impl Fn() -> Option<String> + Send + Sync + 'static) -> Self {
self.env = Arc::new(env);
self
}
async fn call(&self, query: &str, max_results: usize, key: &str) -> Result<Vec<SearchResult>, WebSearchError> {
let headers = (!key.is_empty()).then(|| BTreeMap::from([("x-api-key".to_owned(), key.to_owned())]));
let config = McpClientConfig {
url: Some(self.url.clone()),
headers,
options: McpClientOptions {
tool_filters: Some(McpToolFilters {
allowed: Some(vec![EXA_TOOL.into()]),
rejected: None,
}),
..McpClientOptions::default()
},
..McpClientConfig::default()
};
let client = McpClient::new(config).map_err(|error| WebSearchError::new(error.to_string()))?;
let result = self.call_with(&client, query, max_results).await;
if let Err(error) = client.disconnect().await {
tracing::debug!("error=<{error}> | exa mcp session teardown failed");
}
let result = result?;
let text = result
.get("content")
.and_then(JsonValue::as_array)
.map(|blocks| {
blocks
.iter()
.filter_map(|block| block.get("text").and_then(JsonValue::as_str))
.collect::<Vec<_>>()
.join("\n")
})
.unwrap_or_default();
if result.get("isError").and_then(JsonValue::as_bool) == Some(true) {
let message = if text.is_empty() {
"Exa reported an error".to_owned()
} else {
text
};
return Err(WebSearchError::new(message));
}
Ok(parse_exa(&text))
}
async fn call_with(
&self,
client: &McpClient,
query: &str,
max_results: usize,
) -> Result<JsonValue, WebSearchError> {
let tools = client.list_tools().await.map_err(|error| {
WebSearchError::new(format!("could not reach Exa's MCP server at {} ({error})", self.url))
})?;
let Some(exa) = tools.iter().find(|tool| tool.name() == EXA_TOOL) else {
return Err(WebSearchError::new(format!(
"Exa's MCP server does not offer {EXA_TOOL}"
)));
};
let options = McpCallToolOptions {
timeout: Some(TIMEOUT),
..McpCallToolOptions::default()
};
client
.call_tool(exa, json!({"query": query, "numResults": max_results}), options)
.await
.map_err(|error| WebSearchError::new(error.to_string()))
}
}
#[async_trait]
impl SearchBackend for ExaBackend {
async fn search(&self, query: &str, max_results: usize) -> Result<Vec<SearchResult>, WebSearchError> {
let key = self.api_key.clone().or_else(|| (self.env)()).unwrap_or_default();
match self.call(query, max_results, &key).await {
Ok(results) => Ok(results),
Err(error) if !key.is_empty() => Err(error),
Err(error) => Err(WebSearchError::new(format!(
"{} (Exa's keyless tier is rate limited; set EXA_API_KEY to lift it.)",
error.message()
))),
}
}
}
#[must_use]
pub fn web_search_tool() -> FunctionTool {
search_tool(Arc::new(ExaBackend::default()))
}
}
#[cfg(test)]
mod tests;