Skip to main content

adk_tool/builtin/
url_context.rs

1use crate::builtin::bypass::BypassMultiToolsLimit;
2use adk_core::{Result, Tool, ToolContext};
3use async_trait::async_trait;
4use serde_json::{Value, json};
5use std::sync::Arc;
6
7/// UrlContext is a built-in tool that is automatically invoked by Gemini
8/// models to fetch and analyze content from URLs.
9/// The tool operates internally within the model and does not require or
10/// perform local code execution.
11#[derive(Default)]
12pub struct UrlContextTool;
13
14impl UrlContextTool {
15    /// Create a new `UrlContextTool`.
16    pub fn new() -> Self {
17        Self
18    }
19}
20
21/// Bypass support: convert the built-in URL-context tool into a
22/// function-calling tool so it can be used alongside custom function tools
23/// under the Gemini Interactions API.
24///
25/// The converted tool declares a `url: string` function schema and performs the
26/// fetch-and-analyze behaviour by delegating to an internal single-turn agent
27/// (an `LlmAgent` configured with [`UrlContextTool`] and a Gemini model).
28///
29/// # Example
30///
31/// ```rust,ignore
32/// use adk_tool::{BypassMultiToolsLimit, UrlContextTool};
33/// use std::sync::Arc;
34///
35/// // `url_agent` is an LlmAgent with UrlContextTool + a Gemini model.
36/// let tool = UrlContextTool::new().with_bypass_multi_tools_limit(Arc::new(url_agent));
37/// assert!(!tool.is_builtin());
38/// ```
39impl BypassMultiToolsLimit for UrlContextTool {
40    fn bypass_name(&self) -> String {
41        self.name().to_string()
42    }
43
44    fn bypass_description(&self) -> String {
45        "Fetches and analyzes content from a URL to provide context.".to_string()
46    }
47
48    fn bypass_parameters_schema(&self) -> Value {
49        json!({
50            "type": "object",
51            "properties": {
52                "url": {
53                    "type": "string",
54                    "description": "The URL whose content should be fetched and analyzed."
55                }
56            },
57            "required": ["url"]
58        })
59    }
60
61    fn bypass_query_field(&self) -> String {
62        "url".to_string()
63    }
64}
65
66#[async_trait]
67impl Tool for UrlContextTool {
68    fn name(&self) -> &str {
69        "url_context"
70    }
71
72    fn description(&self) -> &str {
73        "Fetches and analyzes content from URLs to provide context."
74    }
75
76    fn is_builtin(&self) -> bool {
77        true
78    }
79
80    fn declaration(&self) -> Value {
81        json!({
82            "name": self.name(),
83            "description": self.description(),
84            "x-adk-gemini-tool": {
85                "url_context": {}
86            }
87        })
88    }
89
90    async fn execute(&self, _ctx: Arc<dyn ToolContext>, _args: Value) -> Result<Value> {
91        Err(adk_core::AdkError::tool("UrlContext is handled internally by Gemini"))
92    }
93}