UNPKG

@aashari/boilerplate-mcp-server

Version:

TypeScript MCP server boilerplate with STDIO and HTTP transport support, CLI tools, and extensible architecture

102 lines (101 loc) 3.47 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.toToonOrJson = toToonOrJson; exports.toToonOrJsonSync = toToonOrJsonSync; exports.preloadToonEncoder = preloadToonEncoder; const logger_util_js_1 = require("./logger.util.js"); const logger = logger_util_js_1.Logger.forContext('utils/toon.util.ts'); /** * Cached TOON encoder to avoid repeated dynamic imports */ let toonEncode = null; /** * Dynamically loads the TOON encoder module. * Uses dynamic import because @toon-format/toon is an ESM-only package. * * @returns Promise resolving to the encode function or null if loading fails */ async function loadToonEncoder() { const methodLogger = logger.forMethod('loadToonEncoder'); // Return cached encoder if available if (toonEncode) { return toonEncode; } try { methodLogger.debug('Loading TOON encoder module...'); // Dynamic import for ESM module in CommonJS project const toon = await import('@toon-format/toon'); toonEncode = toon.encode; methodLogger.debug('TOON encoder loaded successfully'); return toonEncode; } catch (error) { methodLogger.error('Failed to load TOON encoder', error); return null; } } /** * Convert data to TOON format with JSON fallback. * * TOON (Token-Oriented Object Notation) is 30-60% more token-efficient than JSON * for tabular data, making it ideal for LLM responses. * * @param data - The data to convert * @param jsonFallback - JSON string to return if TOON encoding fails * @returns TOON formatted string, or JSON fallback on failure * * @example * const json = JSON.stringify(data, null, 2); * const output = await toToonOrJson(data, json); */ async function toToonOrJson(data, jsonFallback) { const methodLogger = logger.forMethod('toToonOrJson'); try { const encode = await loadToonEncoder(); if (!encode) { methodLogger.debug('TOON encoder not available, using JSON fallback'); return jsonFallback; } const result = encode(data, { indent: 2 }); methodLogger.debug('Successfully converted to TOON format'); return result; } catch (error) { methodLogger.error('TOON encoding failed, using JSON fallback', error); return jsonFallback; } } /** * Synchronous TOON conversion with JSON fallback. * * Uses cached encoder if available, otherwise returns JSON fallback. * Prefer toToonOrJson for first-time conversion. * * @param data - The data to convert * @param jsonFallback - The JSON string to return if TOON is unavailable * @returns TOON formatted string, or JSON fallback */ function toToonOrJsonSync(data, jsonFallback) { const methodLogger = logger.forMethod('toToonOrJsonSync'); if (!toonEncode) { methodLogger.debug('TOON encoder not loaded, using JSON fallback'); return jsonFallback; } try { const toonResult = toonEncode(data, { indent: 2 }); methodLogger.debug('Successfully converted to TOON format'); return toonResult; } catch (error) { methodLogger.error('TOON conversion failed, using JSON fallback', error); return jsonFallback; } } /** * Pre-load the TOON encoder for synchronous usage later. * Call this during server initialization. */ async function preloadToonEncoder() { const encode = await loadToonEncoder(); return encode !== null; }