38 lines
1.1 KiB
JavaScript
38 lines
1.1 KiB
JavaScript
import { registerSession, unregisterSession, findPlugin } from "@/lib/mcp/stdioSseBridge";
|
|
|
|
export const runtime = "nodejs";
|
|
export const dynamic = "force-dynamic";
|
|
|
|
export async function GET(request, { params }) {
|
|
// Cowork disabled: MCP stdio bridge spawns arbitrary processes (RCE risk).
|
|
return new Response("Cowork is disabled", { status: 403 });
|
|
const { plugin } = await params;
|
|
if (!findPlugin(plugin)) {
|
|
return new Response(`Unknown plugin: ${plugin}`, { status: 404 });
|
|
}
|
|
|
|
const encoder = new TextEncoder();
|
|
let sid;
|
|
|
|
const stream = new ReadableStream({
|
|
start(controller) {
|
|
const send = (chunk) => controller.enqueue(encoder.encode(chunk));
|
|
sid = registerSession(plugin, send);
|
|
// MCP SSE handshake: tell client where to POST messages.
|
|
send(`event: endpoint\ndata: /api/mcp/${plugin}/message?sessionId=${sid}\n\n`);
|
|
},
|
|
cancel() {
|
|
if (sid) unregisterSession(plugin, sid);
|
|
},
|
|
});
|
|
|
|
return new Response(stream, {
|
|
headers: {
|
|
"Content-Type": "text/event-stream",
|
|
"Cache-Control": "no-cache, no-transform",
|
|
Connection: "keep-alive",
|
|
"X-Accel-Buffering": "no",
|
|
},
|
|
});
|
|
}
|