"use strict";
// MCP stdio transport — line-delimited JSON-RPC over stdin/stdout.
//
// Implements the minimum needed to round-trip MCP `initialize`,
// `tools/list`, and `tools/call` against `HarnessMcpServer` from
// `mcpServer.ts`. Reads requests one line at a time, dispatches, writes
// responses as one JSON object per line.
//
// This is wire-compatible with simple MCP clients that speak line-delimited
// JSON-RPC (the canonical MCP shape uses Content-Length framing; for that,
// install `@modelcontextprotocol/sdk` and swap this transport — the
// HarnessMcpServer class stays unchanged).
//
// To run:
//   node scripts/jarvis-mcp-serve.js
//   echo '{"id":1,"method":"tools/list"}' | node scripts/jarvis-mcp-serve.js
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.startMcpStdioServer = startMcpStdioServer;
exports.singleRequest = singleRequest;
const readline = __importStar(require("readline"));
const mcpServer_1 = require("./mcpServer");
function startMcpStdioServer(options) {
    const input = options.input ?? process.stdin;
    const output = options.output ?? process.stdout;
    const server = options.server;
    const rl = readline.createInterface({ input, crlfDelay: Infinity });
    rl.on('line', async (line) => {
        const trimmed = line.trim();
        if (!trimmed)
            return;
        let request;
        try {
            request = JSON.parse(trimmed);
        }
        catch (err) {
            if (options.onParseError)
                options.onParseError(trimmed, err);
            return;
        }
        const transport = new mcpServer_1.StubTransport();
        await server.handle(request, transport);
        for (const response of transport.sent) {
            output.write(JSON.stringify(response) + '\n');
        }
    });
    return {
        stop: () => rl.close(),
    };
}
/** Helper for tests: run a single request through the server and return the response. */
async function singleRequest(server, request) {
    const transport = new mcpServer_1.StubTransport();
    await server.handle(request, transport);
    return transport.sent[0];
}
//# sourceMappingURL=mcpStdio.js.map