tambahan
This commit is contained in:
@@ -1,49 +1,117 @@
|
|||||||
import { Elysia } from "elysia";
|
import { Elysia } from "elysia";
|
||||||
|
|
||||||
export const MCPRoute = new Elysia()
|
export const MCPRoute = new Elysia()
|
||||||
.get("/mcp/:sessionId", ({ set, params }) => {
|
.get("/mcp/:sessionId", ({ params }) => {
|
||||||
set.headers["Content-Type"] = "text/event-stream; charset=utf-8";
|
const encoder = new TextEncoder();
|
||||||
set.headers["Cache-Control"] = "no-cache";
|
let interval: Timer | null = null;
|
||||||
set.headers["Connection"] = "keep-alive";
|
|
||||||
|
|
||||||
const encoder = new TextEncoder();
|
const stream = new ReadableStream({
|
||||||
|
start(controller) {
|
||||||
|
// Kirim event awal
|
||||||
|
const init = {
|
||||||
|
jsonrpc: "2.0",
|
||||||
|
id: null,
|
||||||
|
result: {
|
||||||
|
protocol: "2024-11-05",
|
||||||
|
capabilities: {
|
||||||
|
"tools/list": true,
|
||||||
|
"tools/call": true,
|
||||||
|
},
|
||||||
|
status: `MCP session ${params.sessionId} aktif`,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
const stream = new ReadableStream({
|
// Kirim data dan flush
|
||||||
start(controller) {
|
controller.enqueue(
|
||||||
const send = (obj: any) => {
|
encoder.encode(`data: ${JSON.stringify(init)}\n\n`)
|
||||||
controller.enqueue(encoder.encode(`data: ${JSON.stringify(obj)}\n\n`));
|
);
|
||||||
};
|
|
||||||
|
|
||||||
// Kirim pesan pembuka
|
// SSE heartbeat
|
||||||
send({
|
interval = setInterval(() => {
|
||||||
jsonrpc: "2.0",
|
try {
|
||||||
id: null,
|
controller.enqueue(encoder.encode(`: ping ${Date.now()}\n\n`));
|
||||||
result: {
|
} catch (e) {
|
||||||
protocol: "2024-11-05",
|
if (interval) clearInterval(interval);
|
||||||
capabilities: {
|
}
|
||||||
"tools/list": true,
|
}, 10000);
|
||||||
"tools/call": true,
|
|
||||||
|
console.log(`[SSE] koneksi session ${params.sessionId} dibuka`);
|
||||||
|
},
|
||||||
|
cancel() {
|
||||||
|
if (interval) clearInterval(interval);
|
||||||
|
console.log(`[SSE] koneksi session ${params.sessionId} ditutup`);
|
||||||
},
|
},
|
||||||
status: `MCP Stream aktif untuk session ${params.sessionId}`,
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// Keep connection alive
|
return new Response(stream, {
|
||||||
const interval = setInterval(() => {
|
status: 200,
|
||||||
send({ ping: Date.now() });
|
headers: {
|
||||||
}, 15000);
|
"Content-Type": "text/event-stream; charset=utf-8",
|
||||||
|
"Cache-Control": "no-cache, no-transform",
|
||||||
|
"Connection": "keep-alive",
|
||||||
|
"X-Accel-Buffering": "no", // Penting untuk nginx/cloudflare
|
||||||
|
"Access-Control-Allow-Origin": "*",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
})
|
||||||
|
.post("/mcp", async ({ body, set }) => {
|
||||||
|
set.headers["Content-Type"] = "application/json; charset=utf-8";
|
||||||
|
const { id, method, params } = body as any;
|
||||||
|
|
||||||
// Simpan handle agar bisa dibersihkan nanti
|
if (method === "tools/list") {
|
||||||
(controller as any)._interval = interval;
|
return {
|
||||||
},
|
jsonrpc: "2.0",
|
||||||
|
id,
|
||||||
|
result: {
|
||||||
|
tools: [
|
||||||
|
{
|
||||||
|
name: "pengajuan-pembuatan-ktp",
|
||||||
|
description:
|
||||||
|
"untuk melakukan pengajuan pembuatan ktp\nmembutuhkan :\n- jenis\n- name\n- deskripsi",
|
||||||
|
inputSchema: {
|
||||||
|
type: "object",
|
||||||
|
properties: {
|
||||||
|
JSON: { type: "object" },
|
||||||
|
},
|
||||||
|
required: ["JSON"],
|
||||||
|
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#",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
cancel() {
|
if (method === "tools/call") {
|
||||||
// Ketika stream dibatalkan oleh client
|
const { tool, arguments: args } = params;
|
||||||
console.log("🔌 Stream closed by client");
|
if (tool === "pengajuan-pembuatan-ktp") {
|
||||||
// Bersihkan interval
|
return {
|
||||||
clearInterval((this as any)._interval);
|
jsonrpc: "2.0",
|
||||||
},
|
id,
|
||||||
|
result: { message: "Berhasil menerima pengajuan KTP", data: args },
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
jsonrpc: "2.0",
|
||||||
|
id,
|
||||||
|
error: {
|
||||||
|
code: -32601,
|
||||||
|
message: `Method ${method} tidak dikenali`,
|
||||||
|
},
|
||||||
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
return stream;
|
|
||||||
});
|
|
||||||
|
|||||||
Reference in New Issue
Block a user