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.

125 lines (121 loc) 4.22 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/utils/index.ts var utils_exports = {}; __export(utils_exports, { applyProxyConfig: () => applyProxyConfig, detectFavicon: () => detectFavicon, isLocalServer: () => isLocalServer }); module.exports = __toCommonJS(utils_exports); // 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");