This commit is contained in:
bipproduction
2025-10-26 21:48:24 +08:00
parent a0434c3c32
commit 296f7b4429
2 changed files with 140 additions and 81 deletions

View File

@@ -5,87 +5,146 @@ export const MCPRoute = new Elysia({
tags: ["mcp-server"], tags: ["mcp-server"],
}) })
// ✅ 1. GET untuk handshake n8n (wajib) // ✅ 1. GET untuk handshake n8n - Mengembalikan protocol info
.get("/mcp", ({ set }) => { .get("/mcp", ({ set }) => {
set.headers["Content-Type"] = "application/json"; set.headers["Content-Type"] = "application/json";
return { return {
jsonrpc: "2.0", jsonrpc: "2.0",
result: { result: {
protocol: "2024-11-05", protocolVersion: "2024-11-05",
capabilities: { capabilities: {
"tools/list": true, tools: {},
"tools/call": true, resources: {}
},
serverInfo: {
name: "tentang-darmasaba-mcp",
version: "1.0.0"
} }
} }
}; };
}) })
// ✅ 2. POST untuk komunikasi JSON-RPC (non-streaming untuk kompatibilitas)
// ✅ 2. POST untuk komunikasi streaming (tools/list, tools/call) .post("/mcp", async ({ body, set }) => {
.post("/mcp", ({ body, set }) => {
const { id, method, params } = body as any; const { id, method, params } = body as any;
set.headers["Content-Type"] = "application/json; charset=utf-8";
set.headers["Transfer-Encoding"] = "chunked";
set.headers["Connection"] = "keep-alive";
const stream = new ReadableStream({ set.headers["Content-Type"] = "application/json";
async start(controller) {
if (method === "tools/list") { // Initialize response
controller.enqueue( if (method === "initialize") {
JSON.stringify({ return {
jsonrpc: "2.0", jsonrpc: "2.0",
id, id,
result: [ result: {
protocolVersion: "2024-11-05",
capabilities: {
tools: {},
resources: {}
},
serverInfo: {
name: "tentang-darmasaba-mcp",
version: "1.0.0"
}
}
};
}
// List tools
if (method === "tools/list") {
return {
jsonrpc: "2.0",
id,
result: {
tools: [
{ {
name: "sayHello", name: "sayHello",
description: "Greets user", description: "Greets user with a friendly message",
inputSchema: { inputSchema: {
type: "object", type: "object",
properties: { name: { type: "string" } } properties: {
name: {
type: "string",
description: "Name of the person to greet"
}
},
required: ["name"]
}
},
{
name: "getTentangDarmasaba",
description: "Get information about Tentang Darmasaba",
inputSchema: {
type: "object",
properties: {}
} }
} }
] ]
}) + "\n" }
); };
controller.close();
return;
} }
if (method === "tools/call" && params?.name === "sayHello") { // Call tool
controller.enqueue( if (method === "tools/call") {
JSON.stringify({ jsonrpc: "2.0", id, result: { status: "Processing..." } }) + "\n" const toolName = params?.name;
); const args = params?.arguments || {};
await Bun.sleep(300);
controller.enqueue( if (toolName === "sayHello") {
JSON.stringify({ return {
jsonrpc: "2.0", jsonrpc: "2.0",
id, id,
result: { message: `Hello ${params?.arguments?.name || "User"}!` } result: {
}) + "\n" content: [
); {
controller.close(); type: "text",
return; text: `Hello ${args.name || "User"}! Welcome to Tentang Darmasaba MCP Server! 👋`
}
]
}
};
} }
controller.enqueue( if (toolName === "getTentangDarmasaba") {
JSON.stringify({ return {
jsonrpc: "2.0", jsonrpc: "2.0",
id, id,
error: { code: -32601, message: `Method ${method} not found` } result: {
}) + "\n" content: [
); {
controller.close(); type: "text",
text: "Tentang Darmasaba adalah platform untuk belajar tentang Darmasaba. Server MCP ini menyediakan tools untuk berinteraksi dengan sistem."
}
]
}
};
} }
});
return new Response(stream); // Tool not found
}, { return {
jsonrpc: "2.0",
id,
error: {
code: -32602,
message: `Tool '${toolName}' not found`
}
};
}
// Method not found
return {
jsonrpc: "2.0",
id,
error: {
code: -32601,
message: `Method '${method}' not found`
}
};
}, {
body: t.Object({ body: t.Object({
jsonrpc: t.Optional(t.String()), jsonrpc: t.String(),
method: t.String(), method: t.String(),
params: t.Optional(t.Any()), params: t.Optional(t.Any()),
id: t.Optional(t.Union([t.String(), t.Number()])), id: t.Union([t.String(), t.Number()]),
}), }),
}); });
export default MCPRoute; export default MCPRoute;

2
x.sh
View File

@@ -1,3 +1,3 @@
curl -N -X POST https://cld-dkr-prod-jenna-mcp.wibudev.com/mcp-server/mcp \ curl -N -X GET https://cld-dkr-prod-jenna-mcp.wibudev.com/mcp-server/mcp \
-H "Content-Type: application/json" \ -H "Content-Type: application/json" \
-d '{"id":1,"method":"tools/list"}' -d '{"id":1,"method":"tools/list"}'