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

View File

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