tambahan
This commit is contained in:
@@ -2,16 +2,21 @@ import { Elysia } from "elysia";
|
|||||||
|
|
||||||
export const MCPRoute = new Elysia()
|
export const MCPRoute = new Elysia()
|
||||||
.get("/mcp/:sessionId", async ({ params, set }) => {
|
.get("/mcp/:sessionId", async ({ params, set }) => {
|
||||||
set.headers["Content-Type"] = "text/event-stream; charset=utf-8";
|
// Atur header SSE
|
||||||
set.headers["Cache-Control"] = "no-cache";
|
set.headers = {
|
||||||
set.headers["Connection"] = "keep-alive";
|
"Content-Type": "text/event-stream; charset=utf-8",
|
||||||
set.headers["Access-Control-Allow-Origin"] = "*";
|
"Cache-Control": "no-cache",
|
||||||
|
Connection: "keep-alive",
|
||||||
|
"Access-Control-Allow-Origin": "*",
|
||||||
|
};
|
||||||
|
|
||||||
const encoder = new TextEncoder();
|
const encoder = new TextEncoder();
|
||||||
|
|
||||||
|
let interval: NodeJS.Timeout;
|
||||||
|
|
||||||
const stream = new ReadableStream({
|
const stream = new ReadableStream({
|
||||||
start(controller) {
|
start(controller) {
|
||||||
// kirim pesan handshake awal
|
// kirim handshake awal
|
||||||
const initial = {
|
const initial = {
|
||||||
jsonrpc: "2.0",
|
jsonrpc: "2.0",
|
||||||
id: null,
|
id: null,
|
||||||
@@ -27,13 +32,18 @@ export const MCPRoute = new Elysia()
|
|||||||
|
|
||||||
controller.enqueue(encoder.encode(`data: ${JSON.stringify(initial)}\n\n`));
|
controller.enqueue(encoder.encode(`data: ${JSON.stringify(initial)}\n\n`));
|
||||||
|
|
||||||
// jaga stream tetap hidup setiap 10 detik
|
// jaga stream tetap hidup
|
||||||
const interval = setInterval(() => {
|
interval = setInterval(() => {
|
||||||
controller.enqueue(encoder.encode(`: ping\n\n`)); // komentar SSE
|
controller.enqueue(encoder.encode(`: ping ${Date.now()}\n\n`));
|
||||||
}, 10000);
|
}, 10000);
|
||||||
|
|
||||||
// ketika stream ditutup
|
// kirim ready event
|
||||||
controller.enqueue(encoder.encode(`: stream ready\n\n`));
|
controller.enqueue(encoder.encode(`event: ready\ndata: "stream ready"\n\n`));
|
||||||
|
},
|
||||||
|
|
||||||
|
cancel() {
|
||||||
|
clearInterval(interval);
|
||||||
|
console.log(`SSE connection ${params.sessionId} closed by client`);
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -52,7 +62,8 @@ export const MCPRoute = new Elysia()
|
|||||||
tools: [
|
tools: [
|
||||||
{
|
{
|
||||||
name: "pengajuan-pembuatan-ktp",
|
name: "pengajuan-pembuatan-ktp",
|
||||||
description: "untuk melakukan pengajuan pembuatan ktp\nmembutuhkan :\n- jenis\n- name\n- deskripsi",
|
description:
|
||||||
|
"untuk melakukan pengajuan pembuatan ktp\nmembutuhkan :\n- jenis\n- name\n- deskripsi",
|
||||||
inputSchema: {
|
inputSchema: {
|
||||||
type: "object",
|
type: "object",
|
||||||
properties: {
|
properties: {
|
||||||
|
|||||||
Reference in New Issue
Block a user