UNPKG

@mseep/hyperbrowser-mcp

Version:

Hyperbrowser Model Context Protocol Server

56 lines (55 loc) 2.13 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.downloadImageAsBase64 = exports.logWithTimestamp = exports.getClient = void 0; const dotenv_1 = require("dotenv"); const sdk_1 = require("@hyperbrowser/sdk"); (0, dotenv_1.config)(); const getClient = async () => { const apiKey = process.env.HB_API_KEY || process.env.HYPERBROWSER_API_KEY; if (!apiKey) { throw new Error("No API key provided or found in environment variables"); } return new sdk_1.Hyperbrowser({ apiKey }); }; exports.getClient = getClient; const logWithTimestamp = ({ level = "info", name = "hyperbrowser", data, }) => { const timestamp = new Date().toISOString(); const consoleData = [`${timestamp} [${name}] [${level}]`]; if (Array.isArray(data)) { consoleData.push(...data); } else { consoleData.push(data); } console.error(...consoleData); }; exports.logWithTimestamp = logWithTimestamp; /** * Downloads an image from a URL and converts it to base64 * @param imageUrl The URL of the image to download * @returns Promise resolving to the base64-encoded image data */ const downloadImageAsBase64 = async (imageUrl) => { try { // Fetch the image const response = await fetch(imageUrl); if (!response.ok) { throw new Error(`Failed to download image: ${response.status} ${response.statusText}`); } // Get the image data as an ArrayBuffer const imageBuffer = await response.arrayBuffer(); const buffer = Buffer.from(imageBuffer); const base64Data = buffer.toString("base64"); const contentType = response.headers.get("content-type") || "image/jpeg"; // Return the complete base64 data URI return { data: base64Data, mimeType: contentType }; } catch (error) { (0, exports.logWithTimestamp)({ level: "error", data: `Error downloading image from ${imageUrl}: ${error instanceof Error ? error.message : String(error)}`, }); return null; } }; exports.downloadImageAsBase64 = downloadImageAsBase64;