File size: 917 Bytes
325b94c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from crewai.tools import BaseTool
from typing import Literal

import requests

class URLValidatorTool(BaseTool):
    name: Literal["url_validator"]
    description : str = "Validate whether a given URL is reachable and returns a valid HTTP status."

    def _run(self, url: str) -> dict:
        """Synchronous tool execution"""
        try:
            response = requests.head(url, allow_redirects=True, timeout=15)
            return {
                "url": url,
                "status_code": response.status_code,
                "ok": response.status_code < 400
            }
        except Exception as e:
            return {
                "url": url,
                "status_code": None,
                "ok": False,
                "error": str(e)
            }

    async def _arun(self, url: str) -> dict:
        """Asynchronous tool execution (not used but required)"""
        return self._run(url)