tambahan
This commit is contained in:
@@ -1,15 +1,21 @@
|
|||||||
import { Elysia } from "elysia";
|
import { Elysia } from "elysia";
|
||||||
|
|
||||||
export const MCPRoute = new Elysia()
|
export const MCPRoute = new Elysia()
|
||||||
.get("/mcp/:sessionId", ({ params }) => {
|
.get("/mcp/:sessionId", ({ set, params }) => {
|
||||||
const encoder = new TextEncoder();
|
set.headers["Content-Type"] = "text/event-stream; charset=utf-8";
|
||||||
|
set.headers["Cache-Control"] = "no-cache";
|
||||||
|
set.headers["Connection"] = "keep-alive";
|
||||||
|
|
||||||
let interval: Timer | null = null;
|
const encoder = new TextEncoder();
|
||||||
|
|
||||||
const stream = new ReadableStream({
|
const stream = new ReadableStream({
|
||||||
start(controller) {
|
start(controller) {
|
||||||
// kirim event awal (flush langsung)
|
const send = (obj: any) => {
|
||||||
const init = {
|
controller.enqueue(encoder.encode(`data: ${JSON.stringify(obj)}\n\n`));
|
||||||
|
};
|
||||||
|
|
||||||
|
// Kirim pesan pembuka
|
||||||
|
send({
|
||||||
jsonrpc: "2.0",
|
jsonrpc: "2.0",
|
||||||
id: null,
|
id: null,
|
||||||
result: {
|
result: {
|
||||||
@@ -18,94 +24,26 @@ export const MCPRoute = new Elysia()
|
|||||||
"tools/list": true,
|
"tools/list": true,
|
||||||
"tools/call": true,
|
"tools/call": true,
|
||||||
},
|
},
|
||||||
status: `MCP session ${params.sessionId} aktif`,
|
status: `MCP Stream aktif untuk session ${params.sessionId}`,
|
||||||
},
|
},
|
||||||
};
|
});
|
||||||
|
|
||||||
controller.enqueue(
|
// Keep connection alive
|
||||||
encoder.encode(`data: ${JSON.stringify(init)}\n\n`)
|
const interval = setInterval(() => {
|
||||||
);
|
send({ ping: Date.now() });
|
||||||
|
}, 15000);
|
||||||
|
|
||||||
// SSE heartbeat
|
// Simpan handle agar bisa dibersihkan nanti
|
||||||
interval = setInterval(() => {
|
(controller as any)._interval = interval;
|
||||||
controller.enqueue(encoder.encode(`: ping ${Date.now()}\n\n`));
|
|
||||||
}, 10000);
|
|
||||||
},
|
},
|
||||||
|
|
||||||
cancel() {
|
cancel() {
|
||||||
if (interval) clearInterval(interval);
|
// Ketika stream dibatalkan oleh client
|
||||||
console.log(`[SSE] koneksi session ${params.sessionId} ditutup`);
|
console.log("🔌 Stream closed by client");
|
||||||
|
// Bersihkan interval
|
||||||
|
clearInterval((this as any)._interval);
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
// gunakan Response manual agar Bun flush data pertama
|
return stream;
|
||||||
return new Response(stream, {
|
|
||||||
headers: {
|
|
||||||
"Content-Type": "text/event-stream; charset=utf-8",
|
|
||||||
"Cache-Control": "no-cache, no-transform",
|
|
||||||
Connection: "keep-alive",
|
|
||||||
"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;
|
|
||||||
|
|
||||||
if (method === "tools/list") {
|
|
||||||
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#",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
],
|
|
||||||
},
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
if (method === "tools/call") {
|
|
||||||
const { tool, arguments: args } = params;
|
|
||||||
if (tool === "pengajuan-pembuatan-ktp") {
|
|
||||||
return {
|
|
||||||
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`,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user