/** * toolHarness.ts — Transport adapter for standalone custom tools. * * This is the ONLY file that knows about the stdout protocol. It is invoked * by ProxyTool as: * * bun run toolHarness.ts * * It dynamically imports the tool file, calls its exported `execute` function, * and writes the result using the protocol. The tool itself has zero knowledge * of how it is being called — it simply exports a function. * * A tool file must export: * - TOOL_METADATA: { name, description, inputs, outputType } * - execute(args: Record): Promise * * Any console.log calls made inside execute() will flow through stdout * naturally and be captured as streaming output by the ProxyTool on the * other end — no special handling needed by the tool author. */ async function main(): Promise { const toolPath = process.argv[2]; const argsJson = process.argv[3] ?? '{}'; if (!toolPath) { process.stderr.write('toolHarness: no tool path provided (argv[2])\n'); process.exit(1); } // Deserialize arguments let args: Record; try { args = JSON.parse(argsJson); } catch (err) { process.stderr.write(`toolHarness: invalid JSON arguments: ${(err as Error).message}\n`); process.exit(1); } // Dynamically import the tool module — Bun handles .ts natively let toolModule: Record; try { toolModule = await import(toolPath) as Record; } catch (err) { process.stderr.write(`toolHarness: failed to import tool at "${toolPath}": ${(err as Error).message}\n`); process.exit(1); } // Extract and validate the execute export const execute = toolModule.execute; if (typeof execute !== 'function') { const exported = Object.keys(toolModule).filter(k => k !== '__esModule').join(', '); process.stderr.write( `toolHarness: tool at "${toolPath}" does not export an 'execute' function.\n` + ` Exported keys: [${exported}]\n` + ` A custom tool must: export async function execute(args) { ... }\n` ); process.exit(1); } // Invoke the tool's execute function const result = await (execute as (args: Record) => Promise)(args); // Write the return value on the protocol line — this is the only place // the protocol is spoken. The tool never sees it. process.stdout.write(`[TOOL_RESULT]${JSON.stringify(result)}\n`); } main().catch((err: Error) => { // Protocol error line — tool threw or something went wrong process.stdout.write(`[TOOL_ERROR]${err.message}\n`); process.exit(1); });