/** * A tool service for the acceptance scripts: the code behind an agent's tools, served as a * remote MCP server the Runtime calls itself (it runs no code of the developer's during a * session). It is a stateless Streamable HTTP MCP server at `POST /mcp` answering JSON: each * tool is answered by its handler, and every call is counted. On a local Tenant the Runtime * runs in Docker and maps `localhost` to this machine, so the server listens on every interface. */ import { createServer } from "node:http"; /** * @param {Record unknown }>} tools one per tool name * @param {{ port?: number, host?: string }} [options] `port` 0 (default) picks a free one; * `host` is the name the Runtime reaches this machine by (default `localhost`) */ export async function startToolService(tools, options = {}) { const calls = []; const server = createServer(async (request, response) => { const chunks = []; for await (const chunk of request) chunks.push(chunk); const path = new URL(request.url ?? "/", "http://x").pathname; if (path !== "/mcp") return void response.writeHead(404).end(); // Stateless: no server-sent stream to open and no session to end. if (request.method !== "POST") return void response.writeHead(405, { allow: "POST" }).end(); let message; try { message = JSON.parse(Buffer.concat(chunks).toString()); } catch { return void response.writeHead(400).end(); } // A notification (`notifications/initialized`) has no id and gets no answer. if (message.id === undefined || message.id === null) return void response.writeHead(202).end(); const reply = (body) => response.writeHead(200, { "content-type": "application/json" }).end(JSON.stringify({ jsonrpc: "2.0", id: message.id, ...body })); if (message.method === "initialize") return reply({ result: { protocolVersion: message.params?.protocolVersion ?? "2025-06-18", capabilities: { tools: {} }, serverInfo: { name: "acceptance-tools", version: "1.0.0" }, }, }); if (message.method === "ping") return reply({ result: {} }); if (message.method === "tools/list") return reply({ result: { tools: Object.entries(tools).map(([name, tool]) => ({ name, inputSchema: tool.inputSchema })) } }); if (message.method === "tools/call") { const name = message.params?.name; const tool = tools[name]; if (!tool) return reply({ error: { code: -32602, message: `No tool ${name}` } }); const input = message.params?.arguments ?? {}; calls.push({ name, input }); try { const output = await tool.run(input); return reply({ result: { content: [{ type: "text", text: JSON.stringify(output ?? null) }] } }); } catch (error) { return reply({ result: { isError: true, content: [{ type: "text", text: String(error) }] } }); } } return reply({ error: { code: -32601, message: `No method ${message.method}` } }); }); await new Promise((resolve, reject) => { server.once("error", reject); server.listen(options.port ?? 0, resolve); }); const base = `http://${options.host ?? "localhost"}:${server.address().port}`; return { base, /** Every call, in order. */ calls, /** * The MCP server an agent declares for this service (`.mcp({ name: service.server() })`). * @param {string} [url] where the Runtime reaches it, when not at this service's own URL (a tunnel) */ server: (url = `${base}/mcp`) => ({ type: "streamable-http", url }), async close() { server.closeAllConnections(); await new Promise((resolve) => server.close(resolve)); }, }; }