This commit is contained in:
bipproduction
2025-10-27 01:27:21 +08:00
parent 65e4cfd137
commit 02a66882de

View File

@@ -1,262 +1,190 @@
// src/routes/mcp_route.ts
import { Elysia, t } from "elysia"; import { Elysia, t } from "elysia";
import { cors } from '@elysiajs/cors';
export const MCPRoute = new Elysia({ export const MCPRoute = new Elysia({
prefix: "/mcp-server", prefix: "/mcp",
tags: ["mcp-server"],
}) })
.use(cors()) .get("/", ({ set, query }) => {
const id = query.id ?? 1;
const method = query.method as string;
const params = query;
// Health check endpoint // Header untuk SSE / Streaming agar diterima oleh n8n
.get("/", () => ({ set.headers["Content-Type"] = "application/json; charset=utf-8";
status: "ok", set.headers["Cache-Control"] = "no-cache, no-transform";
message: "MCP Server is running", set.headers["Connection"] = "keep-alive";
protocol: "2024-11-05", set.headers["Transfer-Encoding"] = "chunked";
transport: "sse", set.headers["X-Accel-Buffering"] = "no"; // Matikan buffering (nginx/cloudflare)
endpoints: {
sse: "/mcp-server/sse",
message: "/mcp-server/message"
}
}))
// ✅ SSE endpoint - Server-Sent Events untuk n8n const stream = new ReadableStream({
.get("/sse", ({ set }) => { async start(controller) {
set.headers = { // ✅ Heartbeat agar tidak time-out di Cloudflare / n8n
"Content-Type": "text/event-stream", controller.enqueue(":\n\n");
"Cache-Control": "no-cache, no-transform",
"Connection": "keep-alive",
"X-Accel-Buffering": "no"
};
const encoder = new TextEncoder(); // 1. Handshake MCP Version
let isClosed = false; if (method === "mcp/version") {
controller.enqueue(
const stream = new ReadableStream({ JSON.stringify({
start(controller) { jsonrpc: "2.0",
// Send endpoint info immediately id,
const endpointMsg = { result: {
type: "endpoint", protocol: "2024-11-05",
endpoint: "/mcp-server/message" capabilities: {
}; "tools/list": true,
"tools/call": true,
try { },
controller.enqueue( },
encoder.encode(`data: ${JSON.stringify(endpointMsg)}\n\n`) }) + "\n"
); );
} catch (e) { return; // ❗ biarkan stream tetap terbuka
console.error("Failed to send endpoint message:", e); }
}
// 2. tools/list → Informasi tools ke n8n
// Keep connection alive with heartbeat if (method === "tools/list") {
const heartbeat = setInterval(() => { controller.enqueue(
if (isClosed) { JSON.stringify({
clearInterval(heartbeat); jsonrpc: "2.0",
id,
result: [
{
name: "sayHello",
description: "Greets a user",
inputSchema: {
type: "object",
properties: {
name: { type: "string", description: "Your name" },
},
required: ["name"],
},
},
],
}) + "\n"
);
return; return;
} }
try { // 3. tools/sayHello → kirim progres lalu hasil
controller.enqueue(encoder.encode(`: heartbeat\n\n`)); if (method === "tools/sayHello") {
} catch (e) { controller.enqueue(
console.error("Heartbeat failed:", e); JSON.stringify({
clearInterval(heartbeat); jsonrpc: "2.0",
isClosed = true; id,
try { result: { status: "Processing..." },
controller.close(); }) + "\n"
} catch (closeErr) { );
// Already closed await Bun.sleep(500);
}
controller.enqueue(
JSON.stringify({
jsonrpc: "2.0",
id,
result: { message: `Hello ${params?.name || "User"}` },
}) + "\n"
);
return;
} }
}, 15000); // Every 15 seconds
// Cleanup on connection close
return () => {
isClosed = true;
clearInterval(heartbeat);
};
}
});
return new Response(stream);
})
// ✅ Message endpoint - untuk JSON-RPC messages // 4. Unknown method
.post("/message", async ({ body, set }) => { controller.enqueue(
set.headers["Content-Type"] = "application/json"; JSON.stringify({
jsonrpc: "2.0",
const { jsonrpc, id, method, params } = body as any; id,
error: {
try { code: -32601,
// Initialize message: `Method "${method}" not found`,
if (method === "initialize") {
return {
jsonrpc: "2.0",
id,
result: {
protocolVersion: "2024-11-05",
capabilities: {
tools: {}
},
serverInfo: {
name: "tentang-darmasaba-mcp",
version: "1.0.0"
}
}
};
}
// Ping
if (method === "ping") {
return {
jsonrpc: "2.0",
id,
result: {}
};
}
// List tools
if (method === "tools/list") {
return {
jsonrpc: "2.0",
id,
result: {
tools: [
{
name: "sayHello",
description: "Greets user with a personalized message",
inputSchema: {
type: "object",
properties: {
name: {
type: "string",
description: "Name of the person to greet"
}
},
required: ["name"]
}
}, },
{ }) + "\n"
name: "getCurrentTime", );
description: "Returns current server time", },
inputSchema: { cancel() {
type: "object", console.log("🔴 Stream closed by client");
properties: {} },
} });
},
{
name: "getTentangDarmasaba",
description: "Get information about Tentang Darmasaba platform",
inputSchema: {
type: "object",
properties: {
section: {
type: "string",
description: "Specific section to get info about",
enum: ["about", "features", "contact"]
}
}
}
}
]
}
};
}
// Call tool return new Response(stream);
if (method === "tools/call") {
const toolName = params?.name;
const args = params?.arguments || {};
if (toolName === "sayHello") {
return {
jsonrpc: "2.0",
id,
result: {
content: [
{
type: "text",
text: `Hello ${args.name || "there"}! 👋 Welcome to Tentang Darmasaba MCP Server!`
}
]
}
};
}
if (toolName === "getCurrentTime") {
return {
jsonrpc: "2.0",
id,
result: {
content: [
{
type: "text",
text: `Current server time: ${new Date().toISOString()}`
}
]
}
};
}
if (toolName === "getTentangDarmasaba") {
const section = args.section || "about";
const info: Record<string, string> = {
about: "Tentang Darmasaba adalah platform pembelajaran dan kolaborasi.",
features: "Fitur: MCP Server integration, Real-time collaboration, API tools",
contact: "Contact: support@tentangdarmasaba.com"
};
return {
jsonrpc: "2.0",
id,
result: {
content: [
{
type: "text",
text: info[section] || info.about
}
]
}
};
}
// Tool not found
return {
jsonrpc: "2.0",
id,
error: {
code: -32602,
message: `Unknown tool: ${toolName}`
}
};
}
// Method not found
return {
jsonrpc: "2.0",
id,
error: {
code: -32601,
message: `Unknown method: ${method}`
}
};
} catch (error: any) {
console.error("MCP Error:", error);
return {
jsonrpc: "2.0",
id,
error: {
code: -32603,
message: `Internal error: ${error.message}`
}
};
}
}, {
body: t.Object({
jsonrpc: t.String(),
method: t.String(),
params: t.Optional(t.Any()),
id: t.Union([t.String(), t.Number()]),
}) })
}); // Optional: juga izinkan MCP pakai POST request JSON-RPC
.post("/", async ({ body, set }) => {
const { id, method, params } = body as any;
export default MCPRoute; set.headers["Content-Type"] = "application/json; charset=utf-8";
set.headers["Connection"] = "keep-alive";
set.headers["Transfer-Encoding"] = "chunked";
set.headers["X-Accel-Buffering"] = "no";
const stream = new ReadableStream({
async start(controller) {
controller.enqueue(":\n\n");
if (method === "mcp/version") {
controller.enqueue(
JSON.stringify({
jsonrpc: "2.0",
id,
result: {
protocol: "2024-11-05",
capabilities: {
"tools/list": true,
"tools/call": true,
},
},
}) + "\n"
);
return;
}
if (method === "tools/list") {
controller.enqueue(
JSON.stringify({
jsonrpc: "2.0",
id,
result: [
{
name: "sayHello",
description: "Greets a user",
inputSchema: {
type: "object",
properties: {
name: { type: "string" },
},
},
},
],
}) + "\n"
);
return;
}
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 || ""}` },
}) + "\n"
);
return;
}
},
});
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;