@@ -7,11 +7,12 @@ import { gotScraping } from "got-scraping"
77import { toMarkdown } from "./markdown"
88import { YoutubeTranscript } from "./youtube"
99import { toVideoTimestamp } from "./utils"
10+ import { version } from "./package.json"
1011
1112const server = new McpServer (
1213 {
1314 name : "fetch-mcp" ,
14- version : "0.0.0" ,
15+ version,
1516 } ,
1617 {
1718 capabilities : {
@@ -22,13 +23,13 @@ const server = new McpServer(
2223
2324server . tool (
2425 "fetch_url" ,
25- "Fetch a URL" ,
26+ "Fetch a URL, support HTML, text, and image " ,
2627 {
2728 url : z . string ( ) . describe ( "The URL to fetch" ) ,
2829 raw : z
2930 . boolean ( )
3031 . nullish ( )
31- . describe ( "Return raw HTML instead of Markdown" )
32+ . describe ( "Return raw HTML instead of Markdown for HTML pages " )
3233 . default ( false ) ,
3334 max_length : z
3435 . number ( )
@@ -43,10 +44,32 @@ server.tool(
4344 const url = / ^ h t t p s ? \: \/ \/ / . test ( args . url )
4445 ? args . url
4546 : `https://${ args . url } `
47+
4648 const res = await gotScraping ( url )
4749
4850 if ( res . ok ) {
49- let content = args . raw ? res . body : toMarkdown ( res . body )
51+ const contentType = res . headers [ "content-type" ]
52+
53+ if ( ! contentType || ! contentType . includes ( "text/" ) ) {
54+ const isImage = contentType ?. includes ( "image/" )
55+ return {
56+ content : [
57+ isImage && contentType
58+ ? {
59+ type : "image" ,
60+ data : res . rawBody . toString ( "base64" ) ,
61+ mimeType : contentType ,
62+ }
63+ : {
64+ type : "text" ,
65+ text : `Unsupported mime type: ${ contentType } ` ,
66+ } ,
67+ ] ,
68+ }
69+ }
70+
71+ const isHTML = contentType . includes ( "html" )
72+ let content = args . raw || ! isHTML ? res . body : toMarkdown ( res . body )
5073 let remainingContentLength = 0
5174
5275 if ( args . start_index ) {
0 commit comments