File size: 1,040 Bytes
68b80a4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from .tool import Tool
from markdownify import markdownify
import requests

class FetchTool(Tool):
    def __init__(self):
        super().__init__(
            name="fetch",
            description="Fetch the content of a URL and return the markdownified version of the content",
            inputSchema={
                "type": "object",
                "properties": {
                    "url": {"type": "string", "description": "The URL to fetch"}
                }
            }
        )
    
    def __call__(self, url: str):
        try:
            if not url:
                return "Error: URL parameter is required"
                
            resp = requests.get(url)
            resp.raise_for_status()  # Raise an exception for bad status codes
            
            return markdownify(resp.text)
            
        except requests.exceptions.RequestException as e:
            return f"Error fetching URL: {str(e)}"
        except Exception as e:
            return f"Unexpected error while processing URL: {str(e)}"