tambahan
This commit is contained in:
@@ -1,23 +1,15 @@
|
|||||||
import { Elysia } from "elysia";
|
import { Elysia } from "elysia";
|
||||||
|
|
||||||
export const MCPRoute = new Elysia()
|
export const MCPRoute = new Elysia()
|
||||||
.get("/mcp/:sessionId", async ({ params, set }) => {
|
.get("/mcp/:sessionId", ({ params }) => {
|
||||||
// Atur header SSE
|
|
||||||
set.headers = {
|
|
||||||
"Content-Type": "text/event-stream; charset=utf-8",
|
|
||||||
"Cache-Control": "no-cache",
|
|
||||||
Connection: "keep-alive",
|
|
||||||
"Access-Control-Allow-Origin": "*",
|
|
||||||
};
|
|
||||||
|
|
||||||
const encoder = new TextEncoder();
|
const encoder = new TextEncoder();
|
||||||
|
|
||||||
let interval: NodeJS.Timeout;
|
let interval: Timer | null = null;
|
||||||
|
|
||||||
const stream = new ReadableStream({
|
const stream = new ReadableStream({
|
||||||
start(controller) {
|
start(controller) {
|
||||||
// kirim handshake awal
|
// kirim event awal (flush langsung)
|
||||||
const initial = {
|
const init = {
|
||||||
jsonrpc: "2.0",
|
jsonrpc: "2.0",
|
||||||
id: null,
|
id: null,
|
||||||
result: {
|
result: {
|
||||||
@@ -30,24 +22,30 @@ export const MCPRoute = new Elysia()
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
controller.enqueue(encoder.encode(`data: ${JSON.stringify(initial)}\n\n`));
|
controller.enqueue(
|
||||||
|
encoder.encode(`data: ${JSON.stringify(init)}\n\n`)
|
||||||
|
);
|
||||||
|
|
||||||
// jaga stream tetap hidup
|
// SSE heartbeat
|
||||||
interval = setInterval(() => {
|
interval = setInterval(() => {
|
||||||
controller.enqueue(encoder.encode(`: ping ${Date.now()}\n\n`));
|
controller.enqueue(encoder.encode(`: ping ${Date.now()}\n\n`));
|
||||||
}, 10000);
|
}, 10000);
|
||||||
|
|
||||||
// kirim ready event
|
|
||||||
controller.enqueue(encoder.encode(`event: ready\ndata: "stream ready"\n\n`));
|
|
||||||
},
|
},
|
||||||
|
|
||||||
cancel() {
|
cancel() {
|
||||||
clearInterval(interval);
|
if (interval) clearInterval(interval);
|
||||||
console.log(`SSE connection ${params.sessionId} closed by client`);
|
console.log(`[SSE] koneksi session ${params.sessionId} ditutup`);
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
return stream;
|
// gunakan Response manual agar Bun flush data pertama
|
||||||
|
return new Response(stream, {
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "text/event-stream; charset=utf-8",
|
||||||
|
"Cache-Control": "no-cache, no-transform",
|
||||||
|
Connection: "keep-alive",
|
||||||
|
"Access-Control-Allow-Origin": "*",
|
||||||
|
},
|
||||||
|
});
|
||||||
})
|
})
|
||||||
|
|
||||||
.post("/mcp", async ({ body, set }) => {
|
.post("/mcp", async ({ body, set }) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user