1pub mod handler;
6pub use handler::BrowserServer;
7
8use crate::tools::{self, Tool, ToolContext, ToolResult as InternalToolResult};
9use rmcp::{
10 ErrorData as McpError,
11 handler::server::wrapper::Parameters,
12 model::{CallToolResult, Content},
13 tool, tool_router,
14};
15
16fn convert_result(result: InternalToolResult) -> Result<CallToolResult, McpError> {
18 if result.success {
19 let text = if let Some(data) = result.data {
20 serde_json::to_string_pretty(&data).unwrap_or_else(|_| data.to_string())
21 } else {
22 "Success".to_string()
23 };
24 Ok(CallToolResult::success(vec![Content::text(text)]))
25 } else {
26 let error_msg = result.error.unwrap_or_else(|| "Unknown error".to_string());
27 Err(McpError::internal_error(error_msg, None))
28 }
29}
30
31macro_rules! register_mcp_tools {
33 ($($mcp_name:ident => $tool_type:ty, $description:expr);* $(;)?) => {
34 #[tool_router]
35 impl BrowserServer {
36 $(
37 #[tool(description = $description)]
38 fn $mcp_name(
39 &self,
40 params: Parameters<<$tool_type as Tool>::Params>,
41 ) -> Result<CallToolResult, McpError> {
42 let session = self.session();
43 let mut context = ToolContext::new(&*session);
44 let tool = <$tool_type>::default();
45 let result = tool.execute_typed(params.0, &mut context)
46 .map_err(|e| McpError::internal_error(e.to_string(), None))?;
47 convert_result(result)
48 }
49 )*
50 }
51 };
52}
53
54register_mcp_tools! {
56 browser_navigate => tools::navigate::NavigateTool, "Navigate to a specified URL in the browser";
58 browser_go_back => tools::go_back::GoBackTool, "Navigate back in browser history";
59 browser_go_forward => tools::go_forward::GoForwardTool, "Navigate forward in browser history";
60 browser_close => tools::close::CloseTool, "Close the browser when the task is complete";
61
62 browser_get_markdown => tools::markdown::GetMarkdownTool, "Get the markdown content of the current page (use this tool only for information extraction; for interaction use the snapshot tool instead)";
64 browser_snapshot => tools::snapshot::SnapshotTool, "Get a snapshot of the current page with indexed interactive elements for interaction";
65 browser_screenshot => tools::screenshot::ScreenshotTool, "Capture a screenshot of the current page";
66 browser_evaluate => tools::evaluate::EvaluateTool, "Execute JavaScript code in the browser context";
68
69 browser_click => tools::click::ClickTool, "Click on an element specified by CSS selector or index (index obtained from browser_snapshot tool)";
71 browser_hover => tools::hover::HoverTool, "Hover over an element specified by CSS selector or index (index obtained from browser_snapshot tool)";
72 browser_select => tools::select::SelectTool, "Select an option in a dropdown element by CSS selector or index (index obtained from browser_snapshot tool)";
73 browser_input_fill => tools::input::InputTool, "Type text into an input element specified by CSS selector or index (index obtained from browser_snapshot tool)";
74 browser_press_key => tools::press_key::PressKeyTool, "Press a key on the keyboard";
75 browser_scroll => tools::scroll::ScrollTool, "Scroll the page by a specified amount or to the bottom";
76 browser_wait => tools::wait::WaitTool, "Wait for an element to appear on the page";
77
78 browser_new_tab => tools::new_tab::NewTabTool, "Open a new tab and navigate to the specified URL";
80 browser_tab_list => tools::tab_list::TabListTool, "Get the list of all browser tabs with their titles and URLs";
81 browser_switch_tab => tools::switch_tab::SwitchTabTool, "Switch to a specific tab by index";
82 browser_close_tab => tools::close_tab::CloseTabTool, "Close the current active tab";
83}