Never stop coding. Free MIT AI gateway: one endpoint, 375 providers (150+ free), 1,400+ models Kimi, Claude, GPT, Gemini, GLM, DeepSeek, MiniMax. Works with Claude Code, Codex, Cursor, OpenCode, Cline & Copilot. Quota-aware auto-fallback, RTK+Caveman compression saves 15-95% tokens, MCP/A2A, Desktop/PWA. Built by 900+ contributors
TypeScript a2aai-agentsai-gatewayanthropicclaudeclaude-codeclinecodexcopilotcursordeepseekfree-aigeminikimillm-gatewaymcpopenaiopenai-proxyqwentoken-saver
/**
* MCP Streamable HTTP Transport — /api/mcp/stream
*
* Endpoints:
* POST — send JSON-RPC messages to the MCP server
* GET — open SSE stream for server-initiated messages
* DELETE — end session
*/
import { NextRequest, NextResponse } from "next/server";
import { getCachedSettings } from "@/lib/db/settings";
import { handleMcpStreamableHTTP } from "../../../../../open-sse/mcp-server/httpTransport";
import { requireManagementAuth } from "@/lib/api/requireManagementAuth";
async function guardEnabled(): Promise<NextResponse | null> {
const settings = await getCachedSettings();
if (!settings.mcpEnabled) {
return NextResponse.json(
{ error: "MCP server is disabled. Enable it from the Endpoints page." },
{ status: 503 }
);
}
const transport = (settings.mcpTransport as string) || "stdio";
if (transport !== "streamable-http") {
return NextResponse.json(
{
error: `MCP transport is set to "${transport}", not "streamable-http". Change it from Settings.`,
},
{ status: 400 }
);
}
return null;
}
export async function POST(request: NextRequest) {
const authError = await requireManagementAuth(request, { acceptMcpConnectScope: true });
if (authError) return authError;
const blocked = await guardEnabled();
if (blocked) return blocked;
return handleMcpStreamableHTTP(request);
}
export async function GET(request: NextRequest) {
const authError = await requireManagementAuth(request, { acceptMcpConnectScope: true });
if (authError) return authError;
const blocked = await guardEnabled();
if (blocked) return blocked;
return handleMcpStreamableHTTP(request);
}
export async function DELETE(request: NextRequest) {
const authError = await requireManagementAuth(request, { acceptMcpConnectScope: true });
if (authError) return authError;
const blocked = await guardEnabled();
if (blocked) return blocked;
return handleMcpStreamableHTTP(request);
}