agent-sdk 0.8.0

Rust Agent SDK for building LLM agents
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
//! Link fetch tool implementation.

use crate::tools::{PrimitiveToolName, Tool, ToolContext};
use crate::types::{ToolResult, ToolTier};
use anyhow::{Context, Result, bail};
use serde_json::{Value, json};
use std::time::Duration;

use super::security::UrlValidator;

/// Maximum content size to fetch (1MB).
const MAX_CONTENT_SIZE: usize = 1024 * 1024;

/// Default request timeout.
const DEFAULT_TIMEOUT: Duration = Duration::from_secs(30);

/// Output format for fetched content.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum FetchFormat {
    /// Plain text output (HTML tags removed).
    #[default]
    Text,
    /// Markdown-formatted output.
    Markdown,
}

impl FetchFormat {
    fn from_str(s: &str) -> Option<Self> {
        match s.to_lowercase().as_str() {
            "text" => Some(Self::Text),
            "markdown" | "md" => Some(Self::Markdown),
            _ => None,
        }
    }
}

/// Link fetch tool for securely retrieving web page content.
///
/// This tool fetches web pages and converts them to text or markdown format.
/// It includes SSRF protection to prevent access to internal resources.
///
/// # Example
///
/// ```ignore
/// use agent_sdk::web::LinkFetchTool;
///
/// let tool = LinkFetchTool::new();
///
/// // Register with agent
/// tools.register(tool);
/// ```
pub struct LinkFetchTool {
    client: reqwest::Client,
    validator: UrlValidator,
    default_format: FetchFormat,
}

impl Default for LinkFetchTool {
    fn default() -> Self {
        Self::new()
    }
}

impl LinkFetchTool {
    /// Create a new link fetch tool with default settings.
    ///
    /// # Panics
    ///
    /// Panics if the HTTP client cannot be built (should never happen with default settings).
    #[must_use]
    pub fn new() -> Self {
        let client = reqwest::Client::builder()
            .redirect(reqwest::redirect::Policy::none())
            .timeout(DEFAULT_TIMEOUT)
            .user_agent("Mozilla/5.0 (compatible; AgentSDK/1.0)")
            .build()
            .expect("Failed to build HTTP client");

        Self {
            client,
            validator: UrlValidator::new(),
            default_format: FetchFormat::Text,
        }
    }

    /// Create with a custom URL validator.
    #[must_use]
    pub fn with_validator(mut self, validator: UrlValidator) -> Self {
        self.validator = validator;
        self
    }

    /// Create with a custom HTTP client.
    #[must_use]
    pub fn with_client(mut self, client: reqwest::Client) -> Self {
        self.client = client;
        self
    }

    /// Set the default output format.
    #[must_use]
    pub const fn with_default_format(mut self, format: FetchFormat) -> Self {
        self.default_format = format;
        self
    }

    /// Fetch a URL and convert to the specified format.
    ///
    /// Manually follows redirects, validating each target URL through the
    /// SSRF validator to prevent redirect-based SSRF attacks.
    async fn fetch_url(&self, url_str: &str, format: FetchFormat) -> Result<String> {
        // Validate initial URL before fetching
        let mut url = self.validator.validate(url_str)?;
        let max_redirects = self.validator.max_redirects();

        let mut response = self
            .client
            .get(url.as_str())
            .send()
            .await
            .context("Failed to fetch URL")?;

        // Manually follow redirects with validation
        let mut redirects = 0;
        while response.status().is_redirection() {
            redirects += 1;
            if redirects > max_redirects {
                bail!("Too many redirects ({redirects} > {max_redirects})");
            }

            let location = response
                .headers()
                .get(reqwest::header::LOCATION)
                .context("Redirect response missing Location header")?
                .to_str()
                .context("Invalid Location header")?;

            // Resolve relative redirect URLs against the current URL
            let redirect_url_str = url
                .join(location)
                .map_or_else(|_| location.to_string(), |u| u.to_string());

            // Validate the redirect target through the same SSRF checks
            url = self.validator.validate(&redirect_url_str)?;

            response = self
                .client
                .get(url.as_str())
                .send()
                .await
                .context("Failed to follow redirect")?;
        }

        // Check status
        if !response.status().is_success() {
            bail!("HTTP error: {}", response.status());
        }

        // Check content length if available
        if let Some(len) = response.content_length()
            && len > MAX_CONTENT_SIZE as u64
        {
            bail!("Content too large: {len} bytes (max {MAX_CONTENT_SIZE} bytes)");
        }

        // Get content type to determine processing
        let content_type = response
            .headers()
            .get(reqwest::header::CONTENT_TYPE)
            .and_then(|v| v.to_str().ok())
            .unwrap_or("text/html")
            .to_string();

        // Read body with size limit
        let bytes = response
            .bytes()
            .await
            .context("Failed to read response body")?;

        if bytes.len() > MAX_CONTENT_SIZE {
            bail!(
                "Content too large: {} bytes (max {} bytes)",
                bytes.len(),
                MAX_CONTENT_SIZE
            );
        }

        // Convert to string
        let html = String::from_utf8_lossy(&bytes);

        // Process based on content type and format
        if content_type.contains("text/html") || content_type.contains("application/xhtml") {
            Ok(convert_html(&html, format))
        } else if content_type.contains("text/plain") {
            Ok(html.into_owned())
        } else {
            // For other content types, just return as-is
            Ok(html.into_owned())
        }
    }
}

