/**
 * olam mcp serve — Run the olam MCP server (stdio transport).
 *
 * Claude Code wires this via `claude mcp add olam --scope user -- npx -y @pleri/olam-cli mcp serve`.
 *
 * Consumes the publish-time bundle at `packages/cli/dist/mcp-server.js` (Decision 27
 * in `docs/plans/olam-continuous-distribution/`). The bundle's top-level
 * `main().catch(...)` invocation auto-runs the stdio loop on import.
 *
 * Path resolution accounts for both runtime layouts:
 *   1. Dev / tsc-only: this file is at `dist/commands/mcp/serve.js`, so the
 *      bundle sibling lives two levels up: `dist/mcp-server.js`.
 *   2. Published / `bundle-cli.mjs` output: the CLI is bundled into a single
 *      `dist/index.js`, so this code is inlined and `import.meta.url` points
 *      to `dist/index.js`. The bundle is then a direct sibling.
 * We probe both candidates so the same binary works in dev AND from a
 * published tarball.
 */
import { existsSync } from 'node:fs';
import { dirname, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
import { printError } from '../../output.js';
const here = dirname(fileURLToPath(import.meta.url));
export const BUNDLE_PATH_CANDIDATES = [
    // bundled (`dist/index.js` after bundle-cli.mjs) — sibling
    resolve(here, 'mcp-server.js'),
    // dev / tsc-only (`dist/commands/mcp/serve.js`) — up two levels
    resolve(here, '..', '..', 'mcp-server.js'),
];
export function resolveBundlePath(candidates = BUNDLE_PATH_CANDIDATES, exists = existsSync) {
    return candidates.find(exists) ?? null;
}
const MISSING_BUNDLE_REMEDY = 'olam mcp server bundle missing. Searched: ' +
    BUNDLE_PATH_CANDIDATES.join(', ') +
    '. For local dev, run: node packages/cli/scripts/bundle-mcp-server.mjs. ' +
    'A fresh `npm install -g @pleri/olam-cli@latest` should always include the bundle ' +
    '(see prepublishOnly in packages/cli/package.json); file an issue if it does not.';
export function registerMcpServe(cmd) {
    cmd
        .command('serve')
        .description('Run the olam MCP server (stdio transport). Claude Code wires this via `claude mcp add olam --scope user -- npx -y @pleri/olam-cli mcp serve`.')
        .action(async () => {
        const bundlePath = resolveBundlePath();
        if (bundlePath === null) {
            printError(MISSING_BUNDLE_REMEDY);
            process.exitCode = 1;
            return;
        }
        // The bundle's top-level `main().catch(...)` auto-runs the stdio loop on import.
        // Awaiting the dynamic import resolves once the module is loaded; the StdioServerTransport
        // keeps the event loop alive until the client closes stdin.
        await import(bundlePath);
    });
}
//# sourceMappingURL=serve.js.map