#!/usr/bin/env node
import { StreamableHTTPServerTransport as i } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
import l from "dotenv";
import a from "express";
import { g as p } from "./mcp-server-B2UZESHN.js";
l.config({ quiet: !0 });
const s = Number.parseInt(process.env.PORT || "3000", 10);
function d() {
  const c = process.env.TODOIST_BASE_URL, n = process.env.TODOIST_API_KEY;
  n || (console.error("Error: TODOIST_API_KEY environment variable is required"), process.exit(1));
  const r = a();
  r.use(a.json()), r.get("/health", (o, e) => {
    e.json({ status: "ok" });
  }), r.post("/mcp", async (o, e) => {
    try {
      const t = new i({
        sessionIdGenerator: void 0
      });
      await p({ todoistApiKey: n, baseUrl: c }).connect(t), await t.handleRequest(o, e, o.body);
    } catch (t) {
      console.error("[Error] Request handling failed:", t), e.status(500).json({
        jsonrpc: "2.0",
        error: {
          code: -32603,
          message: "Internal server error"
        },
        id: null
      });
    }
  }), r.get("/mcp", (o, e) => {
    e.status(405).set("Allow", "POST").send("Method Not Allowed");
  }), r.listen(s, () => {
    console.error(`Todoist MCP HTTP Server started on port ${s}`), console.error(`MCP endpoint: http://localhost:${s}/mcp`), console.error(`Health check: http://localhost:${s}/health`);
  });
}
d();
