This commit is contained in:
bipproduction
2025-10-26 21:23:40 +08:00
parent cd7784a375
commit a31f3136f7
2 changed files with 26 additions and 19 deletions

View File

@@ -1,19 +1,20 @@
import { Elysia, t } from "elysia";
export const MCPRoute = new Elysia({
prefix: "/mcp-server", // ✅ Sesuaikan dengan endpoint n8n
prefix: "/mcp-server",
tags: ["mcp-server"],
})
.post("/mcp", ({ body, set }) => {
const { id, method, params } = body as any;
set.headers["Content-Type"] = "application/json";
set.headers["Content-Type"] = "application/json; charset=utf-8";
set.headers["Transfer-Encoding"] = "chunked";
set.headers["Connection"] = "keep-alive";
// ✅ Streaming Response
const stream = new ReadableStream({
async start(controller) {
// tools.list → kirim daftar tools
// tools/list
if (method === "tools/list") {
controller.enqueue(
JSON.stringify({
@@ -31,40 +32,46 @@ export const MCPRoute = new Elysia({
],
}) + "\n"
);
// ❌ Jangan tutup langsung, beri delay agar n8n sempat membaca
await Bun.sleep(200);
controller.close();
return;
}
// tools.sayHello → streaming bertahap
else if (method === "tools/sayHello") {
// tools/sayHello → streaming progress
if (method === "tools/sayHello") {
controller.enqueue(
JSON.stringify({
jsonrpc: "2.0",
id,
result: { message: "Processing..." },
result: { status: "Processing..." },
}) + "\n"
);
await Bun.sleep(500);
controller.enqueue(
JSON.stringify({
jsonrpc: "2.0",
id,
result: { message: `Hello ${params?.name || ""}` },
result: { message: `Hello ${params?.name || "User"}` },
}) + "\n"
);
await Bun.sleep(200);
controller.close();
return;
}
// Method tidak ditemukan
else {
controller.enqueue(
JSON.stringify({
jsonrpc: "2.0",
id,
error: { code: -32601, message: `Method ${method} not found` },
}) + "\n"
);
controller.close();
}
// Method tidak dikenal
controller.enqueue(
JSON.stringify({
jsonrpc: "2.0",
id,
error: { code: -32601, message: `Method ${method} not found` },
}) + "\n"
);
await Bun.sleep(200);
controller.close();
},
});