tambahan
This commit is contained in:
@@ -1,57 +1,45 @@
|
||||
import { Elysia } from "elysia";
|
||||
|
||||
export const MCPRoute = new Elysia()
|
||||
// handshake awal (optional)
|
||||
.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 }) => {
|
||||
.get("/mcp/:sessionId", async ({ params, set }) => {
|
||||
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"] = "*";
|
||||
|
||||
const encoder = new TextEncoder();
|
||||
|
||||
const send = (obj: any) => encoder.encode(`data: ${JSON.stringify(obj)}\n\n`);
|
||||
|
||||
const stream = new ReadableStream({
|
||||
start(controller) {
|
||||
// kirim pesan awal
|
||||
controller.enqueue(
|
||||
send({
|
||||
jsonrpc: "2.0",
|
||||
id: null,
|
||||
result: {
|
||||
protocol: "2024-11-05",
|
||||
capabilities: {
|
||||
"tools/list": true,
|
||||
"tools/call": true,
|
||||
},
|
||||
status: "MCP Server Ready",
|
||||
// kirim pesan handshake awal
|
||||
const initial = {
|
||||
jsonrpc: "2.0",
|
||||
id: null,
|
||||
result: {
|
||||
protocol: "2024-11-05",
|
||||
capabilities: {
|
||||
"tools/list": true,
|
||||
"tools/call": true,
|
||||
},
|
||||
})
|
||||
);
|
||||
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;
|
||||
})
|
||||
|
||||
// handler untuk JSONRPC POST
|
||||
.post("/mcp", async ({ body, set }) => {
|
||||
set.headers["Content-Type"] = "application/json; charset=utf-8";
|
||||
const { id, method, params } = body as any;
|
||||
@@ -98,10 +86,7 @@ export const MCPRoute = new Elysia()
|
||||
return {
|
||||
jsonrpc: "2.0",
|
||||
id,
|
||||
result: {
|
||||
message: "Berhasil menerima pengajuan KTP",
|
||||
data: args,
|
||||
},
|
||||
result: { message: "Berhasil menerima pengajuan KTP", data: args },
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user