@@ -8,6 +8,7 @@ import (
12 md "github.com/JohannesKaufmann/html-to-markdown"
13 md "github.com/JohannesKaufmann/html-to-markdown"
13 "github.com/PuerkitoBio/goquery"
14 "github.com/PuerkitoBio/goquery"
@@ -182,6 +183,11 @@ func (t *fetchTool) Run(ctx context.Context, call ToolCall) (ToolResponse, error
184 content := string(body)
185 content := string(body)
187+ isValidUt8 := utf8.ValidString(content)
189+ return NewTextErrorResponse("Response content is not valid UTF-8"), nil
185 contentType := resp.Header.Get("Content-Type")
191 contentType := resp.Header.Get("Content-Type")
187 switch format {
193 switch format {
@@ -191,9 +197,8 @@ func (t *fetchTool) Run(ctx context.Context, call ToolCall) (ToolResponse, error
191 if err != nil {
197 if err != nil {
192 return NewTextErrorResponse("Failed to extract text from HTML: " + err.Error()), nil
198 return NewTextErrorResponse("Failed to extract text from HTML: " + err.Error()), nil
194− return NewTextResponse(text), nil
200+ content = text
196− return NewTextResponse(content), nil
198 case "markdown":
203 case "markdown":
199 if strings.Contains(contentType, "text/html") {
204 if strings.Contains(contentType, "text/html") {
@@ -201,17 +206,36 @@ func (t *fetchTool) Run(ctx context.Context, call ToolCall) (ToolResponse, error
201 if err != nil {
206 if err != nil {
202 return NewTextErrorResponse("Failed to convert HTML to Markdown: " + err.Error()), nil
207 return NewTextErrorResponse("Failed to convert HTML to Markdown: " + err.Error()), nil
204− return NewTextResponse(markdown), nil
209+ content = markdown
207− return NewTextResponse("```\n" + content + "\n```"), nil
212+ content = "```\n" + content + "\n```"
209 case "html":
214 case "html":
210− return NewTextResponse(content), nil
215+ // return only the body of the HTML document
211−
216+ if strings.Contains(contentType, "text/html") {
212− default:
217+ doc, err := goquery.NewDocumentFromReader(strings.NewReader(content))
213− return NewTextResponse(content), nil
218+ if err != nil {
219+ return NewTextErrorResponse("Failed to parse HTML: " + err.Error()), nil
221+ body, err := doc.Find("body").Html()
223+ return NewTextErrorResponse("Failed to extract body from HTML: " + err.Error()), nil
226+ return NewTextErrorResponse("No body content found in HTML"), nil
228+ content = "<html>\n<body>\n" + body + "\n</body>\n</html>"
231+ // calculate byte size of content
232+ contentSize := int64(len(content))
233+ if contentSize > MaxReadSize {
234+ content = content[:MaxReadSize]
235+ content += fmt.Sprintf("\n\n[Content truncated to %d bytes]", MaxReadSize)
238+ return NewTextResponse(content), nil
217func extractTextFromHTML(html string) (string, error) {
241func extractTextFromHTML(html string) (string, error) {
@@ -220,7 +244,7 @@ func extractTextFromHTML(html string) (string, error) {
220 return "", err
244 return "", err
223− text := doc.Text()
247+ text := doc.Find("body").Text()
224 text = strings.Join(strings.Fields(text), " ")
248 text = strings.Join(strings.Fields(text), " ")
226 return text, nil
250 return text, nil