tambahan
This commit is contained in:
@@ -1,86 +1,40 @@
|
||||
import { Elysia } from "elysia";
|
||||
|
||||
export const MCPRoute = new Elysia({
|
||||
prefix: "/mcp",
|
||||
tags: ["mcp"],
|
||||
})
|
||||
// ✅ GET /mcp → Harus balikan format MCP metadata + tools (tanpa stream)
|
||||
.get("/", ({ set }) => {
|
||||
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#"
|
||||
}
|
||||
}
|
||||
];
|
||||
|
||||
export const MCPRoute = new Elysia({ prefix: "/mcp" })
|
||||
.post("/", async ({ body, set }) => {
|
||||
set.headers["Content-Type"] = "application/json; charset=utf-8";
|
||||
|
||||
return {
|
||||
protocol: "2024-11-05",
|
||||
capabilities: {
|
||||
"tools/list": true,
|
||||
"tools/call": true,
|
||||
},
|
||||
tools: [
|
||||
{
|
||||
name: "sayHello",
|
||||
description: "Greets a user and returns a message",
|
||||
inputSchema: {
|
||||
type: "object",
|
||||
properties: {
|
||||
name: { type: "string", description: "Name of the person" },
|
||||
},
|
||||
required: ["name"],
|
||||
},
|
||||
},
|
||||
],
|
||||
};
|
||||
})
|
||||
const { methodName } = body as { methodName: string };
|
||||
|
||||
// ✅ GET /mcp/tools/list → Untuk eksplisit list tool
|
||||
.get("/tools/list", ({ set }) => {
|
||||
set.headers["Content-Type"] = "application/json; charset=utf-8";
|
||||
return {
|
||||
tools: [
|
||||
{
|
||||
name: "sayHello",
|
||||
description: "Greets a user",
|
||||
inputSchema: {
|
||||
type: "object",
|
||||
properties: {
|
||||
name: { type: "string" },
|
||||
},
|
||||
required: ["name"],
|
||||
},
|
||||
},
|
||||
],
|
||||
};
|
||||
})
|
||||
if (methodName === "getTools") {
|
||||
return { data: tools }; // ✅ sesuai format yang n8n minta
|
||||
}
|
||||
|
||||
// ✅ GET /mcp/tools/call?name=sayHello&name=Malik → streaming output
|
||||
.get("/tools/call", ({ set, query }) => {
|
||||
const toolName = query.name ?? "";
|
||||
const user = query.user ?? "User";
|
||||
|
||||
set.headers["Content-Type"] = "application/json; charset=utf-8";
|
||||
set.headers["Cache-Control"] = "no-cache";
|
||||
set.headers["Connection"] = "keep-alive";
|
||||
set.headers["Transfer-Encoding"] = "chunked";
|
||||
|
||||
const stream = new ReadableStream({
|
||||
async start(controller) {
|
||||
controller.enqueue(":\n\n"); // Heartbeat
|
||||
|
||||
if (toolName === "sayHello") {
|
||||
controller.enqueue(JSON.stringify({ status: "processing" }) + "\n");
|
||||
await Bun.sleep(500);
|
||||
controller.enqueue(
|
||||
JSON.stringify({ result: `Hello ${user}! 👋` }) + "\n"
|
||||
);
|
||||
controller.close();
|
||||
} else {
|
||||
controller.enqueue(
|
||||
JSON.stringify({ error: `Unknown tool: ${toolName}` }) + "\n"
|
||||
);
|
||||
controller.close();
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
return new Response(stream);
|
||||
return { error: "Unsupported method" };
|
||||
});
|
||||
|
||||
export default MCPRoute;
|
||||
|
||||
Reference in New Issue
Block a user