tambahan
This commit is contained in:
@@ -4,187 +4,162 @@ import { Elysia, t } from "elysia";
|
|||||||
export const MCPRoute = new Elysia({
|
export const MCPRoute = new Elysia({
|
||||||
prefix: "/mcp",
|
prefix: "/mcp",
|
||||||
})
|
})
|
||||||
.get("/", ({ set, query }) => {
|
// POST endpoint untuk MCP protocol (standard)
|
||||||
const id = query.id ?? 1;
|
|
||||||
const method = query.method as string;
|
|
||||||
const params = query;
|
|
||||||
|
|
||||||
// Header untuk SSE / Streaming agar diterima oleh n8n
|
|
||||||
set.headers["Content-Type"] = "application/json; charset=utf-8";
|
|
||||||
set.headers["Cache-Control"] = "no-cache, no-transform";
|
|
||||||
set.headers["Connection"] = "keep-alive";
|
|
||||||
set.headers["Transfer-Encoding"] = "chunked";
|
|
||||||
set.headers["X-Accel-Buffering"] = "no"; // Matikan buffering (nginx/cloudflare)
|
|
||||||
|
|
||||||
const stream = new ReadableStream({
|
|
||||||
async start(controller) {
|
|
||||||
// ✅ Heartbeat agar tidak time-out di Cloudflare / n8n
|
|
||||||
controller.enqueue(":\n\n");
|
|
||||||
|
|
||||||
// 1. Handshake MCP Version
|
|
||||||
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; // ❗ biarkan stream tetap terbuka
|
|
||||||
}
|
|
||||||
|
|
||||||
// 2. tools/list → Informasi tools ke n8n
|
|
||||||
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", description: "Your name" },
|
|
||||||
},
|
|
||||||
required: ["name"],
|
|
||||||
},
|
|
||||||
},
|
|
||||||
],
|
|
||||||
}) + "\n"
|
|
||||||
);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 3. tools/sayHello → kirim progres lalu hasil
|
|
||||||
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 || "User"}` },
|
|
||||||
}) + "\n"
|
|
||||||
);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 4. Unknown method
|
|
||||||
controller.enqueue(
|
|
||||||
JSON.stringify({
|
|
||||||
jsonrpc: "2.0",
|
|
||||||
id,
|
|
||||||
error: {
|
|
||||||
code: -32601,
|
|
||||||
message: `Method "${method}" not found`,
|
|
||||||
},
|
|
||||||
}) + "\n"
|
|
||||||
);
|
|
||||||
},
|
|
||||||
cancel() {
|
|
||||||
console.log("🔴 Stream closed by client");
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
return new Response(stream);
|
|
||||||
})
|
|
||||||
// Optional: juga izinkan MCP pakai POST request JSON-RPC
|
|
||||||
.post("/", async ({ body, set }) => {
|
.post("/", async ({ 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["Content-Type"] = "application/json";
|
||||||
set.headers["Connection"] = "keep-alive";
|
|
||||||
set.headers["Transfer-Encoding"] = "chunked";
|
|
||||||
set.headers["X-Accel-Buffering"] = "no";
|
|
||||||
|
|
||||||
const stream = new ReadableStream({
|
try {
|
||||||
async start(controller) {
|
// 1. Initialize - Version handshake
|
||||||
controller.enqueue(":\n\n");
|
if (method === "initialize") {
|
||||||
|
return {
|
||||||
|
jsonrpc: "2.0",
|
||||||
|
id,
|
||||||
|
result: {
|
||||||
|
protocolVersion: "2024-11-05",
|
||||||
|
capabilities: {
|
||||||
|
tools: {
|
||||||
|
listChanged: false
|
||||||
|
}
|
||||||
|
},
|
||||||
|
serverInfo: {
|
||||||
|
name: "elysia-mcp-server",
|
||||||
|
version: "1.0.0"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
if (method === "mcp/version") {
|
// 2. List available tools
|
||||||
controller.enqueue(
|
if (method === "tools/list") {
|
||||||
JSON.stringify({
|
return {
|
||||||
jsonrpc: "2.0",
|
jsonrpc: "2.0",
|
||||||
id,
|
id,
|
||||||
result: {
|
result: {
|
||||||
protocol: "2024-11-05",
|
tools: [
|
||||||
capabilities: {
|
{
|
||||||
"tools/list": true,
|
name: "sayHello",
|
||||||
"tools/call": true,
|
description: "Greets a user by name",
|
||||||
},
|
inputSchema: {
|
||||||
},
|
type: "object",
|
||||||
}) + "\n"
|
properties: {
|
||||||
);
|
name: {
|
||||||
return;
|
type: "string",
|
||||||
}
|
description: "Name of the person to greet"
|
||||||
|
}
|
||||||
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" },
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
},
|
required: ["name"]
|
||||||
],
|
}
|
||||||
}) + "\n"
|
},
|
||||||
);
|
{
|
||||||
return;
|
name: "getCurrentTime",
|
||||||
|
description: "Returns the current server time",
|
||||||
|
inputSchema: {
|
||||||
|
type: "object",
|
||||||
|
properties: {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. Call tool - Execute the requested tool
|
||||||
|
if (method === "tools/call") {
|
||||||
|
const toolName = params?.name;
|
||||||
|
const toolArgs = params?.arguments || {};
|
||||||
|
|
||||||
|
if (toolName === "sayHello") {
|
||||||
|
const userName = toolArgs.name || "User";
|
||||||
|
return {
|
||||||
|
jsonrpc: "2.0",
|
||||||
|
id,
|
||||||
|
result: {
|
||||||
|
content: [
|
||||||
|
{
|
||||||
|
type: "text",
|
||||||
|
text: `Hello, ${userName}! Welcome to the MCP server.`
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
if (method === "tools/sayHello") {
|
if (toolName === "getCurrentTime") {
|
||||||
controller.enqueue(
|
const currentTime = new Date().toISOString();
|
||||||
JSON.stringify({
|
return {
|
||||||
jsonrpc: "2.0",
|
jsonrpc: "2.0",
|
||||||
id,
|
id,
|
||||||
result: { status: "Processing..." },
|
result: {
|
||||||
}) + "\n"
|
content: [
|
||||||
);
|
{
|
||||||
await Bun.sleep(500);
|
type: "text",
|
||||||
|
text: `Current server time: ${currentTime}`
|
||||||
controller.enqueue(
|
}
|
||||||
JSON.stringify({
|
]
|
||||||
jsonrpc: "2.0",
|
}
|
||||||
id,
|
};
|
||||||
result: { message: `Hello ${params?.name || ""}` },
|
|
||||||
}) + "\n"
|
|
||||||
);
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
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`
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
} catch (error: any) {
|
||||||
|
return {
|
||||||
|
jsonrpc: "2.0",
|
||||||
|
id,
|
||||||
|
error: {
|
||||||
|
code: -32603,
|
||||||
|
message: error.message || "Internal error"
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
}, {
|
}, {
|
||||||
body: t.Object({
|
body: t.Object({
|
||||||
jsonrpc: t.Optional(t.String()),
|
jsonrpc: t.String(),
|
||||||
method: t.String(),
|
method: t.String(),
|
||||||
params: t.Optional(t.Record(t.String(), t.Any())),
|
params: t.Optional(t.Any()),
|
||||||
id: t.Optional(t.Union([t.String(), t.Number()])),
|
id: t.Union([t.String(), t.Number()])
|
||||||
}),
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
// GET endpoint untuk testing / health check
|
||||||
|
.get("/", () => {
|
||||||
|
return {
|
||||||
|
status: "ok",
|
||||||
|
message: "MCP Server is running",
|
||||||
|
endpoints: {
|
||||||
|
main: "POST /mcp",
|
||||||
|
methods: ["initialize", "tools/list", "tools/call"]
|
||||||
|
}
|
||||||
|
};
|
||||||
|
})
|
||||||
|
|
||||||
|
// Health check endpoint
|
||||||
|
.get("/health", () => {
|
||||||
|
return {
|
||||||
|
status: "healthy",
|
||||||
|
timestamp: new Date().toISOString()
|
||||||
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
export default MCPRoute;
|
export default MCPRoute;
|
||||||
Reference in New Issue
Block a user