@@ -8,6 +8,7 @@ import (
88 "net/http"
99 "strings"
1010 "time"
11+ "unicode/utf8"
1112
1213 md "github.com/JohannesKaufmann/html-to-markdown"
1314 "github.com/PuerkitoBio/goquery"
@@ -182,6 +183,11 @@ func (t *fetchTool) Run(ctx context.Context, call ToolCall) (ToolResponse, error
182183 }
183184
184185 content := string(body)
186+
187+ isValidUt8 := utf8.ValidString(content)
188+ if !isValidUt8 {
189+ return NewTextErrorResponse("Response content is not valid UTF-8"), nil
190+ }
185191 contentType := resp.Header.Get("Content-Type")
186192
187193 switch format {
@@ -191,9 +197,8 @@ func (t *fetchTool) Run(ctx context.Context, call ToolCall) (ToolResponse, error
191197 if err != nil {
192198 return NewTextErrorResponse("Failed to extract text from HTML: " + err.Error()), nil
193199 }
194− return NewTextResponse(text), nil
200+ content = text
195201 }
196− return NewTextResponse(content), nil
197202
198203 case "markdown":
199204 if strings.Contains(contentType, "text/html") {
@@ -201,17 +206,36 @@ func (t *fetchTool) Run(ctx context.Context, call ToolCall) (ToolResponse, error
201206 if err != nil {
202207 return NewTextErrorResponse("Failed to convert HTML to Markdown: " + err.Error()), nil
203208 }
204− return NewTextResponse(markdown), nil
209+ content = markdown
205210 }
206211
207− return NewTextResponse("```\n" + content + "\n```"), nil
212+ content = "```\n" + content + "\n```"
208213
209214 case "html":
210− return NewTextResponse(content), nil
211−
212− default:
213− return NewTextResponse(content), nil
215+ // return only the body of the HTML document
216+ if strings.Contains(contentType, "text/html") {
217+ doc, err := goquery.NewDocumentFromReader(strings.NewReader(content))
218+ if err != nil {
219+ return NewTextErrorResponse("Failed to parse HTML: " + err.Error()), nil
220+ }
221+ body, err := doc.Find("body").Html()
222+ if err != nil {
223+ return NewTextErrorResponse("Failed to extract body from HTML: " + err.Error()), nil
224+ }
225+ if body == "" {
226+ return NewTextErrorResponse("No body content found in HTML"), nil
227+ }
228+ content = "<html>\n<body>\n" + body + "\n</body>\n</html>"
229+ }
230+ }
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)
214236 }
237+
238+ return NewTextResponse(content), nil
215239}
216240
217241func extractTextFromHTML(html string) (string, error) {
@@ -220,7 +244,7 @@ func extractTextFromHTML(html string) (string, error) {
220244 return "", err
221245 }
222246
223− text := doc.Text()
247+ text := doc.Find("body").Text()
224248 text = strings.Join(strings.Fields(text), " ")
225249
226250 return text, nil