UNPKG

mcp-use

Version:

Opinionated MCP Framework for TypeScript (@modelcontextprotocol/sdk compatible) - Build MCP Agents, Clients and Servers with support for ChatGPT Apps, Code Mode, OAuth, Notifications, Sampling, Observability and more.

106 lines (102 loc) 3.2 kB
import { __name } from "./chunk-3GQAWCBQ.js"; // src/utils/favicon-detector.ts function isLocalServer(domain) { return domain === "localhost" || domain === "127.0.0.1" || domain.startsWith("127.") || domain.startsWith("192.168.") || domain.startsWith("10.") || domain.startsWith("172."); } __name(isLocalServer, "isLocalServer"); function getBaseDomain(hostname) { const parts = hostname.split("."); if (parts.length <= 2) { return hostname; } return parts.slice(parts.length - 2).join("."); } __name(getBaseDomain, "getBaseDomain"); function blobToBase64(blob) { return new Promise((resolve, reject) => { const reader = new FileReader(); reader.onloadend = () => { resolve(reader.result); }; reader.onerror = reject; reader.readAsDataURL(blob); }); } __name(blobToBase64, "blobToBase64"); async function detectFavicon(serverUrl) { try { let domain; if (serverUrl.startsWith("http://") || serverUrl.startsWith("https://")) { domain = new URL(serverUrl).hostname; } else if (serverUrl.includes("://")) { domain = serverUrl.split("://")[1].split("/")[0]; } else { domain = serverUrl.split("/")[0]; } if (isLocalServer(domain)) { return null; } const baseDomain = getBaseDomain(domain); const domainsToTry = domain !== baseDomain ? [domain, baseDomain] : [domain]; for (const currentDomain of domainsToTry) { try { const faviconApiUrl = `https://favicon.tools.mcp-use.com/${currentDomain}`; const controller = new AbortController(); const timeoutId = setTimeout(() => controller.abort(), 2e3); try { const response = await fetch(faviconApiUrl, { signal: controller.signal }); clearTimeout(timeoutId); if (!response.ok) { continue; } const blob = await response.blob(); const base64Image = await blobToBase64(blob); return base64Image; } catch (err) { clearTimeout(timeoutId); continue; } } catch (error) { continue; } } return null; } catch (error) { console.warn("[favicon-detector] Error detecting favicon:", error); return null; } } __name(detectFavicon, "detectFavicon"); // src/utils/proxy-config.ts function applyProxyConfig(originalUrl, proxyConfig) { const proxyHeaders = proxyConfig?.headers ?? proxyConfig?.customHeaders ?? {}; if (proxyConfig?.customHeaders && !proxyConfig?.headers) { console.warn( '[applyProxyConfig] The "customHeaders" option in proxyConfig is deprecated. Use "headers" instead.' ); } if (!proxyConfig?.proxyAddress) { return { url: originalUrl, headers: proxyHeaders }; } const proxyUrl = new URL(proxyConfig.proxyAddress); const targetUrl = new URL(originalUrl); const finalUrl = `${proxyUrl.origin}${proxyUrl.pathname}${targetUrl.pathname}${targetUrl.search}`; const headers = { "X-Target-URL": originalUrl, ...proxyHeaders }; return { url: finalUrl, headers }; } __name(applyProxyConfig, "applyProxyConfig"); export { isLocalServer, detectFavicon, applyProxyConfig };