This commit is contained in:
bipproduction
2025-10-27 02:08:10 +08:00
parent f2570bef05
commit 053ed4338e

View File

@@ -1,35 +1,59 @@
import { Elysia } from "elysia"; import { Elysia } from "elysia";
const MCPRoute = new Elysia({ export const MCPRoute = new Elysia({
prefix: "/mcp", prefix: "/mcp",
tags: ["mcp"], tags: ["mcp"],
}) })
// ✅ n8n Fetch Tools → HARUS format { data: [...] } // ✅ GET /mcp → Harus balikan format MCP metadata + tools (tanpa stream)
.get("/", ({ set }) => { .get("/", ({ set }) => {
set.headers["Content-Type"] = "application/json; charset=utf-8"; set.headers["Content-Type"] = "application/json; charset=utf-8";
return { return {
data: [ protocol: "2024-11-05",
capabilities: {
"tools/list": true,
"tools/call": true,
},
tools: [
{ {
name: "sayHello", name: "sayHello",
value: "sayHello", description: "Greets a user and returns a message",
description: "Greets a user",
inputSchema: { inputSchema: {
type: "object", type: "object",
properties: { properties: {
name: { type: "string", description: "Your name" }, name: { type: "string", description: "Name of the person" },
}, },
required: ["name"], required: ["name"],
additionalProperties: false,
}, },
}, },
], ],
}; };
}) })
// ✅ Tools Execution (Streaming / JSON-RPC Optional) // ✅ GET /mcp/tools/list → Untuk eksplisit list tool
.get("/call", ({ set, query }) => { .get("/tools/list", ({ set }) => {
const method = query.method; set.headers["Content-Type"] = "application/json; charset=utf-8";
const name = query.name || "User"; return {
tools: [
{
name: "sayHello",
description: "Greets a user",
inputSchema: {
type: "object",
properties: {
name: { type: "string" },
},
required: ["name"],
},
},
],
};
})
// ✅ 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["Content-Type"] = "application/json; charset=utf-8";
set.headers["Cache-Control"] = "no-cache"; set.headers["Cache-Control"] = "no-cache";
@@ -40,21 +64,19 @@ const MCPRoute = new Elysia({
async start(controller) { async start(controller) {
controller.enqueue(":\n\n"); // Heartbeat controller.enqueue(":\n\n"); // Heartbeat
if (method === "sayHello") { if (toolName === "sayHello") {
controller.enqueue(JSON.stringify({ status: "processing" }) + "\n"); controller.enqueue(JSON.stringify({ status: "processing" }) + "\n");
await Bun.sleep(500); await Bun.sleep(500);
controller.enqueue( controller.enqueue(
JSON.stringify({ result: `Hello ${name}` }) + "\n" JSON.stringify({ result: `Hello ${user}! 👋` }) + "\n"
);
controller.close();
} else {
controller.enqueue(
JSON.stringify({ error: `Unknown tool: ${toolName}` }) + "\n"
); );
controller.close(); controller.close();
return;
} }
controller.enqueue(
JSON.stringify({ error: `Method ${method} not found` }) + "\n"
);
controller.close();
}, },
}); });