tambahan
This commit is contained in:
@@ -10,8 +10,9 @@ import { convertOpenApiToMcp } from "./server/lib/mcp-converter";
|
||||
import UserRoute from "./server/routes/user_route";
|
||||
import LayananRoute from "./server/routes/layanan_route";
|
||||
import AduanRoute from "./server/routes/aduan_route";
|
||||
import MCPRoute from "./server/routes/mcp_route";
|
||||
|
||||
import { cors } from "@elysiajs/cors";
|
||||
import { MCPRoute } from "./server/routes/mcp_route";
|
||||
|
||||
const Docs = new Elysia({
|
||||
tags: ["docs"],
|
||||
@@ -34,7 +35,6 @@ const Api = new Elysia({
|
||||
.use(AduanRoute);
|
||||
|
||||
const app = new Elysia()
|
||||
.use(cors())
|
||||
.use(Api)
|
||||
.use(Docs)
|
||||
.use(Auth)
|
||||
@@ -53,6 +53,11 @@ const app = new Elysia()
|
||||
)
|
||||
.use(MCPRoute)
|
||||
// .get("/*", html)
|
||||
.onRequest(({ set }) => {
|
||||
set.headers["Access-Control-Allow-Origin"] = "*";
|
||||
set.headers["Access-Control-Allow-Methods"] = "GET, POST, OPTIONS";
|
||||
set.headers["Access-Control-Allow-Headers"] = "Content-Type";
|
||||
})
|
||||
.listen(3000, () => {
|
||||
console.log("Server running at http://localhost:3000");
|
||||
});
|
||||
|
||||
@@ -1,70 +1,117 @@
|
||||
import { Elysia } from "elysia";
|
||||
|
||||
const MCPRoute = new Elysia()
|
||||
.get("/mcp", ({ set }) => {
|
||||
set.headers["Content-Type"] = "application/json; charset=utf-8";
|
||||
return {
|
||||
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 }) => {
|
||||
set.headers["Content-Type"] = "text/event-stream; charset=utf-8";
|
||||
set.headers["Cache-Control"] = "no-cache";
|
||||
set.headers["Connection"] = "keep-alive";
|
||||
|
||||
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",
|
||||
protocol: "2024-11-05",
|
||||
capabilities: {
|
||||
"tools/list": true,
|
||||
"tools/call": true,
|
||||
},
|
||||
status: "MCP Server Ready",
|
||||
},
|
||||
};
|
||||
})
|
||||
.post("/mcp", async ({ body, set }) => {
|
||||
set.headers["Content-Type"] = "application/json; charset=utf-8";
|
||||
})
|
||||
);
|
||||
},
|
||||
});
|
||||
|
||||
const { id, method, params } = body as any;
|
||||
return stream;
|
||||
})
|
||||
|
||||
if (method === "tools/list") {
|
||||
return {
|
||||
jsonrpc: "2.0",
|
||||
id,
|
||||
result: {
|
||||
tools: [
|
||||
{
|
||||
name: "pengajuan-pembuatan-ktp",
|
||||
description: "untuk melakukan pengajuan pembuatan ktp",
|
||||
inputSchema: {
|
||||
type: "object",
|
||||
properties: {
|
||||
JSON: { type: "object" },
|
||||
},
|
||||
required: ["JSON"],
|
||||
additionalProperties: true,
|
||||
},
|
||||
},
|
||||
// ... tambahkan tool lain
|
||||
],
|
||||
// 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;
|
||||
|
||||
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" },
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (method === "tools/call") {
|
||||
// contoh sederhana
|
||||
const { tool, arguments: args } = params;
|
||||
if (tool === "pengajuan-pembuatan-ktp") {
|
||||
return {
|
||||
jsonrpc: "2.0",
|
||||
id,
|
||||
result: { message: "Berhasil menerima pengajuan KTP", data: args },
|
||||
};
|
||||
}
|
||||
}
|
||||
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,
|
||||
error: {
|
||||
code: -32601,
|
||||
message: `Method ${method} tidak dikenali`,
|
||||
},
|
||||
jsonrpc: "2.0",
|
||||
id,
|
||||
result: {
|
||||
message: "Berhasil menerima pengajuan KTP",
|
||||
data: args,
|
||||
},
|
||||
};
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
export default MCPRoute;
|
||||
return {
|
||||
jsonrpc: "2.0",
|
||||
id,
|
||||
error: {
|
||||
code: -32601,
|
||||
message: `Method ${method} tidak dikenali`,
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user