tambahan
This commit is contained in:
@@ -1,67 +1,72 @@
|
|||||||
import Elysia from "elysia";
|
import { Elysia } from "elysia";
|
||||||
import { t } from "elysia";
|
import { t } from "elysia";
|
||||||
|
|
||||||
export const MCPRoute = new Elysia({
|
export const MCPRoute = new Elysia({ prefix: "/mcp-server" })
|
||||||
prefix: "/mcp-server",
|
.post("/mcp", async ({ body, set }) => {
|
||||||
tags: ["mcp-server"],
|
const { id, method, params } = body;
|
||||||
})
|
set.headers['Content-Type'] = 'application/json';
|
||||||
.post("/mcp", ({ body }) => {
|
set.headers['Transfer-Encoding'] = 'chunked'; // ✅ Streaming-required for n8n
|
||||||
const { id, method, params } = body;
|
|
||||||
|
|
||||||
// ==== TOOLS EXECUTION ====
|
// ---- STREAMING RESPONSE CONSTRUCTION ----
|
||||||
|
const stream = new ReadableStream({
|
||||||
|
async start(controller) {
|
||||||
|
// Jika tools/sayHello → kirim stream
|
||||||
if (method === "tools/sayHello") {
|
if (method === "tools/sayHello") {
|
||||||
|
controller.enqueue(JSON.stringify({
|
||||||
return {
|
|
||||||
jsonrpc: "2.0",
|
|
||||||
id,
|
|
||||||
result: {
|
|
||||||
message: `Hello from MCP Server`,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
// ==== LIST ALL TOOLS ====
|
|
||||||
if (method === "tools/list") {
|
|
||||||
return {
|
|
||||||
jsonrpc: "2.0",
|
|
||||||
id,
|
|
||||||
result: [
|
|
||||||
{
|
|
||||||
name: "sayHello",
|
|
||||||
description: "Greets a user with a name",
|
|
||||||
inputSchema: {
|
|
||||||
type: "object",
|
|
||||||
properties: {
|
|
||||||
name: { type: "string" },
|
|
||||||
},
|
|
||||||
required: ["name"],
|
|
||||||
},
|
|
||||||
},
|
|
||||||
],
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
// ==== IF METHOD NOT FOUND ====
|
|
||||||
return {
|
|
||||||
jsonrpc: "2.0",
|
jsonrpc: "2.0",
|
||||||
id,
|
id,
|
||||||
error: {
|
result: { message: "Processing..." }
|
||||||
code: -32601,
|
}) + "\n"); // kirim chunk pertama
|
||||||
message: `Method '${method}' not found`,
|
|
||||||
},
|
await Bun.sleep(500); // contoh delay
|
||||||
};
|
controller.enqueue(JSON.stringify({
|
||||||
}, {
|
jsonrpc: "2.0",
|
||||||
// ✅ Body JSON-RPC 2.0 schema (params & id bisa optional)
|
id,
|
||||||
body: t.Object({
|
result: { message: `Hello ` }
|
||||||
jsonrpc: t.Optional(t.String()),
|
}) + "\n");
|
||||||
method: t.String(),
|
|
||||||
params: t.Optional(t.Record(t.String(), t.Any())),
|
controller.close();
|
||||||
id: t.Optional(t.Union([t.String(), t.Number()])),
|
}
|
||||||
}),
|
|
||||||
detail: {
|
// Jika tools/list → kirim langsung tapi tetap stream
|
||||||
summary: "MCP Server Endpoint",
|
else if (method === "tools/list") {
|
||||||
description: "Handle MCP JSON-RPC requests for tools and discovery.",
|
controller.enqueue(JSON.stringify({
|
||||||
},
|
jsonrpc: "2.0",
|
||||||
|
id,
|
||||||
|
result: [
|
||||||
|
{
|
||||||
|
name: "sayHello",
|
||||||
|
description: "Greets user",
|
||||||
|
inputSchema: {
|
||||||
|
type: "object",
|
||||||
|
properties: { name: { type: "string" } },
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}));
|
||||||
|
controller.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Jika tidak ada method
|
||||||
|
else {
|
||||||
|
controller.enqueue(JSON.stringify({
|
||||||
|
jsonrpc: "2.0",
|
||||||
|
id,
|
||||||
|
error: { code: -32601, message: `Method ${method} not found` }
|
||||||
|
}));
|
||||||
|
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()])),
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
export default MCPRoute;
|
export default MCPRoute;
|
||||||
|
|||||||
Reference in New Issue
Block a user