tambahan
This commit is contained in:
@@ -1,58 +1,48 @@
|
||||
// src/routes/mcp_route.ts
|
||||
import { Elysia, t } from "elysia";
|
||||
import { Elysia } from "elysia";
|
||||
|
||||
const MCPRoute = new Elysia({ prefix: "/mcp" })
|
||||
.post("/", async ({ body, set }) => {
|
||||
const { id = "123", method = "getTools" } = body;
|
||||
|
||||
// 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({
|
||||
const MCPRoute = new Elysia({ prefix: "" }) // tanpa prefix agar langsung /mcp
|
||||
.get("/mcp", ({ set }) => {
|
||||
set.headers["Content-Type"] = "application/json; charset=utf-8";
|
||||
return {
|
||||
jsonrpc: "2.0",
|
||||
id: id || "123",
|
||||
id: null,
|
||||
result: {
|
||||
data: [
|
||||
{
|
||||
name: "Calculator",
|
||||
value: "Calculator",
|
||||
description: "Calculate math expression",
|
||||
inputSchema: {
|
||||
type: "object",
|
||||
properties: { input: { type: "string" } },
|
||||
required: ["input"],
|
||||
additionalProperties: true
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
});
|
||||
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";
|
||||
|
||||
controller.enqueue(encoder.encode(firstChunk + "\n"));
|
||||
controller.close(); // jika tidak mau streaming penuh, tutup
|
||||
} else {
|
||||
controller.enqueue(encoder.encode(JSON.stringify({
|
||||
jsonrpc: "2.0",
|
||||
id: id || "123",
|
||||
error: { code: -32601, message: "Method not found" }
|
||||
})));
|
||||
controller.close();
|
||||
const { id, method, params } = body as { id: string; method: string; params: any };
|
||||
|
||||
if (method === "getTools") {
|
||||
return {
|
||||
jsonrpc: "2.0",
|
||||
id,
|
||||
result: {
|
||||
tools: [
|
||||
{ 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;
|
||||
Reference in New Issue
Block a user