/// Convert HTML to the specified format.
fn convert_html(html: &str, format: FetchFormat) -> String {
    let result = match format {
        FetchFormat::Text => {
            // Use html2text with default width
            html2text::from_read(html.as_bytes(), 80)
        }
        FetchFormat::Markdown => {
            // Use html2text with markdown-friendly settings
            html2text::from_read(html.as_bytes(), 80)
        }
    };
    result.unwrap_or_else(|_| html.to_string())
}

impl<Ctx> Tool<Ctx> for LinkFetchTool
where
    Ctx: Send + Sync + 'static,
{
    type Name = PrimitiveToolName;

    fn name(&self) -> PrimitiveToolName {
        PrimitiveToolName::LinkFetch
    }

    fn display_name(&self) -> &'static str {
        "Fetch URL"
    }

    fn description(&self) -> &'static str {
        "Fetch and read web page content. Returns the page content as text or markdown. \
         Includes SSRF protection to prevent access to internal resources."
    }

    fn input_schema(&self) -> Value {
        json!({
            "type": "object",
            "properties": {
                "url": {
                    "type": "string",
                    "description": "The URL to fetch (must be HTTPS)"
                },
                "format": {
                    "type": "string",
                    "enum": ["text", "markdown"],
                    "description": "Output format (default: text)"
                }
            },
            "required": ["url"]
        })
    }

    fn tier(&self) -> ToolTier {
        // Link fetch is read-only, so Observe tier
        ToolTier::Observe
    }

    async fn execute(&self, _ctx: &ToolContext<Ctx>, input: Value) -> Result<ToolResult> {
        let url = input
            .get("url")
            .and_then(Value::as_str)
            .context("Missing 'url' parameter")?;

        let format = input
            .get("format")
            .and_then(Value::as_str)
            .and_then(FetchFormat::from_str)
            .unwrap_or(self.default_format);

        match self.fetch_url(url, format).await {
            Ok(content) => Ok(ToolResult {
                success: true,
                output: content,
                data: Some(json!({ "url": url, "format": format_name(format) })),
                documents: Vec::new(),
                duration_ms: None,
            }),
            Err(e) => Ok(ToolResult {
                success: false,
                output: format!("Failed to fetch URL: {e}"),
                data: Some(json!({ "url": url, "error": e.to_string() })),
                documents: Vec::new(),
                duration_ms: None,
            }),
        }
    }
}

