#!/usr/bin/env node
import { StreamableHTTPServerTransport as i } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
import p from "dotenv";
import c from "express";
import { g as l } from "./mcp-server-zOuk6rId.js";
import { r as d } from "./require-valid-todoist-token-BNjPQnWx.js";
p.config({ quiet: !0 });
const s = Number.parseInt(process.env.PORT || "3000", 10);
function m() {
  const a = 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 = c();
  r.use(c.json()), r.get("/health", (o, e) => {
    e.json({ status: "ok" });
  }), r.post(
    "/mcp",
    d({ type: "static", apiKey: n, baseUrl: a }),
    async (o, e) => {
      try {
        const t = new i({
          sessionIdGenerator: void 0
        });
        await l({ todoistApiKey: n, baseUrl: a }).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`);
  });
}
m();
