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