tambahan
This commit is contained in:
@@ -1,91 +1,150 @@
|
|||||||
import { Elysia, t } from "elysia";
|
import { Elysia, t } from "elysia";
|
||||||
|
|
||||||
export const MCPRoute = new Elysia({
|
export const MCPRoute = new Elysia({
|
||||||
prefix: "/mcp-server",
|
prefix: "/mcp-server",
|
||||||
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",
|
|
||||||
result: {
|
|
||||||
protocol: "2024-11-05",
|
|
||||||
capabilities: {
|
|
||||||
"tools/list": true,
|
|
||||||
"tools/call": true,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
})
|
|
||||||
|
|
||||||
|
|
||||||
// ✅ 2. POST untuk komunikasi streaming (tools/list, tools/call)
|
|
||||||
.post("/mcp", ({ body, set }) => {
|
|
||||||
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({
|
|
||||||
async start(controller) {
|
|
||||||
if (method === "tools/list") {
|
|
||||||
controller.enqueue(
|
|
||||||
JSON.stringify({
|
|
||||||
jsonrpc: "2.0",
|
jsonrpc: "2.0",
|
||||||
id,
|
result: {
|
||||||
result: [
|
protocolVersion: "2024-11-05",
|
||||||
{
|
capabilities: {
|
||||||
name: "sayHello",
|
tools: {},
|
||||||
description: "Greets user",
|
resources: {}
|
||||||
inputSchema: {
|
},
|
||||||
type: "object",
|
serverInfo: {
|
||||||
properties: { name: { type: "string" } }
|
name: "tentang-darmasaba-mcp",
|
||||||
|
version: "1.0.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]
|
};
|
||||||
}) + "\n"
|
})
|
||||||
);
|
|
||||||
controller.close();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (method === "tools/call" && params?.name === "sayHello") {
|
// ✅ 2. POST untuk komunikasi JSON-RPC (non-streaming untuk kompatibilitas)
|
||||||
controller.enqueue(
|
.post("/mcp", async ({ body, set }) => {
|
||||||
JSON.stringify({ jsonrpc: "2.0", id, result: { status: "Processing..." } }) + "\n"
|
const { id, method, params } = body as any;
|
||||||
);
|
|
||||||
await Bun.sleep(300);
|
set.headers["Content-Type"] = "application/json";
|
||||||
controller.enqueue(
|
|
||||||
JSON.stringify({
|
// Initialize response
|
||||||
|
if (method === "initialize") {
|
||||||
|
return {
|
||||||
|
jsonrpc: "2.0",
|
||||||
|
id,
|
||||||
|
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",
|
||||||
|
description: "Greets user with a friendly message",
|
||||||
|
inputSchema: {
|
||||||
|
type: "object",
|
||||||
|
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: {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// Call tool
|
||||||
|
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 || "User"}! Welcome to Tentang Darmasaba MCP Server! 👋`
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (toolName === "getTentangDarmasaba") {
|
||||||
|
return {
|
||||||
|
jsonrpc: "2.0",
|
||||||
|
id,
|
||||||
|
result: {
|
||||||
|
content: [
|
||||||
|
{
|
||||||
|
type: "text",
|
||||||
|
text: "Tentang Darmasaba adalah platform untuk belajar tentang Darmasaba. Server MCP ini menyediakan tools untuk berinteraksi dengan sistem."
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// Tool not found
|
||||||
|
return {
|
||||||
|
jsonrpc: "2.0",
|
||||||
|
id,
|
||||||
|
error: {
|
||||||
|
code: -32602,
|
||||||
|
message: `Tool '${toolName}' not found`
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// Method not found
|
||||||
|
return {
|
||||||
jsonrpc: "2.0",
|
jsonrpc: "2.0",
|
||||||
id,
|
id,
|
||||||
result: { message: `Hello ${params?.arguments?.name || "User"}!` }
|
error: {
|
||||||
}) + "\n"
|
code: -32601,
|
||||||
);
|
message: `Method '${method}' not found`
|
||||||
controller.close();
|
}
|
||||||
return;
|
};
|
||||||
}
|
}, {
|
||||||
|
body: t.Object({
|
||||||
controller.enqueue(
|
jsonrpc: t.String(),
|
||||||
JSON.stringify({
|
method: t.String(),
|
||||||
jsonrpc: "2.0",
|
params: t.Optional(t.Any()),
|
||||||
id,
|
id: t.Union([t.String(), t.Number()]),
|
||||||
error: { code: -32601, message: `Method ${method} not found` }
|
}),
|
||||||
}) + "\n"
|
});
|
||||||
);
|
|
||||||
controller.close();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
return new Response(stream);
|
|
||||||
}, {
|
|
||||||
body: t.Object({
|
|
||||||
jsonrpc: t.Optional(t.String()),
|
|
||||||
method: t.String(),
|
|
||||||
params: t.Optional(t.Any()),
|
|
||||||
id: t.Optional(t.Union([t.String(), t.Number()])),
|
|
||||||
}),
|
|
||||||
});
|
|
||||||
|
|
||||||
export default MCPRoute;
|
export default MCPRoute;
|
||||||
2
x.sh
2
x.sh
@@ -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"}'
|
||||||
|
|||||||
Reference in New Issue
Block a user