This commit is contained in:
bipproduction
2025-10-27 02:47:39 +08:00
parent 781db9b1d1
commit 11ba68daf3
3 changed files with 112 additions and 60 deletions

View File

@@ -10,8 +10,9 @@ import { convertOpenApiToMcp } from "./server/lib/mcp-converter";
import UserRoute from "./server/routes/user_route"; import UserRoute from "./server/routes/user_route";
import LayananRoute from "./server/routes/layanan_route"; import LayananRoute from "./server/routes/layanan_route";
import AduanRoute from "./server/routes/aduan_route"; import AduanRoute from "./server/routes/aduan_route";
import MCPRoute from "./server/routes/mcp_route";
import { cors } from "@elysiajs/cors"; import { cors } from "@elysiajs/cors";
import { MCPRoute } from "./server/routes/mcp_route";
const Docs = new Elysia({ const Docs = new Elysia({
tags: ["docs"], tags: ["docs"],
@@ -34,7 +35,6 @@ const Api = new Elysia({
.use(AduanRoute); .use(AduanRoute);
const app = new Elysia() const app = new Elysia()
.use(cors())
.use(Api) .use(Api)
.use(Docs) .use(Docs)
.use(Auth) .use(Auth)
@@ -53,6 +53,11 @@ const app = new Elysia()
) )
.use(MCPRoute) .use(MCPRoute)
// .get("/*", html) // .get("/*", html)
.onRequest(({ set }) => {
set.headers["Access-Control-Allow-Origin"] = "*";
set.headers["Access-Control-Allow-Methods"] = "GET, POST, OPTIONS";
set.headers["Access-Control-Allow-Headers"] = "Content-Type";
})
.listen(3000, () => { .listen(3000, () => {
console.log("Server running at http://localhost:3000"); console.log("Server running at http://localhost:3000");
}); });

View File

@@ -1,6 +1,7 @@
import { Elysia } from "elysia"; import { Elysia } from "elysia";
const MCPRoute = new Elysia() export const MCPRoute = new Elysia()
// handshake awal (optional)
.get("/mcp", ({ set }) => { .get("/mcp", ({ set }) => {
set.headers["Content-Type"] = "application/json; charset=utf-8"; set.headers["Content-Type"] = "application/json; charset=utf-8";
return { return {
@@ -16,9 +17,43 @@ const MCPRoute = new Elysia()
}, },
}; };
}) })
// endpoint MCP streaming
.get("/mcp/:sessionId", async ({ set }) => {
set.headers["Content-Type"] = "text/event-stream; charset=utf-8";
set.headers["Cache-Control"] = "no-cache";
set.headers["Connection"] = "keep-alive";
const encoder = new TextEncoder();
const send = (obj: any) => encoder.encode(`data: ${JSON.stringify(obj)}\n\n`);
const stream = new ReadableStream({
start(controller) {
// kirim pesan awal
controller.enqueue(
send({
jsonrpc: "2.0",
id: null,
result: {
protocol: "2024-11-05",
capabilities: {
"tools/list": true,
"tools/call": true,
},
status: "MCP Server Ready",
},
})
);
},
});
return stream;
})
// handler untuk JSONRPC POST
.post("/mcp", async ({ body, set }) => { .post("/mcp", async ({ body, set }) => {
set.headers["Content-Type"] = "application/json; charset=utf-8"; set.headers["Content-Type"] = "application/json; charset=utf-8";
const { id, method, params } = body as any; const { id, method, params } = body as any;
if (method === "tools/list") { if (method === "tools/list") {
@@ -29,7 +64,7 @@ const MCPRoute = new Elysia()
tools: [ tools: [
{ {
name: "pengajuan-pembuatan-ktp", name: "pengajuan-pembuatan-ktp",
description: "untuk melakukan pengajuan pembuatan ktp", description: "untuk melakukan pengajuan pembuatan ktp\nmembutuhkan :\n- jenis\n- name\n- deskripsi",
inputSchema: { inputSchema: {
type: "object", type: "object",
properties: { properties: {
@@ -37,22 +72,36 @@ const MCPRoute = new Elysia()
}, },
required: ["JSON"], required: ["JSON"],
additionalProperties: true, additionalProperties: true,
$schema: "http://json-schema.org/draft-07/schema#",
},
},
{
name: "pengetahuan_malik_kurosaki",
description: "penjelasan tentang malik kurosaki",
inputSchema: {
type: "object",
properties: {
input: { type: "string" },
},
additionalProperties: true,
$schema: "http://json-schema.org/draft-07/schema#",
}, },
}, },
// ... tambahkan tool lain
], ],
}, },
}; };
} }
if (method === "tools/call") { if (method === "tools/call") {
// contoh sederhana
const { tool, arguments: args } = params; const { tool, arguments: args } = params;
if (tool === "pengajuan-pembuatan-ktp") { if (tool === "pengajuan-pembuatan-ktp") {
return { return {
jsonrpc: "2.0", jsonrpc: "2.0",
id, id,
result: { message: "Berhasil menerima pengajuan KTP", data: args }, result: {
message: "Berhasil menerima pengajuan KTP",
data: args,
},
}; };
} }
} }
@@ -65,6 +114,4 @@ const MCPRoute = new Elysia()
message: `Method ${method} tidak dikenali`, message: `Method ${method} tidak dikenali`,
}, },
}; };
}) });
export default MCPRoute;

2
x.sh
View File

@@ -1,2 +1,2 @@
curl -N -v -X GET "https://cld-dkr-prod-jenna-mcp.wibudev.com/mcp" curl -N -v -X GET "https://n8n.wibudev.com/mcp/fd665648-b38d-4bee-9ab8-11ca0cd83d0d"