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,70 +1,117 @@
import { Elysia } from "elysia"; import { Elysia } from "elysia";
const MCPRoute = new Elysia() export const MCPRoute = new Elysia()
.get("/mcp", ({ set }) => { // handshake awal (optional)
set.headers["Content-Type"] = "application/json; charset=utf-8"; .get("/mcp", ({ set }) => {
return { set.headers["Content-Type"] = "application/json; charset=utf-8";
return {
jsonrpc: "2.0",
id: null,
result: {
protocol: "2024-11-05",
capabilities: {
"tools/list": true,
"tools/call": true,
},
status: "MCP Server Ready",
},
};
})
// 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", jsonrpc: "2.0",
id: null, id: null,
result: { result: {
protocol: "2024-11-05", protocol: "2024-11-05",
capabilities: { capabilities: {
"tools/list": true, "tools/list": true,
"tools/call": true, "tools/call": true,
}, },
status: "MCP Server Ready", status: "MCP Server Ready",
}, },
}; })
}) );
.post("/mcp", async ({ body, set }) => { },
set.headers["Content-Type"] = "application/json; charset=utf-8"; });
const { id, method, params } = body as any; return stream;
})
if (method === "tools/list") { // handler untuk JSONRPC POST
return { .post("/mcp", async ({ body, set }) => {
jsonrpc: "2.0", set.headers["Content-Type"] = "application/json; charset=utf-8";
id, const { id, method, params } = body as any;
result: {
tools: [ if (method === "tools/list") {
{ return {
name: "pengajuan-pembuatan-ktp", jsonrpc: "2.0",
description: "untuk melakukan pengajuan pembuatan ktp", id,
inputSchema: { result: {
type: "object", tools: [
properties: { {
JSON: { type: "object" }, name: "pengajuan-pembuatan-ktp",
}, description: "untuk melakukan pengajuan pembuatan ktp\nmembutuhkan :\n- jenis\n- name\n- deskripsi",
required: ["JSON"], inputSchema: {
additionalProperties: true, type: "object",
}, properties: {
}, JSON: { type: "object" },
// ... tambahkan tool lain
],
}, },
}; required: ["JSON"],
} additionalProperties: true,
$schema: "http://json-schema.org/draft-07/schema#",
if (method === "tools/call") { },
// contoh sederhana },
const { tool, arguments: args } = params; {
if (tool === "pengajuan-pembuatan-ktp") { name: "pengetahuan_malik_kurosaki",
return { description: "penjelasan tentang malik kurosaki",
jsonrpc: "2.0", inputSchema: {
id, type: "object",
result: { message: "Berhasil menerima pengajuan KTP", data: args }, properties: {
}; input: { type: "string" },
} },
} additionalProperties: true,
$schema: "http://json-schema.org/draft-07/schema#",
},
},
],
},
};
}
if (method === "tools/call") {
const { tool, arguments: args } = params;
if (tool === "pengajuan-pembuatan-ktp") {
return { return {
jsonrpc: "2.0", jsonrpc: "2.0",
id, id,
error: { result: {
code: -32601, message: "Berhasil menerima pengajuan KTP",
message: `Method ${method} tidak dikenali`, data: args,
}, },
}; };
}) }
}
export default MCPRoute; return {
jsonrpc: "2.0",
id,
error: {
code: -32601,
message: `Method ${method} tidak dikenali`,
},
};
});

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"