This commit is contained in:
bipproduction
2025-10-26 21:42:37 +08:00
parent 948bf14312
commit a0434c3c32

View File

@@ -4,17 +4,32 @@ export const MCPRoute = new Elysia({
prefix: "/mcp-server", prefix: "/mcp-server",
tags: ["mcp-server"], tags: ["mcp-server"],
}) })
.post("/mcp", ({ body, set }) => {
const { id, method, params } = body as any;
// ✅ 1. GET untuk handshake n8n (wajib)
.get("/mcp", ({ set }) => {
set.headers["Content-Type"] = "application/json";
return {
jsonrpc: "2.0",
result: {
protocol: "2024-11-05",
capabilities: {
"tools/list": true,
"tools/call": true,
}
}
};
})
// ✅ 2. POST untuk komunikasi streaming (tools/list, tools/call)
.post("/mcp", ({ body, set }) => {
const { id, method, params } = body as any;
set.headers["Content-Type"] = "application/json; charset=utf-8"; 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
if (method === "tools/list") { if (method === "tools/list") {
controller.enqueue( controller.enqueue(
JSON.stringify({ JSON.stringify({
@@ -26,81 +41,51 @@ export const MCPRoute = new Elysia({
description: "Greets user", description: "Greets user",
inputSchema: { inputSchema: {
type: "object", type: "object",
properties: { name: { type: "string" } }, properties: { name: { type: "string" } }
},
},
],
}) + "\n"
);
// ❌ Jangan tutup langsung, beri delay agar n8n sempat membaca
await Bun.sleep(200);
controller.close();
return;
} }
// tools/sayHello → streaming progress
if (method === "tools/sayHello") {
controller.enqueue(
JSON.stringify({
jsonrpc: "2.0",
id,
result: { status: "Processing..." },
}) + "\n"
);
await Bun.sleep(500);
controller.enqueue(
JSON.stringify({
jsonrpc: "2.0",
id,
result: { message: `Hello ${params?.name || "User"}` },
}) + "\n"
);
await Bun.sleep(200);
controller.close();
return;
} }
]
if (method === "mcp/version") {
controller.enqueue(
JSON.stringify({
jsonrpc: "2.0",
id,
result: {
protocol: "2024-11-05", // versi MCP baru
capabilities: {
"tools/list": true,
"tools/call": true,
},
},
}) + "\n" }) + "\n"
); );
controller.close(); controller.close();
return; return;
} }
// Method tidak dikenal if (method === "tools/call" && params?.name === "sayHello") {
controller.enqueue(
JSON.stringify({ jsonrpc: "2.0", id, result: { status: "Processing..." } }) + "\n"
);
await Bun.sleep(300);
controller.enqueue( controller.enqueue(
JSON.stringify({ JSON.stringify({
jsonrpc: "2.0", jsonrpc: "2.0",
id, id,
error: { code: -32601, message: `Method ${method} not found` }, result: { message: `Hello ${params?.arguments?.name || "User"}!` }
}) + "\n" }) + "\n"
); );
await Bun.sleep(200);
controller.close(); controller.close();
}, return;
}
controller.enqueue(
JSON.stringify({
jsonrpc: "2.0",
id,
error: { code: -32601, message: `Method ${method} not found` }
}) + "\n"
);
controller.close();
}
}); });
return new Response(stream); return new Response(stream);
}, { }, {
body: t.Object({ body: t.Object({
jsonrpc: t.Optional(t.String()), jsonrpc: t.Optional(t.String()),
method: t.String(), method: t.String(),
params: t.Optional(t.Record(t.String(), t.Any())), params: t.Optional(t.Any()),
id: t.Optional(t.Union([t.String(), t.Number()])), id: t.Optional(t.Union([t.String(), t.Number()])),
}), }),
}); });
export default MCPRoute; export default MCPRoute;