MCPcopy Create free account
hub / github.com/arctic-cli/interface / execute

Function execute

packages/arctic/src/tool/webfetch.ts:21–142  ·  view source on GitHub ↗
(params, ctx)

Source from the content-addressed store, hash-verified

19 timeout: z.number().describe("Optional timeout in seconds (max 120)").optional(),
20 }),
21 async execute(params, ctx) {
22 // Validate URL
23 if (!params.url.startsWith("http://") && !params.url.startsWith("https://")) {
24 throw new Error("URL must start with http:// or https://")
25 }
26
27 const cfg = await Config.get()
28 if (cfg.permission?.webfetch === "ask")
29 await Permission.ask({
30 type: "webfetch",
31 sessionID: ctx.sessionID,
32 messageID: ctx.messageID,
33 callID: ctx.callID,
34 title: "Fetch content from: " + params.url,
35 metadata: {
36 url: params.url,
37 format: params.format,
38 timeout: params.timeout,
39 },
40 })
41
42 const timeout = Math.min((params.timeout ?? DEFAULT_TIMEOUT / 1000) * 1000, MAX_TIMEOUT)
43
44 const controller = new AbortController()
45 const timeoutId = setTimeout(() => controller.abort(), timeout)
46
47 // Build Accept header based on requested format with q parameters for fallbacks
48 let acceptHeader = "*/*"
49 switch (params.format) {
50 case "markdown":
51 acceptHeader = "text/markdown;q=1.0, text/x-markdown;q=0.9, text/plain;q=0.8, text/html;q=0.7, */*;q=0.1"
52 break
53 case "text":
54 acceptHeader = "text/plain;q=1.0, text/markdown;q=0.9, text/html;q=0.8, */*;q=0.1"
55 break
56 case "html":
57 acceptHeader = "text/html;q=1.0, application/xhtml+xml;q=0.9, text/plain;q=0.8, text/markdown;q=0.7, */*;q=0.1"
58 break
59 default:
60 acceptHeader =
61 "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8"
62 }
63
64 const response = await fetch(params.url, {
65 signal: AbortSignal.any([controller.signal, ctx.abort]),
66 headers: {
67 "User-Agent":
68 "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
69 Accept: acceptHeader,
70 "Accept-Language": "en-US,en;q=0.9",
71 },
72 })
73
74 clearTimeout(timeoutId)
75
76 if (!response.ok) {
77 throw new Error(`Request failed with status code: ${response.status}`)
78 }

Callers

nothing calls this directly

Calls 6

convertHTMLToMarkdownFunction · 0.85
extractTextFromHTMLFunction · 0.85
arrayBufferMethod · 0.65
fetchFunction · 0.50
getMethod · 0.45
abortMethod · 0.45

Tested by

no test coverage detected