From 41ff845f3157097950ec3f8134907651b30f79b8 Mon Sep 17 00:00:00 2001 From: bipproduction Date: Mon, 27 Oct 2025 03:03:30 +0800 Subject: [PATCH] tambahan --- src/server/routes/mcp_route.ts | 142 ++++++++++++++++++++++++--------- x.sh | 2 +- 2 files changed, 106 insertions(+), 38 deletions(-) diff --git a/src/server/routes/mcp_route.ts b/src/server/routes/mcp_route.ts index 7b53740..1a4eed6 100644 --- a/src/server/routes/mcp_route.ts +++ b/src/server/routes/mcp_route.ts @@ -1,49 +1,117 @@ import { Elysia } from "elysia"; export const MCPRoute = new Elysia() - .get("/mcp/:sessionId", ({ set, params }) => { - set.headers["Content-Type"] = "text/event-stream; charset=utf-8"; - set.headers["Cache-Control"] = "no-cache"; - set.headers["Connection"] = "keep-alive"; + .get("/mcp/:sessionId", ({ params }) => { + const encoder = new TextEncoder(); + let interval: Timer | null = null; - const encoder = new TextEncoder(); + const stream = new ReadableStream({ + start(controller) { + // Kirim event awal + const init = { + jsonrpc: "2.0", + id: null, + result: { + protocol: "2024-11-05", + capabilities: { + "tools/list": true, + "tools/call": true, + }, + status: `MCP session ${params.sessionId} aktif`, + }, + }; - const stream = new ReadableStream({ - start(controller) { - const send = (obj: any) => { - controller.enqueue(encoder.encode(`data: ${JSON.stringify(obj)}\n\n`)); - }; + // Kirim data dan flush + controller.enqueue( + encoder.encode(`data: ${JSON.stringify(init)}\n\n`) + ); - // Kirim pesan pembuka - send({ - jsonrpc: "2.0", - id: null, - result: { - protocol: "2024-11-05", - capabilities: { - "tools/list": true, - "tools/call": true, + // SSE heartbeat + 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() { + if (interval) clearInterval(interval); + console.log(`[SSE] koneksi session ${params.sessionId} ditutup`); }, - status: `MCP Stream aktif untuk session ${params.sessionId}`, - }, }); - // Keep connection alive - const interval = setInterval(() => { - send({ ping: Date.now() }); - }, 15000); + 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; - // Simpan handle agar bisa dibersihkan nanti - (controller as any)._interval = interval; - }, + 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#", + }, + }, + ], + }, + }; + } - cancel() { - // Ketika stream dibatalkan oleh client - console.log("🔌 Stream closed by client"); - // Bersihkan interval - clearInterval((this as any)._interval); - }, - }); + 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 stream; - }); + return { + jsonrpc: "2.0", + id, + error: { + code: -32601, + message: `Method ${method} tidak dikenali`, + }, + }; + }); \ No newline at end of file diff --git a/x.sh b/x.sh index f0725e7..95d4490 100644 --- a/x.sh +++ b/x.sh @@ -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"