From 3cc6f9f3cad4e947682792bf0cd84df4f5d91300 Mon Sep 17 00:00:00 2001 From: bipproduction Date: Mon, 27 Oct 2025 02:35:05 +0800 Subject: [PATCH] tambahan --- src/server/routes/mcp_route.ts | 90 +++++++++++++++------------------- 1 file changed, 40 insertions(+), 50 deletions(-) diff --git a/src/server/routes/mcp_route.ts b/src/server/routes/mcp_route.ts index e60c7fd..6e12ea1 100644 --- a/src/server/routes/mcp_route.ts +++ b/src/server/routes/mcp_route.ts @@ -1,58 +1,48 @@ -// src/routes/mcp_route.ts -import { Elysia, t } from "elysia"; +import { Elysia } from "elysia"; -const MCPRoute = new Elysia({ prefix: "/mcp" }) - .post("/", async ({ body, set }) => { - const { id = "123", method = "getTools" } = body; - - // Header wajib untuk MCP - set.status = 200; - set.headers["Content-Type"] = "application/json"; - set.headers["Transfer-Encoding"] = "chunked"; - set.headers["Connection"] = "keep-alive"; - - // ✔ Streaming response (langsung kirim chunk pertama) - const encoder = new TextEncoder(); - - const stream = new ReadableStream({ - start(controller) { - if (method === "getTools") { - const firstChunk = JSON.stringify({ +const MCPRoute = new Elysia({ prefix: "" }) // tanpa prefix agar langsung /mcp + .get("/mcp", ({ set }) => { + set.headers["Content-Type"] = "application/json; charset=utf-8"; + return { jsonrpc: "2.0", - id: id || "123", + id: null, result: { - data: [ - { - name: "Calculator", - value: "Calculator", - description: "Calculate math expression", - inputSchema: { - type: "object", - properties: { input: { type: "string" } }, - required: ["input"], - additionalProperties: true - } - } - ] - } - }); + protocol: "2024-11-05", + capabilities: { + "tools/list": true, + "tools/call": true, + }, + status: "MCP Server Ready", + }, + }; + }) + .post("/mcp", async ({ body, set }) => { + set.headers["Content-Type"] = "application/json; charset=utf-8"; - 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: id || "123", - error: { code: -32601, message: "Method not found" } - }))); - controller.close(); + const { id, method, params } = body as { id: string; method: string; params: any }; + + if (method === "getTools") { + return { + jsonrpc: "2.0", + id, + result: { + tools: [ + { name: "sayHello", description: "Returns a greeting" }, + { name: "getTime", description: "Current server time" }, + ], + }, + }; } - } + + // Default jika method salah + return { + jsonrpc: "2.0", + id, + error: { + code: -32601, + message: `Method ${method} not found`, + }, + }; }); - return stream; - },{ - body: t.Any() - }); - export default MCPRoute; \ No newline at end of file