tambahan
This commit is contained in:
@@ -1,91 +1,150 @@
|
||||
import { Elysia, t } from "elysia";
|
||||
|
||||
export const MCPRoute = new Elysia({
|
||||
prefix: "/mcp-server",
|
||||
tags: ["mcp-server"],
|
||||
prefix: "/mcp-server",
|
||||
tags: ["mcp-server"],
|
||||
})
|
||||
|
||||
// ✅ 1. GET untuk handshake n8n (wajib)
|
||||
.get("/mcp", ({ set }) => {
|
||||
set.headers["Content-Type"] = "application/json";
|
||||
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({
|
||||
// ✅ 1. GET untuk handshake n8n - Mengembalikan protocol info
|
||||
.get("/mcp", ({ set }) => {
|
||||
set.headers["Content-Type"] = "application/json";
|
||||
return {
|
||||
jsonrpc: "2.0",
|
||||
id,
|
||||
result: [
|
||||
{
|
||||
name: "sayHello",
|
||||
description: "Greets user",
|
||||
inputSchema: {
|
||||
type: "object",
|
||||
properties: { name: { type: "string" } }
|
||||
result: {
|
||||
protocolVersion: "2024-11-05",
|
||||
capabilities: {
|
||||
tools: {},
|
||||
resources: {}
|
||||
},
|
||||
serverInfo: {
|
||||
name: "tentang-darmasaba-mcp",
|
||||
version: "1.0.0"
|
||||
}
|
||||
}
|
||||
]
|
||||
}) + "\n"
|
||||
);
|
||||
controller.close();
|
||||
return;
|
||||
}
|
||||
}
|
||||
};
|
||||
})
|
||||
|
||||
if (method === "tools/call" && params?.name === "sayHello") {
|
||||
controller.enqueue(
|
||||
JSON.stringify({ jsonrpc: "2.0", id, result: { status: "Processing..." } }) + "\n"
|
||||
);
|
||||
await Bun.sleep(300);
|
||||
controller.enqueue(
|
||||
JSON.stringify({
|
||||
// ✅ 2. POST untuk komunikasi JSON-RPC (non-streaming untuk kompatibilitas)
|
||||
.post("/mcp", async ({ body, set }) => {
|
||||
const { id, method, params } = body as any;
|
||||
|
||||
set.headers["Content-Type"] = "application/json";
|
||||
|
||||
// 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",
|
||||
id,
|
||||
result: { message: `Hello ${params?.arguments?.name || "User"}!` }
|
||||
}) + "\n"
|
||||
);
|
||||
controller.close();
|
||||
return;
|
||||
}
|
||||
error: {
|
||||
code: -32601,
|
||||
message: `Method '${method}' not found`
|
||||
}
|
||||
};
|
||||
}, {
|
||||
body: t.Object({
|
||||
jsonrpc: t.String(),
|
||||
method: t.String(),
|
||||
params: t.Optional(t.Any()),
|
||||
id: t.Union([t.String(), t.Number()]),
|
||||
}),
|
||||
});
|
||||
|
||||
controller.enqueue(
|
||||
JSON.stringify({
|
||||
jsonrpc: "2.0",
|
||||
id,
|
||||
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;
|
||||
Reference in New Issue
Block a user