This commit is contained in:
bipproduction
2025-10-27 02:18:40 +08:00
parent db55abd0f0
commit 0ee273cd45

View File

@@ -1,42 +1,56 @@
// src/routes/mcp_route.ts
import { Elysia } from "elysia";
const tools = [
{
name: "pengajuan-pembuatan-ktp",
value: "pengajuan-pembuatan-ktp",
description: "untuk melakukan pengajuan pembuatan ktp\nmembutuhkan :\n- jenis\n- name\n- deskripsi",
inputSchema: {
type: "object",
properties: { JSON: {} },
required: ["JSON"],
additionalProperties: true,
$schema: "http://json-schema.org/draft-07/schema#"
}
},
{
name: "list-layanan-desa-darmasaba",
value: "list-layanan-desa-darmasaba",
description: "mendapatkan list layanan desa darmasaba",
inputSchema: {
type: "object",
properties: {},
additionalProperties: true,
$schema: "http://json-schema.org/draft-07/schema#"
}
}
];
const MCPRoute = new Elysia({ prefix: "/mcp" })
.post("/", async ({ body, set }) => {
set.headers["Content-Type"] = "application/json; charset=utf-8";
const { id, method } = body as { id: string; method: string };
const { methodName } = body as { methodName: string };
// Header wajib untuk MCP
set.status = 200;
set.headers["Content-Type"] = "application/json";
set.headers["Transfer-Encoding"] = "chunked";
set.headers["Connection"] = "keep-alive";
if (methodName === "getTools") {
return { data: tools }; // ✅ sesuai format yang n8n minta
}
// ✔ Streaming response (langsung kirim chunk pertama)
const encoder = new TextEncoder();
return { error: "Unsupported method" };
const stream = new ReadableStream({
start(controller) {
if (method === "getTools") {
const firstChunk = JSON.stringify({
jsonrpc: "2.0",
id,
result: {
data: [
{
name: "Calculator",
value: "Calculator",
description: "Calculate math expression",
inputSchema: {
type: "object",
properties: { input: { type: "string" } },
required: ["input"],
additionalProperties: true
}
}
]
}
});
controller.enqueue(encoder.encode(firstChunk + "\n"));
controller.close(); // jika tidak mau streaming penuh, tutup
} else {
controller.enqueue(encoder.encode(JSON.stringify({
jsonrpc: "2.0",
id,
error: { code: -32601, message: "Method not found" }
})));
controller.close();
}
}
});
return stream;
});
export default MCPRoute;
export default MCPRoute;