#!/usr/bin/env node
import "dotenv/config";
import process from "node:process";
import { pathToFileURL } from "node:url";
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { getCliVersion } from "../version.js";
import { registerChatGptImageTool } from "./tools/chatgptImage.js";
import { registerConsultTool } from "./tools/consult.js";
import { registerProjectSourcesTool } from "./tools/projectSources.js";
import { registerSessionsTool } from "./tools/sessions.js";
import { registerSessionResources } from "./tools/sessionResources.js";
export async function startMcpServer() {
    const server = new McpServer({
        name: "oracle-mcp",
        version: getCliVersion(),
    }, {
        capabilities: {
            logging: {},
        },
    });
    registerConsultTool(server);
    registerChatGptImageTool(server);
    registerProjectSourcesTool(server);
    registerSessionsTool(server);
    registerSessionResources(server);
    const transport = new StdioServerTransport();
    transport.onerror = (error) => {
        console.error("MCP transport error:", error);
    };
    const closed = new Promise((resolve) => {
        transport.onclose = () => {
            resolve();
        };
    });
    // Keep the process alive until the client closes the transport.
    await server.connect(transport);
    await closed;
}
export function shouldStartMcpServerFromModule(moduleUrl = import.meta.url, argv1 = process.argv[1]) {
    return argv1 ? moduleUrl === pathToFileURL(argv1).href : false;
}
if (shouldStartMcpServerFromModule()) {
    startMcpServer().catch((error) => {
        console.error("Failed to start oracle-mcp:", error);
        process.exitCode = 1;
    });
}
