This commit is contained in:
bipproduction
2025-10-27 02:35:05 +08:00
parent 10162473f4
commit 3cc6f9f3ca

View File

@@ -1,58 +1,48 @@
// src/routes/mcp_route.ts import { Elysia } from "elysia";
import { Elysia, t } from "elysia";
const MCPRoute = new Elysia({ prefix: "/mcp" }) const MCPRoute = new Elysia({ prefix: "" }) // tanpa prefix agar langsung /mcp
.post("/", async ({ body, set }) => { .get("/mcp", ({ set }) => {
const { id = "123", method = "getTools" } = body; set.headers["Content-Type"] = "application/json; charset=utf-8";
return {
// Header wajib untuk MCP
set.status = 200;
set.headers["Content-Type"] = "application/json";
set.headers["Transfer-Encoding"] = "chunked";
set.headers["Connection"] = "keep-alive";
// ✔ Streaming response (langsung kirim chunk pertama)
const encoder = new TextEncoder();
const stream = new ReadableStream({
start(controller) {
if (method === "getTools") {
const firstChunk = JSON.stringify({
jsonrpc: "2.0", jsonrpc: "2.0",
id: id || "123", id: null,
result: { result: {
data: [ protocol: "2024-11-05",
{ capabilities: {
name: "Calculator", "tools/list": true,
value: "Calculator", "tools/call": true,
description: "Calculate math expression", },
inputSchema: { status: "MCP Server Ready",
type: "object", },
properties: { input: { type: "string" } }, };
required: ["input"], })
additionalProperties: true .post("/mcp", async ({ body, set }) => {
} set.headers["Content-Type"] = "application/json; charset=utf-8";
}
]
}
});
controller.enqueue(encoder.encode(firstChunk + "\n")); const { id, method, params } = body as { id: string; method: string; params: any };
controller.close(); // jika tidak mau streaming penuh, tutup
} else { if (method === "getTools") {
controller.enqueue(encoder.encode(JSON.stringify({ return {
jsonrpc: "2.0", jsonrpc: "2.0",
id: id || "123", id,
error: { code: -32601, message: "Method not found" } result: {
}))); tools: [
controller.close(); { name: "sayHello", description: "Returns a greeting" },
{ name: "getTime", description: "Current server time" },
],
},
};
} }
}
// Default jika method salah
return {
jsonrpc: "2.0",
id,
error: {
code: -32601,
message: `Method ${method} not found`,
},
};
}); });
return stream;
},{
body: t.Any()
});
export default MCPRoute; export default MCPRoute;