This commit is contained in:
bipproduction
2025-10-27 03:03:30 +08:00
parent f9e74b59ca
commit 41ff845f31
2 changed files with 106 additions and 38 deletions

View File

@@ -1,21 +1,14 @@
import { Elysia } from "elysia"; import { Elysia } from "elysia";
export const MCPRoute = new Elysia() export const MCPRoute = new Elysia()
.get("/mcp/:sessionId", ({ set, params }) => { .get("/mcp/:sessionId", ({ params }) => {
set.headers["Content-Type"] = "text/event-stream; charset=utf-8";
set.headers["Cache-Control"] = "no-cache";
set.headers["Connection"] = "keep-alive";
const encoder = new TextEncoder(); const encoder = new TextEncoder();
let interval: Timer | null = null;
const stream = new ReadableStream({ const stream = new ReadableStream({
start(controller) { start(controller) {
const send = (obj: any) => { // Kirim event awal
controller.enqueue(encoder.encode(`data: ${JSON.stringify(obj)}\n\n`)); const init = {
};
// Kirim pesan pembuka
send({
jsonrpc: "2.0", jsonrpc: "2.0",
id: null, id: null,
result: { result: {
@@ -24,26 +17,101 @@ export const MCPRoute = new Elysia()
"tools/list": true, "tools/list": true,
"tools/call": true, "tools/call": true,
}, },
status: `MCP Stream aktif untuk session ${params.sessionId}`, status: `MCP session ${params.sessionId} aktif`,
}, },
}); };
// Keep connection alive // Kirim data dan flush
const interval = setInterval(() => { controller.enqueue(
send({ ping: Date.now() }); encoder.encode(`data: ${JSON.stringify(init)}\n\n`)
}, 15000); );
// Simpan handle agar bisa dibersihkan nanti // SSE heartbeat
(controller as any)._interval = interval; interval = setInterval(() => {
try {
controller.enqueue(encoder.encode(`: ping ${Date.now()}\n\n`));
} catch (e) {
if (interval) clearInterval(interval);
}
}, 10000);
console.log(`[SSE] koneksi session ${params.sessionId} dibuka`);
}, },
cancel() { cancel() {
// Ketika stream dibatalkan oleh client if (interval) clearInterval(interval);
console.log("🔌 Stream closed by client"); console.log(`[SSE] koneksi session ${params.sessionId} ditutup`);
// Bersihkan interval
clearInterval((this as any)._interval);
}, },
}); });
return stream; return new Response(stream, {
status: 200,
headers: {
"Content-Type": "text/event-stream; charset=utf-8",
"Cache-Control": "no-cache, no-transform",
"Connection": "keep-alive",
"X-Accel-Buffering": "no", // Penting untuk nginx/cloudflare
"Access-Control-Allow-Origin": "*",
},
});
})
.post("/mcp", async ({ body, set }) => {
set.headers["Content-Type"] = "application/json; charset=utf-8";
const { id, method, params } = body as any;
if (method === "tools/list") {
return {
jsonrpc: "2.0",
id,
result: {
tools: [
{
name: "pengajuan-pembuatan-ktp",
description:
"untuk melakukan pengajuan pembuatan ktp\nmembutuhkan :\n- jenis\n- name\n- deskripsi",
inputSchema: {
type: "object",
properties: {
JSON: { type: "object" },
},
required: ["JSON"],
additionalProperties: true,
$schema: "http://json-schema.org/draft-07/schema#",
},
},
{
name: "pengetahuan_malik_kurosaki",
description: "penjelasan tentang malik kurosaki",
inputSchema: {
type: "object",
properties: {
input: { type: "string" },
},
additionalProperties: true,
$schema: "http://json-schema.org/draft-07/schema#",
},
},
],
},
};
}
if (method === "tools/call") {
const { tool, arguments: args } = params;
if (tool === "pengajuan-pembuatan-ktp") {
return {
jsonrpc: "2.0",
id,
result: { message: "Berhasil menerima pengajuan KTP", data: args },
};
}
}
return {
jsonrpc: "2.0",
id,
error: {
code: -32601,
message: `Method ${method} tidak dikenali`,
},
};
}); });

2
x.sh
View File

@@ -1,2 +1,2 @@
curl -N -v -X GET "https://cld-dkr-prod-jenna-mcp.wibudev.com/mcp/12345" curl -N -v -X GET "https://n8n.wibudev.com/mcp/fd665648-b38d-4bee-9ab8-11ca0cd83d0d"