Files
jenna-mcp/src/server/routes/mcp_route.ts
bipproduction cd7784a375 tambahan
2025-10-26 21:17:51 +08:00

82 lines
2.2 KiB
TypeScript

import { Elysia, t } from "elysia";
export const MCPRoute = new Elysia({
prefix: "/mcp-server", // ✅ Sesuaikan dengan endpoint n8n
tags: ["mcp-server"],
})
.post("/mcp", ({ body, set }) => {
const { id, method, params } = body as any;
set.headers["Content-Type"] = "application/json";
set.headers["Transfer-Encoding"] = "chunked";
set.headers["Connection"] = "keep-alive";
const stream = new ReadableStream({
async start(controller) {
// tools.list → kirim daftar tools
if (method === "tools/list") {
controller.enqueue(
JSON.stringify({
jsonrpc: "2.0",
id,
result: [
{
name: "sayHello",
description: "Greets user",
inputSchema: {
type: "object",
properties: { name: { type: "string" } },
},
},
],
}) + "\n"
);
controller.close();
}
// tools.sayHello → streaming bertahap
else if (method === "tools/sayHello") {
controller.enqueue(
JSON.stringify({
jsonrpc: "2.0",
id,
result: { message: "Processing..." },
}) + "\n"
);
await Bun.sleep(500);
controller.enqueue(
JSON.stringify({
jsonrpc: "2.0",
id,
result: { message: `Hello ${params?.name || ""}` },
}) + "\n"
);
controller.close();
}
// Method tidak ditemukan
else {
controller.enqueue(
JSON.stringify({
jsonrpc: "2.0",
id,
error: { code: -32601, message: `Method ${method} not found` },
}) + "\n"
);
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;