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