Skip to content

Commit 4d87caf

Browse files
committed
support images
1 parent 1a00176 commit 4d87caf

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

index.ts

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ import { gotScraping } from "got-scraping"
77
import { toMarkdown } from "./markdown"
88
import { YoutubeTranscript } from "./youtube"
99
import { toVideoTimestamp } from "./utils"
10+
import { version } from "./package.json"
1011

1112
const 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

2324
server.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 = /^https?\:\/\//.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) {

tsconfig.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
"lib": ["ESNext", "DOM"],
55
"target": "ESNext",
66
"module": "ESNext",
7-
"moduleDetection": "force",
87
"jsx": "react-jsx",
98
"allowJs": true,
109

0 commit comments

Comments
 (0)