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,38 +1,18 @@
import { Elysia } from "elysia"; import { Elysia } from "elysia";
export const MCPRoute = new Elysia() export const MCPRoute = new Elysia()
// handshake awal (optional) .get("/mcp/:sessionId", async ({ params, set }) => {
.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 }) => {
set.headers["Content-Type"] = "text/event-stream; charset=utf-8"; set.headers["Content-Type"] = "text/event-stream; charset=utf-8";
set.headers["Cache-Control"] = "no-cache"; set.headers["Cache-Control"] = "no-cache";
set.headers["Connection"] = "keep-alive"; set.headers["Connection"] = "keep-alive";
set.headers["Access-Control-Allow-Origin"] = "*";
const encoder = new TextEncoder(); const encoder = new TextEncoder();
const send = (obj: any) => encoder.encode(`data: ${JSON.stringify(obj)}\n\n`);
const stream = new ReadableStream({ const stream = new ReadableStream({
start(controller) { start(controller) {
// kirim pesan awal // kirim pesan handshake awal
controller.enqueue( const initial = {
send({
jsonrpc: "2.0", jsonrpc: "2.0",
id: null, id: null,
result: { result: {
@@ -41,17 +21,25 @@ export const MCPRoute = new Elysia()
"tools/list": true, "tools/list": true,
"tools/call": true, "tools/call": true,
}, },
status: "MCP Server Ready", 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; 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;
@@ -98,10 +86,7 @@ export const MCPRoute = new Elysia()
return { return {
jsonrpc: "2.0", jsonrpc: "2.0",
id, id,
result: { result: { message: "Berhasil menerima pengajuan KTP", data: args },
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"