tambahan
This commit is contained in:
@@ -1,103 +1,262 @@
|
|||||||
// src/routes/mcp_route.ts
|
import { Elysia, t } from "elysia";
|
||||||
import { Elysia } from "elysia";
|
import { cors } from '@elysiajs/cors';
|
||||||
|
|
||||||
export const MCPRoute = new Elysia({
|
export const MCPRoute = new Elysia({
|
||||||
prefix: "/mcp-server",
|
prefix: "/mcp-server",
|
||||||
|
tags: ["mcp-server"],
|
||||||
})
|
})
|
||||||
.get("/mcp", ({ set, query }) => {
|
.use(cors())
|
||||||
// n8n akan mengirim method & params lewat query seperti:
|
|
||||||
// ?method=tools/list atau ?method=tools/sayHello&name=Malik
|
|
||||||
|
|
||||||
const id = query.id ?? 1;
|
// ✅ Health check endpoint
|
||||||
const method = query.method as string;
|
.get("/", () => ({
|
||||||
const params = query;
|
status: "ok",
|
||||||
|
message: "MCP Server is running",
|
||||||
|
protocol: "2024-11-05",
|
||||||
|
transport: "sse",
|
||||||
|
endpoints: {
|
||||||
|
sse: "/mcp-server/sse",
|
||||||
|
message: "/mcp-server/message"
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
|
||||||
// ✅ Header wajib agar n8n mau streaming
|
// ✅ SSE endpoint - Server-Sent Events untuk n8n
|
||||||
set.headers["Content-Type"] = "application/json; charset=utf-8";
|
.get("/sse", ({ set }) => {
|
||||||
set.headers["Cache-Control"] = "no-cache, no-transform";
|
set.headers = {
|
||||||
set.headers["Connection"] = "keep-alive";
|
"Content-Type": "text/event-stream",
|
||||||
set.headers["Transfer-Encoding"] = "chunked";
|
"Cache-Control": "no-cache, no-transform",
|
||||||
set.headers["X-Accel-Buffering"] = "no"; // Untuk Cloudflare & Nginx
|
"Connection": "keep-alive",
|
||||||
|
"X-Accel-Buffering": "no"
|
||||||
|
};
|
||||||
|
|
||||||
const stream = new ReadableStream({
|
const encoder = new TextEncoder();
|
||||||
async start(controller) {
|
let isClosed = false;
|
||||||
// --- Handle mcp/version ---
|
|
||||||
if (method === "mcp/version") {
|
const stream = new ReadableStream({
|
||||||
controller.enqueue(
|
start(controller) {
|
||||||
JSON.stringify({
|
// Send endpoint info immediately
|
||||||
jsonrpc: "2.0",
|
const endpointMsg = {
|
||||||
id,
|
type: "endpoint",
|
||||||
result: {
|
endpoint: "/mcp-server/message"
|
||||||
protocol: "2024-11-05",
|
};
|
||||||
capabilities: {
|
|
||||||
"tools/list": true,
|
try {
|
||||||
"tools/call": true,
|
controller.enqueue(
|
||||||
},
|
encoder.encode(`data: ${JSON.stringify(endpointMsg)}\n\n`)
|
||||||
},
|
);
|
||||||
}) + "\n"
|
} catch (e) {
|
||||||
);
|
console.error("Failed to send endpoint message:", e);
|
||||||
return; // Tidak ditutup → tetap open
|
}
|
||||||
}
|
|
||||||
|
// Keep connection alive with heartbeat
|
||||||
|
const heartbeat = setInterval(() => {
|
||||||
|
if (isClosed) {
|
||||||
|
clearInterval(heartbeat);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
controller.enqueue(encoder.encode(`: heartbeat\n\n`));
|
||||||
|
} catch (e) {
|
||||||
|
console.error("Heartbeat failed:", e);
|
||||||
|
clearInterval(heartbeat);
|
||||||
|
isClosed = true;
|
||||||
|
try {
|
||||||
|
controller.close();
|
||||||
|
} catch (closeErr) {
|
||||||
|
// Already closed
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, 15000); // Every 15 seconds
|
||||||
|
|
||||||
|
// Cleanup on connection close
|
||||||
|
return () => {
|
||||||
|
isClosed = true;
|
||||||
|
clearInterval(heartbeat);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return new Response(stream);
|
||||||
|
})
|
||||||
|
|
||||||
// --- Handle tools/list ---
|
// ✅ Message endpoint - untuk JSON-RPC messages
|
||||||
if (method === "tools/list") {
|
.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,
|
|
||||||
result: [
|
try {
|
||||||
{
|
// Initialize
|
||||||
name: "sayHello",
|
if (method === "initialize") {
|
||||||
description: "Greets a user",
|
return {
|
||||||
inputSchema: {
|
jsonrpc: "2.0",
|
||||||
type: "object",
|
id,
|
||||||
properties: {
|
result: {
|
||||||
name: { type: "string", description: "Your name" },
|
protocolVersion: "2024-11-05",
|
||||||
},
|
capabilities: {
|
||||||
},
|
tools: {}
|
||||||
},
|
},
|
||||||
],
|
serverInfo: {
|
||||||
}) + "\n"
|
name: "tentang-darmasaba-mcp",
|
||||||
);
|
version: "1.0.0"
|
||||||
return;
|
}
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
// --- Handle tools/sayHello (progress streaming) ---
|
// Ping
|
||||||
if (method === "tools/sayHello") {
|
if (method === "ping") {
|
||||||
controller.enqueue(
|
return {
|
||||||
JSON.stringify({
|
jsonrpc: "2.0",
|
||||||
jsonrpc: "2.0",
|
id,
|
||||||
id,
|
result: {}
|
||||||
result: { status: "Processing..." },
|
};
|
||||||
}) + "\n"
|
}
|
||||||
);
|
|
||||||
await Bun.sleep(500);
|
|
||||||
|
|
||||||
controller.enqueue(
|
// List tools
|
||||||
JSON.stringify({
|
if (method === "tools/list") {
|
||||||
jsonrpc: "2.0",
|
return {
|
||||||
id,
|
jsonrpc: "2.0",
|
||||||
result: { message: `Hello ${params?.name || "User"}` },
|
id,
|
||||||
}) + "\n"
|
result: {
|
||||||
);
|
tools: [
|
||||||
return;
|
{
|
||||||
}
|
name: "sayHello",
|
||||||
|
description: "Greets user with a personalized message",
|
||||||
// --- Jika method tidak dikenal ---
|
inputSchema: {
|
||||||
controller.enqueue(
|
type: "object",
|
||||||
JSON.stringify({
|
properties: {
|
||||||
jsonrpc: "2.0",
|
name: {
|
||||||
id,
|
type: "string",
|
||||||
error: {
|
description: "Name of the person to greet"
|
||||||
code: -32601,
|
}
|
||||||
message: `Method "${method}" not found`,
|
},
|
||||||
},
|
required: ["name"]
|
||||||
}) + "\n"
|
}
|
||||||
);
|
|
||||||
},
|
},
|
||||||
});
|
{
|
||||||
|
name: "getCurrentTime",
|
||||||
|
description: "Returns current server time",
|
||||||
|
inputSchema: {
|
||||||
|
type: "object",
|
||||||
|
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"]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
return new Response(stream);
|
// Call tool
|
||||||
});
|
if (method === "tools/call") {
|
||||||
|
const toolName = params?.name;
|
||||||
|
const args = params?.arguments || {};
|
||||||
|
|
||||||
export default MCPRoute;
|
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()]),
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
|
export default MCPRoute;
|
||||||
Reference in New Issue
Block a user