tambahan
This commit is contained in:
@@ -1,36 +1,22 @@
|
|||||||
import { Elysia } from "elysia";
|
import { Elysia, t } from "elysia";
|
||||||
import { t } from "elysia";
|
|
||||||
|
|
||||||
export const MCPRoute = new Elysia({ prefix: "/mcp-server", tags: ["mcp-server"] })
|
export const MCPRoute = new Elysia({
|
||||||
.all("/mcp", async ({ body, set }) => {
|
prefix: "/api/mcp-server", // ✅ Sesuaikan dengan endpoint n8n
|
||||||
const { id, method, params } = body;
|
tags: ["mcp-server"],
|
||||||
set.headers['Content-Type'] = 'application/json';
|
})
|
||||||
set.headers['Transfer-Encoding'] = 'chunked'; // ✅ Streaming-required for n8n
|
.post("/mcp", ({ body, set }) => {
|
||||||
|
const { id, method, params } = body as any;
|
||||||
|
|
||||||
|
set.headers["Content-Type"] = "application/json";
|
||||||
|
set.headers["Transfer-Encoding"] = "chunked";
|
||||||
|
set.headers["Connection"] = "keep-alive";
|
||||||
|
|
||||||
// ---- STREAMING RESPONSE CONSTRUCTION ----
|
|
||||||
const stream = new ReadableStream({
|
const stream = new ReadableStream({
|
||||||
async start(controller) {
|
async start(controller) {
|
||||||
// Jika tools/sayHello → kirim stream
|
// tools.list → kirim daftar tools
|
||||||
if (method === "tools/sayHello") {
|
if (method === "tools/list") {
|
||||||
controller.enqueue(JSON.stringify({
|
controller.enqueue(
|
||||||
jsonrpc: "2.0",
|
JSON.stringify({
|
||||||
id,
|
|
||||||
result: { message: "Processing..." }
|
|
||||||
}) + "\n"); // kirim chunk pertama
|
|
||||||
|
|
||||||
await Bun.sleep(500); // contoh delay
|
|
||||||
controller.enqueue(JSON.stringify({
|
|
||||||
jsonrpc: "2.0",
|
|
||||||
id,
|
|
||||||
result: { message: `Hello ` }
|
|
||||||
}) + "\n");
|
|
||||||
|
|
||||||
controller.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Jika tools/list → kirim langsung tapi tetap stream
|
|
||||||
else if (method === "tools/list") {
|
|
||||||
controller.enqueue(JSON.stringify({
|
|
||||||
jsonrpc: "2.0",
|
jsonrpc: "2.0",
|
||||||
id,
|
id,
|
||||||
result: [
|
result: [
|
||||||
@@ -40,23 +26,46 @@ export const MCPRoute = new Elysia({ prefix: "/mcp-server", tags: ["mcp-server"]
|
|||||||
inputSchema: {
|
inputSchema: {
|
||||||
type: "object",
|
type: "object",
|
||||||
properties: { name: { type: "string" } },
|
properties: { name: { type: "string" } },
|
||||||
}
|
},
|
||||||
}
|
},
|
||||||
]
|
],
|
||||||
}));
|
}) + "\n"
|
||||||
|
);
|
||||||
controller.close();
|
controller.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Jika tidak ada method
|
// tools.sayHello → streaming bertahap
|
||||||
else {
|
else if (method === "tools/sayHello") {
|
||||||
controller.enqueue(JSON.stringify({
|
controller.enqueue(
|
||||||
|
JSON.stringify({
|
||||||
jsonrpc: "2.0",
|
jsonrpc: "2.0",
|
||||||
id,
|
id,
|
||||||
error: { code: -32601, message: `Method ${method} not found` }
|
result: { message: "Processing..." },
|
||||||
}));
|
}) + "\n"
|
||||||
|
);
|
||||||
|
await Bun.sleep(500);
|
||||||
|
controller.enqueue(
|
||||||
|
JSON.stringify({
|
||||||
|
jsonrpc: "2.0",
|
||||||
|
id,
|
||||||
|
result: { message: `Hello ${params?.name || ""}` },
|
||||||
|
}) + "\n"
|
||||||
|
);
|
||||||
controller.close();
|
controller.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Method tidak ditemukan
|
||||||
|
else {
|
||||||
|
controller.enqueue(
|
||||||
|
JSON.stringify({
|
||||||
|
jsonrpc: "2.0",
|
||||||
|
id,
|
||||||
|
error: { code: -32601, message: `Method ${method} not found` },
|
||||||
|
}) + "\n"
|
||||||
|
);
|
||||||
|
controller.close();
|
||||||
}
|
}
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
return new Response(stream);
|
return new Response(stream);
|
||||||
@@ -66,7 +75,7 @@ export const MCPRoute = new Elysia({ prefix: "/mcp-server", tags: ["mcp-server"]
|
|||||||
method: t.String(),
|
method: t.String(),
|
||||||
params: t.Optional(t.Record(t.String(), t.Any())),
|
params: t.Optional(t.Record(t.String(), t.Any())),
|
||||||
id: t.Optional(t.Union([t.String(), t.Number()])),
|
id: t.Optional(t.Union([t.String(), t.Number()])),
|
||||||
})
|
}),
|
||||||
});
|
});
|
||||||
|
|
||||||
export default MCPRoute;
|
export default MCPRoute;
|
||||||
|
|||||||
Reference in New Issue
Block a user