This commit is contained in:
bipproduction
2025-10-26 21:26:27 +08:00
parent a31f3136f7
commit 948bf14312
2 changed files with 96 additions and 88 deletions

View File

@@ -1,88 +1,106 @@
import { Elysia, t } from "elysia";
export const MCPRoute = new Elysia({
prefix: "/mcp-server",
tags: ["mcp-server"],
prefix: "/mcp-server",
tags: ["mcp-server"],
})
.post("/mcp", ({ body, set }) => {
const { id, method, params } = body as any;
.post("/mcp", ({ body, set }) => {
const { id, method, params } = body as any;
set.headers["Content-Type"] = "application/json; charset=utf-8";
set.headers["Transfer-Encoding"] = "chunked";
set.headers["Connection"] = "keep-alive";
set.headers["Content-Type"] = "application/json; charset=utf-8";
set.headers["Transfer-Encoding"] = "chunked";
set.headers["Connection"] = "keep-alive";
// ✅ Streaming Response
const stream = new ReadableStream({
async start(controller) {
// tools/list
if (method === "tools/list") {
controller.enqueue(
JSON.stringify({
jsonrpc: "2.0",
id,
result: [
{
name: "sayHello",
description: "Greets user",
inputSchema: {
type: "object",
properties: { name: { type: "string" } },
},
},
],
}) + "\n"
);
// ❌ Jangan tutup langsung, beri delay agar n8n sempat membaca
await Bun.sleep(200);
controller.close();
return;
}
// ✅ Streaming Response
const stream = new ReadableStream({
async start(controller) {
// tools/list
if (method === "tools/list") {
controller.enqueue(
JSON.stringify({
jsonrpc: "2.0",
id,
result: [
{
name: "sayHello",
description: "Greets user",
inputSchema: {
type: "object",
properties: { name: { type: "string" } },
},
},
],
}) + "\n"
);
// ❌ Jangan tutup langsung, beri delay agar n8n sempat membaca
await Bun.sleep(200);
controller.close();
return;
}
// tools/sayHello → streaming progress
if (method === "tools/sayHello") {
controller.enqueue(
JSON.stringify({
jsonrpc: "2.0",
id,
result: { status: "Processing..." },
}) + "\n"
);
await Bun.sleep(500);
// tools/sayHello → streaming progress
if (method === "tools/sayHello") {
controller.enqueue(
JSON.stringify({
jsonrpc: "2.0",
id,
result: { status: "Processing..." },
}) + "\n"
);
await Bun.sleep(500);
controller.enqueue(
JSON.stringify({
jsonrpc: "2.0",
id,
result: { message: `Hello ${params?.name || "User"}` },
}) + "\n"
);
await Bun.sleep(200);
controller.enqueue(
JSON.stringify({
jsonrpc: "2.0",
id,
result: { message: `Hello ${params?.name || "User"}` },
}) + "\n"
);
await Bun.sleep(200);
controller.close();
return;
}
controller.close();
return;
}
// Method tidak dikenal
controller.enqueue(
JSON.stringify({
jsonrpc: "2.0",
id,
error: { code: -32601, message: `Method ${method} not found` },
}) + "\n"
);
await Bun.sleep(200);
controller.close();
},
if (method === "mcp/version") {
controller.enqueue(
JSON.stringify({
jsonrpc: "2.0",
id,
result: {
protocol: "2024-11-05", // versi MCP baru
capabilities: {
"tools/list": true,
"tools/call": true,
},
},
}) + "\n"
);
controller.close();
return;
}
// Method tidak dikenal
controller.enqueue(
JSON.stringify({
jsonrpc: "2.0",
id,
error: { code: -32601, message: `Method ${method} not found` },
}) + "\n"
);
await Bun.sleep(200);
controller.close();
},
});
return new Response(stream);
}, {
body: t.Object({
jsonrpc: t.Optional(t.String()),
method: t.String(),
params: t.Optional(t.Record(t.String(), t.Any())),
id: t.Optional(t.Union([t.String(), t.Number()])),
}),
});
return new Response(stream);
}, {
body: t.Object({
jsonrpc: t.Optional(t.String()),
method: t.String(),
params: t.Optional(t.Record(t.String(), t.Any())),
id: t.Optional(t.Union([t.String(), t.Number()])),
}),
});
export default MCPRoute;