[docs]classNavigateToolInput(BaseModel):"""Input for NavigateToolInput."""url:str=Field(...,description="url to navigate to")@model_validator(mode="before")@classmethoddefvalidate_url_scheme(cls,values:dict)->dict:"""Check that the URL scheme is valid."""url=values.get("url")parsed_url=urlparse(url)ifparsed_url.schemenotin("http","https"):raiseValueError("URL scheme must be 'http' or 'https'")returnvalues
[docs]classNavigateTool(BaseBrowserTool):# type: ignore[override, override]"""Tool for navigating a browser to a URL. **Security Note**: This tool provides code to control web-browser navigation. This tool can navigate to any URL, including internal network URLs, and URLs exposed on the server itself. However, if exposing this tool to end-users, consider limiting network access to the server that hosts the agent. By default, the URL scheme has been limited to 'http' and 'https' to prevent navigation to local file system URLs (or other schemes). If access to the local file system is required, consider creating a custom tool or providing a custom args_schema that allows the desired URL schemes. See https://python.langchain.com/docs/security for more information. """name:str="navigate_browser"description:str="Navigate a browser to the specified URL"args_schema:Type[BaseModel]=NavigateToolInputdef_run(self,url:str,run_manager:Optional[CallbackManagerForToolRun]=None,)->str:"""Use the tool."""ifself.sync_browserisNone:raiseValueError(f"Synchronous browser not provided to {self.name}")page=get_current_page(self.sync_browser)response=page.goto(url)status=response.statusifresponseelse"unknown"returnf"Navigating to {url} returned status code {status}"asyncdef_arun(self,url:str,run_manager:Optional[AsyncCallbackManagerForToolRun]=None,)->str:"""Use the tool."""ifself.async_browserisNone:raiseValueError(f"Asynchronous browser not provided to {self.name}")page=awaitaget_current_page(self.async_browser)response=awaitpage.goto(url)status=response.statusifresponseelse"unknown"returnf"Navigating to {url} returned status code {status}"