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

2
x.sh
View File

@@ -1,5 +1,5 @@
TOKEN=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJob3N0Iiwic3ViIjoiY21neXI4MjR3MDAwMHBkemhwdjIxZGFzZCIsInBheWxvYWQiOiJ7XCJuYW1lXCI6XCJwZXJjb2JhYW5cIixcImRlc2NyaXB0aW9uXCI6XCJ1bnR1ayBwZXJjb2JhYW5cIixcImV4cGlyZWRBdFwiOlwiMjAzMS0xMC0yMFwifSIsImV4cCI6MTk1MDIyMDgwMCwiaWF0IjoxNzYwOTQyNTcwfQ.X4Y4MJ4aIohT65oJjHCaf2d6e8afnroXu3Hz-jH0WGM TOKEN=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJob3N0Iiwic3ViIjoiY21neXI4MjR3MDAwMHBkemhwdjIxZGFzZCIsInBheWxvYWQiOiJ7XCJuYW1lXCI6XCJwZXJjb2JhYW5cIixcImRlc2NyaXB0aW9uXCI6XCJ1bnR1ayBwZXJjb2JhYW5cIixcImV4cGlyZWRBdFwiOlwiMjAzMS0xMC0yMFwifSIsImV4cCI6MTk1MDIyMDgwMCwiaWF0IjoxNzYwOTQyNTcwfQ.X4Y4MJ4aIohT65oJjHCaf2d6e8afnroXu3Hz-jH0WGM
curl https://cld-dkr-prod-jenna-mcp.wibudev.com/api/mcp-server/mcp \ curl https://cld-dkr-prod-jenna-mcp.wibudev.com/mcp-server/mcp \
--request POST \ --request POST \
--header 'Content-Type: application/json' \ --header 'Content-Type: application/json' \
--header 'Authorization: Bearer $TOKEN' \ --header 'Authorization: Bearer $TOKEN' \