import { createFileRoute } from "@tanstack/react-router";
import { handleMcpRequest } from "../../mcp/server";

/**
 * MCP HTTP Streamable transport endpoint. Mounted at `POST /api/mcp` on the
 * existing TanStack Start HTTP server — see
 * `openspec/changes/add-mcp-server/design.md` D1.
 *
 * All method/header routing is handled inside the SDK's
 * `WebStandardStreamableHTTPServerTransport` (it dispatches GET for SSE
 * stream, POST for client→server requests, DELETE for session close), so
 * the route simply forwards the request.
 */
export const Route = createFileRoute("/api/mcp")({
  server: {
    handlers: {
      // The SDK may issue either POST, GET, or DELETE. TanStack Start only
      // requires us to declare the methods we accept; routing by method is
      // handled inside the transport.
      POST: ({ request }: { request: Request }) => handleMcpRequest(request),
      GET: ({ request }: { request: Request }) => handleMcpRequest(request),
      DELETE: ({ request }: { request: Request }) => handleMcpRequest(request),
    },
  },
});
