This commit is contained in:
bipproduction
2025-10-27 02:52:10 +08:00
parent 11ba68daf3
commit 5903f461b9
2 changed files with 26 additions and 41 deletions

View File

@@ -1,57 +1,45 @@
import { Elysia } from "elysia";
export const MCPRoute = new Elysia()
// handshake awal (optional)
.get("/mcp", ({ set }) => {
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 }) => {
.get("/mcp/:sessionId", async ({ params, set }) => {
set.headers["Content-Type"] = "text/event-stream; charset=utf-8";
set.headers["Cache-Control"] = "no-cache";
set.headers["Connection"] = "keep-alive";
set.headers["Access-Control-Allow-Origin"] = "*";
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",
// kirim pesan handshake awal
const initial = {
jsonrpc: "2.0",
id: null,
result: {
protocol: "2024-11-05",
capabilities: {
"tools/list": true,
"tools/call": true,
},
})
);
status: `MCP session ${params.sessionId} aktif`,
},
};
controller.enqueue(encoder.encode(`data: ${JSON.stringify(initial)}\n\n`));
// jaga stream tetap hidup setiap 10 detik
const interval = setInterval(() => {
controller.enqueue(encoder.encode(`: ping\n\n`)); // komentar SSE
}, 10000);
// ketika stream ditutup
controller.enqueue(encoder.encode(`: stream ready\n\n`));
},
});
return stream;
})
// handler untuk JSONRPC POST
.post("/mcp", async ({ body, set }) => {
set.headers["Content-Type"] = "application/json; charset=utf-8";
const { id, method, params } = body as any;
@@ -98,10 +86,7 @@ export const MCPRoute = new Elysia()
return {
jsonrpc: "2.0",
id,
result: {
message: "Berhasil menerima pengajuan KTP",
data: args,
},
result: { message: "Berhasil menerima pengajuan KTP", data: args },
};
}
}

2
x.sh
View File

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