Skip to main content

adk_tool/
lib.rs

1//! # adk-tool
2//!
3//! Tool system for ADK agents: typed Rust tools, toolset composition, hosted
4//! provider tools, and Model Context Protocol clients and servers.
5//!
6//! ## Overview
7//!
8//! This crate provides the tool infrastructure for ADK agents:
9//!
10//! - [`macro@tool`] - Turn an async function into a [`Tool`] whose arguments are
11//!   checked against the derived schema before it runs
12//! - [`FunctionTool`] - Create tools from async Rust functions
13//! - [`StatefulTool`] - Share `Arc<S>` state with a tool handler
14//! - [`AgentTool`] - Use agents as callable tools for composition
15//! - [`AdvisorTool`] - Let an agent consult a stronger or different model for advice
16//!   mid-task, under the run's budget, spend ledger and kill switch
17//! - [`GoogleSearchTool`] and the other [`builtin`] wrappers - Provider-hosted tools,
18//!   each declaring its [`ToolEffect`]
19//! - `McpToolset` - MCP tools, resources, prompts, completion, elicitation,
20//!   subscriptions, and negotiated tasks with the `mcp` feature
21//! - `McpServerManager` - Dynamic local MCP server registry, process lifecycle,
22//!   persistence, health monitoring, and bounded restart with the `mcp` feature
23//! - [`BasicToolset`], [`FilteredToolset`], [`MergedToolset`], [`PrefixedToolset`] -
24//!   Group, filter, combine, and namespace toolsets
25//! - [`ToolArgumentValidator`] - Check hand-written tools' arguments against a JSON Schema
26//! - [`ExitLoopTool`] - Control flow for loop agents
27//! - [`LoadArtifactsTool`] - Inject binary artifacts into context
28//!
29//! ## Quick Start
30//!
31//! ```rust
32//! use adk_core::{Result, ToolContext};
33//! use adk_tool::FunctionTool;
34//! use schemars::JsonSchema;
35//! use serde::Serialize;
36//! use serde_json::{Value, json};
37//! use std::sync::Arc;
38//!
39//! #[derive(Serialize, JsonSchema)]
40//! struct WeatherParams {
41//!     /// The city to get weather for
42//!     city: String,
43//! }
44//!
45//! async fn get_weather(_ctx: Arc<dyn ToolContext>, args: Value) -> Result<Value> {
46//!     let city = args["city"].as_str().unwrap_or("Unknown");
47//!     Ok(json!({
48//!         "city": city,
49//!         "temperature": 22,
50//!         "condition": "sunny"
51//!     }))
52//! }
53//!
54//! let tool = FunctionTool::new("get_weather", "Get current weather for a city", get_weather)
55//!     .with_parameters_schema::<WeatherParams>();
56//! ```
57//!
58//! ## MCP Integration
59//!
60//! Connect to MCP servers for external tools (requires the `mcp` feature):
61//!
62//! ```rust,no_run
63//! # #[cfg(feature = "mcp")]
64//! # async fn connect() -> Result<(), Box<dyn std::error::Error>> {
65//! use adk_tool::{
66//!     McpToolset,
67//!     mcp::rmcp::{ServiceExt, transport::TokioChildProcess},
68//! };
69//! use tokio::process::Command;
70//!
71//! let mut command = Command::new("/opt/company/bin/workspace-mcp");
72//! command.arg("--stdio").arg("--root").arg("/srv/workspace");
73//! let client = ().serve(TokioChildProcess::new(command)?).await?;
74//!
75//! let toolset = McpToolset::new(client);
76//! # Ok(())
77//! # }
78//! ```
79//!
80//! ## Features
81//!
82//! No feature is on by default; function tools, agent tools and toolsets need none. MCP
83//! (`mcp`, `http-transport`, `mcp-server`, `mcp-sampling`, `mcp-apps`), memory tools
84//! (`memory-tools`, `graph-memory-tools`), code execution (`code`, `code-embedded-js`,
85//! `code-embedded-python`, `code-docker`), `slack`, `bigquery`, `spanner`, `example-store` and
86//! `vertex-agent-registry` each have a feature; the
87//! [README](https://github.com/zavora-ai/adk-rust/blob/main/adk-tool/README.md#features)
88//! says what each enables.
89
90#![deny(missing_docs)]
91
92pub mod advisor;
93mod agent_tool;
94/// Argument validation against a tool's parameters schema.
95mod arguments;
96/// Built-in tool wrappers for Gemini, OpenAI, and Anthropic hosted tools.
97pub mod builtin;
98mod function_tool;
99#[cfg(feature = "mcp")]
100/// Model Context Protocol (MCP) clients, server SDK re-export, catalog APIs,
101/// elicitation, tasks, HTTP transport, and dynamic local-server management.
102pub mod mcp;
103mod simple_context;
104mod stateful_tool;
105/// Toolset combinators: basic, filtered, merged, and prefixed toolsets.
106pub mod toolset;
107
108#[cfg(feature = "code")]
109pub mod code_execution;
110
111#[cfg(feature = "memory-tools")]
112pub mod memory;
113
114#[cfg(feature = "graph-memory-tools")]
115pub use memory::{GraphMemoryToolset, RelateTool, RememberTool};
116
117#[cfg(feature = "slack")]
118pub mod slack;
119
120#[cfg(feature = "bigquery")]
121pub mod bigquery;
122
123#[cfg(feature = "spanner")]
124pub mod spanner;
125
126#[cfg(feature = "mcp-sampling")]
127pub mod sampling;
128
129#[cfg(feature = "example-store")]
130pub mod example_store;
131
132#[cfg(feature = "vertex-agent-registry")]
133pub mod vertex;
134
135pub use adk_core::{
136    AdkError, ErrorCategory, ErrorComponent, Result, TOOL_INVALID_ARGUMENTS_CODE, Tool,
137    ToolContext, ToolEffect, Toolset,
138};
139pub use adk_rust_macros::tool;
140
141// Re-export async_trait so the #[tool] macro's generated code can reference it
142// without requiring users to add async-trait as a direct dependency.
143pub use advisor::{AdvisorTool, AdvisorToolBuilder};
144pub use agent_tool::{
145    AgentTool, AgentToolConfig, AgentToolFailureMode, AgentToolSessionSnapshot,
146    AgentToolStateMergePolicy,
147};
148pub use arguments::{
149    TOOL_INVALID_PARAMETERS_SCHEMA_CODE, ToolArgumentValidator, close_object_schemas,
150    coerce_integral_floats, model_facing_schema,
151};
152pub use async_trait::async_trait;
153pub use builtin::{
154    AnthropicAdvisorTool, AnthropicBashTool20241022, AnthropicBashTool20250124,
155    AnthropicTextEditorTool20250124, AnthropicTextEditorTool20250429,
156    AnthropicTextEditorTool20250728, BypassBuiltinTool, BypassMultiToolsLimit,
157    DeepSeekWebSearchTool, ExitLoopTool, GeminiCodeExecutionTool, GeminiComputerEnvironment,
158    GeminiComputerUseTool, GeminiFileSearchTool, GoogleMapsContext, GoogleMapsTool,
159    GoogleSearchTool, LoadArtifactsTool, OpenAIApplyPatchTool, OpenAIApproximateLocation,
160    OpenAICodeInterpreterTool, OpenAIComputerEnvironment, OpenAIComputerUseTool,
161    OpenAIFileSearchTool, OpenAIImageGenerationTool, OpenAILocalShellTool, OpenAIMcpTool,
162    OpenAIShellTool, OpenAIWebSearchTool, UrlContextTool, WebSearchTool, WebSearchUserLocation,
163};
164#[cfg(feature = "example-store")]
165pub use example_store::{ExampleStoreClient, ExampleStoreConfig, ExampleStoreProvider};
166pub use function_tool::FunctionTool;
167#[cfg(feature = "mcp")]
168pub use mcp::{
169    AutoDeclineElicitationHandler, ElicitationHandler, McpAuth, McpDiscoveryLimits,
170    McpHttpClientBuilder, McpSchemaLimits, McpServerManager, McpTaskConfig, McpToolset,
171    OAuth2Config, Resource, ResourceContents, ResourceNotificationHandler, ResourceTemplate,
172};
173pub use simple_context::SimpleToolContext;
174pub use stateful_tool::StatefulTool;
175pub use toolset::{
176    BasicToolset, FilteredToolset, MergedToolset, PrefixedToolset, string_predicate,
177};
178#[cfg(feature = "vertex-agent-registry")]
179pub use vertex::agent_registry::{AgentRegistryClient, AgentRegistryConfig, AgentSearchTool};
180
181#[cfg(feature = "code")]
182pub use code_execution::CodeTool;
183
184#[cfg(feature = "code")]
185pub use code_execution::FrontendCodeTool;
186
187#[cfg(feature = "code")]
188pub use code_execution::JavaScriptCodeTool;
189
190#[cfg(feature = "code")]
191pub use code_execution::PythonCodeTool;
192
193#[cfg(feature = "code")]
194pub use code_execution::MontyPythonCodeTool;
195
196#[cfg(feature = "code-embedded-python")]
197pub use code_execution::MontyPythonCodeToolBuilder;