/// Get the format name for JSON output.
const fn format_name(format: FetchFormat) -> &'static str {
    match format {
        FetchFormat::Text => "text",
        FetchFormat::Markdown => "markdown",
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_link_fetch_tool_metadata() {
        let tool = LinkFetchTool::new();

        assert_eq!(Tool::<()>::name(&tool), PrimitiveToolName::LinkFetch);
        assert!(Tool::<()>::description(&tool).contains("Fetch"));
        assert_eq!(Tool::<()>::tier(&tool), ToolTier::Observe);
    }

    #[test]
    fn test_link_fetch_tool_input_schema() {
        let tool = LinkFetchTool::new();

        let schema = Tool::<()>::input_schema(&tool);
        assert_eq!(schema["type"], "object");
        assert!(schema["properties"]["url"].is_object());
        assert!(schema["properties"]["format"].is_object());
        assert!(
            schema["required"]
                .as_array()
                .is_some_and(|arr| arr.iter().any(|v| v == "url"))
        );
    }

    #[test]
    fn test_format_from_str() {
        assert_eq!(FetchFormat::from_str("text"), Some(FetchFormat::Text));
        assert_eq!(FetchFormat::from_str("TEXT"), Some(FetchFormat::Text));
        assert_eq!(
            FetchFormat::from_str("markdown"),
            Some(FetchFormat::Markdown)
        );
        assert_eq!(FetchFormat::from_str("md"), Some(FetchFormat::Markdown));
        assert_eq!(FetchFormat::from_str("invalid"), None);
    }

    #[test]
    fn test_convert_html_text() {
        let html = "<html><body><h1>Title</h1><p>Paragraph</p></body></html>";
        let result = convert_html(html, FetchFormat::Text);
        assert!(result.contains("Title"));
        assert!(result.contains("Paragraph"));
    }

    #[test]
    fn test_default_format() {
        let tool = LinkFetchTool::new();
        assert_eq!(tool.default_format, FetchFormat::Text);

        let tool = LinkFetchTool::new().with_default_format(FetchFormat::Markdown);
        assert_eq!(tool.default_format, FetchFormat::Markdown);
    }

    #[tokio::test]
    async fn test_link_fetch_blocked_url() {
        let tool = LinkFetchTool::new();
        let ctx = ToolContext::new(());
        let input = json!({ "url": "http://localhost:8080" });

        let result = Tool::<()>::execute(&tool, &ctx, input).await;
        assert!(result.is_ok());

        let tool_result = result.expect("Should succeed");
        assert!(!tool_result.success);
        assert!(
            tool_result.output.contains("HTTPS required") || tool_result.output.contains("blocked")
        );
    }

    #[tokio::test]
    async fn test_link_fetch_missing_url() {
        let tool = LinkFetchTool::new();
        let ctx = ToolContext::new(());
        let input = json!({});

        let result = Tool::<()>::execute(&tool, &ctx, input).await;
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("url"));
    }

    #[tokio::test]
    async fn test_link_fetch_invalid_url() {
        let tool = LinkFetchTool::new();
        let ctx = ToolContext::new(());
        let input = json!({ "url": "not-a-valid-url" });

        let result = Tool::<()>::execute(&tool, &ctx, input).await;
        assert!(result.is_ok());

        let tool_result = result.expect("Should succeed");
        assert!(!tool_result.success);
        assert!(tool_result.output.contains("Invalid URL"));
    }

    #[test]
    fn test_with_validator() {
        let validator = UrlValidator::new().with_allow_http();
        let _tool = LinkFetchTool::new().with_validator(validator);
        // Just verify it compiles - validator is private
    }

    #[test]
    fn test_format_name() {
        assert_eq!(format_name(FetchFormat::Text), "text");
        assert_eq!(format_name(FetchFormat::Markdown), "markdown");
    }

    #[test]
    fn test_redirects_disabled_in_client() {
        // Verify that the default client has redirects disabled
        // (reqwest::Policy::none means no automatic redirect following)
        let tool = LinkFetchTool::new();
        // The client is private, but we can verify redirect behavior indirectly:
        // A redirect response should NOT be automatically followed
        assert_eq!(tool.validator.max_redirects(), 3);
    }

    #[tokio::test]
    async fn test_redirect_to_private_ip_blocked() {
        // Simulate: a redirect target pointing to a private IP should be blocked
        // by the validator during manual redirect following.
        let validator = UrlValidator::new().with_allow_http();

        // Direct access to private IPs should be blocked
        let result = validator.validate("http://169.254.169.254/latest/meta-data/");
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("blocked"));

        // Direct access to 10.x should be blocked
        let result = validator.validate("http://10.0.0.1/internal");
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_redirect_to_localhost_blocked() {
        let validator = UrlValidator::new().with_allow_http();

        // Redirect target pointing to localhost should be blocked
        let result = validator.validate("http://127.0.0.1/admin");
        assert!(result.is_err());
    }
}