browser_use/tools/
close.rs

1use crate::error::{BrowserError, Result};
2use crate::tools::{Tool, ToolContext, ToolResult};
3use schemars::JsonSchema;
4use serde::{Deserialize, Serialize};
5
6/// Parameters for the close tool (no parameters needed)
7#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
8pub struct CloseParams {}
9
10/// Tool for closing the browser
11#[derive(Default)]
12pub struct CloseTool;
13
14impl Tool for CloseTool {
15    type Params = CloseParams;
16
17    fn name(&self) -> &str {
18        "close"
19    }
20
21    fn execute_typed(&self, _params: CloseParams, context: &mut ToolContext) -> Result<ToolResult> {
22        // Note: Closing the browser via BrowserSession is tricky because we hold a reference
23        // In a real implementation, this would need to signal the session owner to close
24        // For now, we'll close all tabs as a proxy for closing the browser
25
26        context
27            .session
28            .close()
29            .map_err(|e| BrowserError::ToolExecutionFailed {
30                tool: "close".to_string(),
31                reason: e.to_string(),
32            })?;
33
34        Ok(ToolResult::success_with(serde_json::json!({
35            "message": "Browser closed successfully"
36        })))
37    }
38}