tambahan
This commit is contained in:
@@ -1,117 +1,144 @@
|
||||
import { Elysia } from "elysia";
|
||||
import { v4 as uuidv4 } from "uuid";
|
||||
|
||||
const API_KEY = process.env.MCP_API_KEY ?? "change-me";
|
||||
const PORT = Number(process.env.PORT ?? 3000);
|
||||
const PING_INTERVAL_MS = 25_000;
|
||||
|
||||
// =====================
|
||||
// Store session & clients
|
||||
// =====================
|
||||
type Client = {
|
||||
id: string;
|
||||
send: (data: string) => void;
|
||||
close: () => void;
|
||||
};
|
||||
|
||||
const sessions = new Map<string, Set<Client>>();
|
||||
|
||||
// =====================
|
||||
// Helper Functions
|
||||
// =====================
|
||||
function isAuthorized(headers: Headers) {
|
||||
return headers.get("x-api-key") === API_KEY;
|
||||
}
|
||||
|
||||
function formatSSE(event: string, data: any, id?: string) {
|
||||
const payload = typeof data === "string" ? data : JSON.stringify(data);
|
||||
return [
|
||||
id ? `id: ${id}` : "",
|
||||
event ? `event: ${event}` : "",
|
||||
...payload.split("\n").map((line) => `data: ${line}`),
|
||||
"",
|
||||
].join("\n");
|
||||
}
|
||||
|
||||
function broadcast(sessionId: string, event: string, data: any) {
|
||||
const clients = sessions.get(sessionId);
|
||||
if (!clients) return 0;
|
||||
const messageId = uuidv4();
|
||||
const message = formatSSE(event, data, messageId);
|
||||
|
||||
for (const client of clients) {
|
||||
try {
|
||||
client.send(message);
|
||||
} catch {
|
||||
clients.delete(client);
|
||||
}
|
||||
}
|
||||
return clients.size;
|
||||
}
|
||||
|
||||
// =====================
|
||||
// Server Initialization
|
||||
// =====================
|
||||
export const MCPRoute = new Elysia()
|
||||
.get("/mcp/:sessionId", ({ params }) => {
|
||||
const encoder = new TextEncoder();
|
||||
let interval: Timer | null = null;
|
||||
.get("/mcp/:sessionId", ({ params, set }) => {
|
||||
const { sessionId } = params;
|
||||
|
||||
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`,
|
||||
},
|
||||
};
|
||||
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"] = "*";
|
||||
|
||||
// Kirim data dan flush
|
||||
controller.enqueue(
|
||||
encoder.encode(`data: ${JSON.stringify(init)}\n\n`)
|
||||
);
|
||||
// Create a readable stream for SSE
|
||||
const stream = new TransformStream();
|
||||
const writer = stream.writable.getWriter();
|
||||
|
||||
// SSE heartbeat
|
||||
interval = setInterval(() => {
|
||||
try {
|
||||
controller.enqueue(encoder.encode(`: ping ${Date.now()}\n\n`));
|
||||
} catch (e) {
|
||||
if (interval) clearInterval(interval);
|
||||
}
|
||||
}, 10000);
|
||||
|
||||
console.log(`[SSE] koneksi session ${params.sessionId} dibuka`);
|
||||
},
|
||||
cancel() {
|
||||
if (interval) clearInterval(interval);
|
||||
console.log(`[SSE] koneksi session ${params.sessionId} ditutup`);
|
||||
},
|
||||
});
|
||||
|
||||
return new Response(stream, {
|
||||
status: 200,
|
||||
headers: {
|
||||
"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;
|
||||
|
||||
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#",
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
};
|
||||
const client: Client = {
|
||||
id: uuidv4(),
|
||||
send: (data) => writer.write(new TextEncoder().encode(data + "\n")),
|
||||
close: () => {
|
||||
writer.close();
|
||||
const set = sessions.get(sessionId);
|
||||
if (set) {
|
||||
set.delete(client);
|
||||
if (set.size === 0) sessions.delete(sessionId);
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
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 },
|
||||
};
|
||||
}
|
||||
}
|
||||
if (!sessions.has(sessionId)) sessions.set(sessionId, new Set());
|
||||
sessions.get(sessionId)!.add(client);
|
||||
|
||||
return {
|
||||
jsonrpc: "2.0",
|
||||
id,
|
||||
error: {
|
||||
code: -32601,
|
||||
message: `Method ${method} tidak dikenali`,
|
||||
},
|
||||
};
|
||||
});
|
||||
// Send "connected" event
|
||||
client.send(formatSSE("connected", { sessionId, id: client.id }));
|
||||
|
||||
// Keepalive ping
|
||||
const ping = setInterval(() => {
|
||||
client.send(formatSSE("ping", { ts: Date.now() }));
|
||||
}, PING_INTERVAL_MS);
|
||||
|
||||
const readable = stream.readable;
|
||||
const abort = new AbortController();
|
||||
|
||||
abort.signal.addEventListener("abort", () => {
|
||||
clearInterval(ping);
|
||||
client.close();
|
||||
});
|
||||
|
||||
return new Response(readable, {
|
||||
headers: set.headers as HeadersInit,
|
||||
status: 200,
|
||||
});
|
||||
})
|
||||
.get("/mcp/:sessionId/status", ({ params, set }) => {
|
||||
set.headers["Access-Control-Allow-Origin"] = "*";
|
||||
const clients = sessions.get(params.sessionId);
|
||||
return {
|
||||
sessionId: params.sessionId,
|
||||
connected: clients?.size ?? 0,
|
||||
};
|
||||
})
|
||||
.post("/mcp/:sessionId", async ({ params, request, set }) => {
|
||||
set.headers["Access-Control-Allow-Origin"] = "*";
|
||||
if (!isAuthorized(request.headers)) {
|
||||
return new Response("Unauthorized", { status: 401 });
|
||||
}
|
||||
|
||||
const body = await request.json();
|
||||
const event = body.event ?? "message";
|
||||
const data = body.data ?? body;
|
||||
|
||||
const sentTo = broadcast(params.sessionId, event, data);
|
||||
return { ok: true, sentTo };
|
||||
})
|
||||
.delete("/mcp/:sessionId", ({ params, request, set }) => {
|
||||
set.headers["Access-Control-Allow-Origin"] = "*";
|
||||
if (!isAuthorized(request.headers)) {
|
||||
return new Response("Unauthorized", { status: 401 });
|
||||
}
|
||||
|
||||
const clients = sessions.get(params.sessionId);
|
||||
if (clients) {
|
||||
for (const c of clients) c.close();
|
||||
sessions.delete(params.sessionId);
|
||||
}
|
||||
return { ok: true };
|
||||
})
|
||||
.options("/mcp/:sessionId", ({ set }) => {
|
||||
set.headers["Access-Control-Allow-Origin"] = "*";
|
||||
set.headers["Access-Control-Allow-Methods"] = "GET,POST,DELETE,OPTIONS";
|
||||
set.headers["Access-Control-Allow-Headers"] = "Content-Type,X-API-Key";
|
||||
return new Response(null, { status: 204 });
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user