tambahan
This commit is contained in:
@@ -36,7 +36,6 @@ const app = new Elysia()
|
|||||||
.use(Api)
|
.use(Api)
|
||||||
.use(Docs)
|
.use(Docs)
|
||||||
.use(Auth)
|
.use(Auth)
|
||||||
.use(MCPRoute)
|
|
||||||
.get(
|
.get(
|
||||||
"/.well-known/mcp.json",
|
"/.well-known/mcp.json",
|
||||||
async () => {
|
async () => {
|
||||||
@@ -50,8 +49,8 @@ const app = new Elysia()
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
// .use(McpRoute)
|
.use(MCPRoute)
|
||||||
.get("*", html)
|
.get("/*", html)
|
||||||
.listen(3000, () => {
|
.listen(3000, () => {
|
||||||
console.log("Server running at http://localhost:3000");
|
console.log("Server running at http://localhost:3000");
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,165 +1,228 @@
|
|||||||
// src/routes/mcp_route.ts
|
// src/routes/mcp_route.ts
|
||||||
import { Elysia, t } from "elysia";
|
import { Elysia, t } from "elysia";
|
||||||
|
|
||||||
|
function createStream(handler: (controller: ReadableStreamDefaultController) => Promise<void>) {
|
||||||
|
return new ReadableStream({
|
||||||
|
async start(controller) {
|
||||||
|
try {
|
||||||
|
controller.enqueue(":\n\n"); // Heartbeat awal
|
||||||
|
await handler(controller);
|
||||||
|
} catch (error) {
|
||||||
|
controller.enqueue(
|
||||||
|
JSON.stringify({
|
||||||
|
jsonrpc: "2.0",
|
||||||
|
error: { code: -32000, message: "Internal Server Error" },
|
||||||
|
}) + "\n"
|
||||||
|
);
|
||||||
|
} finally {
|
||||||
|
try {
|
||||||
|
controller.close();
|
||||||
|
} catch {}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
cancel() {
|
||||||
|
console.log("🔴 Client closed the connection");
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
export const MCPRoute = new Elysia({
|
export const MCPRoute = new Elysia({
|
||||||
prefix: "/mcp",
|
prefix: "/mcp",
|
||||||
|
tags: ["mcp"],
|
||||||
})
|
})
|
||||||
// POST endpoint untuk MCP protocol (standard)
|
/**
|
||||||
.post("/", async ({ body, set }) => {
|
* ✅ GET /mcp/:id — streaming response untuk n8n + curl
|
||||||
const { id, method, params } = body as any;
|
*/
|
||||||
|
.get("/:id", ({ params, query, set }) => {
|
||||||
|
const id = params.id ?? 1;
|
||||||
|
const method = query.method as string | undefined;
|
||||||
|
|
||||||
set.headers["Content-Type"] = "application/json";
|
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";
|
||||||
|
|
||||||
try {
|
const stream = createStream(async (controller) => {
|
||||||
// 1. Initialize - Version handshake
|
// Jika tidak ada method → status server
|
||||||
if (method === "initialize") {
|
if (!method) {
|
||||||
return {
|
controller.enqueue(
|
||||||
jsonrpc: "2.0",
|
JSON.stringify({
|
||||||
id,
|
jsonrpc: "2.0",
|
||||||
result: {
|
id,
|
||||||
protocolVersion: "2024-11-05",
|
result: {
|
||||||
capabilities: {
|
status: "MCP Server Ready",
|
||||||
tools: {
|
message: "Use ?method=mcp/version or ?method=tools/list",
|
||||||
listChanged: false
|
availableMethods: ["mcp/version", "tools/list", "tools/sayHello"],
|
||||||
}
|
|
||||||
},
|
},
|
||||||
serverInfo: {
|
}) + "\n"
|
||||||
name: "elysia-mcp-server",
|
);
|
||||||
version: "1.0.0"
|
return;
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2. List available tools
|
// 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
// tools/list
|
||||||
if (method === "tools/list") {
|
if (method === "tools/list") {
|
||||||
return {
|
controller.enqueue(
|
||||||
jsonrpc: "2.0",
|
JSON.stringify({
|
||||||
id,
|
jsonrpc: "2.0",
|
||||||
result: {
|
id,
|
||||||
tools: [
|
result: [
|
||||||
{
|
{
|
||||||
name: "sayHello",
|
name: "sayHello",
|
||||||
description: "Greets a user by name",
|
description: "Greets a user",
|
||||||
inputSchema: {
|
inputSchema: {
|
||||||
type: "object",
|
type: "object",
|
||||||
properties: {
|
properties: { name: { type: "string", description: "Your name" } },
|
||||||
name: {
|
required: ["name"],
|
||||||
type: "string",
|
},
|
||||||
description: "Name of the person to greet"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
required: ["name"]
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
{
|
],
|
||||||
name: "getCurrentTime",
|
}) + "\n"
|
||||||
description: "Returns the current server time",
|
);
|
||||||
inputSchema: {
|
return;
|
||||||
type: "object",
|
|
||||||
properties: {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 3. Call tool - Execute the requested tool
|
// tools/sayHello (progress + result)
|
||||||
if (method === "tools/call") {
|
if (method === "tools/sayHello") {
|
||||||
const toolName = params?.name;
|
controller.enqueue(
|
||||||
const toolArgs = params?.arguments || {};
|
JSON.stringify({ jsonrpc: "2.0", id, result: { status: "Processing..." } }) + "\n"
|
||||||
|
);
|
||||||
if (toolName === "sayHello") {
|
await Bun.sleep(500);
|
||||||
const userName = toolArgs.name || "User";
|
controller.enqueue(
|
||||||
return {
|
JSON.stringify({
|
||||||
jsonrpc: "2.0",
|
jsonrpc: "2.0",
|
||||||
id,
|
id,
|
||||||
result: {
|
result: { message: `Hello ${query?.name || "User"}` },
|
||||||
content: [
|
}) + "\n"
|
||||||
{
|
);
|
||||||
type: "text",
|
return;
|
||||||
text: `Hello, ${userName}! Welcome to the MCP server.`
|
}
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
if (toolName === "getCurrentTime") {
|
// Unknown method
|
||||||
const currentTime = new Date().toISOString();
|
controller.enqueue(
|
||||||
return {
|
JSON.stringify({
|
||||||
jsonrpc: "2.0",
|
|
||||||
id,
|
|
||||||
result: {
|
|
||||||
content: [
|
|
||||||
{
|
|
||||||
type: "text",
|
|
||||||
text: `Current server time: ${currentTime}`
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
// Tool not found
|
|
||||||
return {
|
|
||||||
jsonrpc: "2.0",
|
jsonrpc: "2.0",
|
||||||
id,
|
id,
|
||||||
error: {
|
error: { code: -32601, message: `Method "${method}" not found` },
|
||||||
code: -32602,
|
}) + "\n"
|
||||||
message: `Tool "${toolName}" not found`
|
);
|
||||||
}
|
});
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
// Method not found
|
return new Response(stream);
|
||||||
return {
|
})
|
||||||
jsonrpc: "2.0",
|
/**
|
||||||
id,
|
* ✅ POST /mcp — JSON-RPC versi body
|
||||||
error: {
|
*/
|
||||||
code: -32601,
|
.post(
|
||||||
message: `Method "${method}" not found`
|
"/",
|
||||||
}
|
async ({ body, set }) => {
|
||||||
};
|
const { id, method, params } = body as any;
|
||||||
|
|
||||||
} catch (error: any) {
|
set.headers["Content-Type"] = "application/json; charset=utf-8";
|
||||||
return {
|
set.headers["Connection"] = "keep-alive";
|
||||||
jsonrpc: "2.0",
|
set.headers["Transfer-Encoding"] = "chunked";
|
||||||
id,
|
set.headers["X-Accel-Buffering"] = "no";
|
||||||
error: {
|
|
||||||
code: -32603,
|
const stream = createStream(async (controller) => {
|
||||||
message: error.message || "Internal error"
|
if (!method) {
|
||||||
|
controller.enqueue(
|
||||||
|
JSON.stringify({
|
||||||
|
jsonrpc: "2.0",
|
||||||
|
id,
|
||||||
|
error: { code: -32600, message: "Method required" },
|
||||||
|
}) + "\n"
|
||||||
|
);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
controller.enqueue(
|
||||||
|
JSON.stringify({
|
||||||
|
jsonrpc: "2.0",
|
||||||
|
id,
|
||||||
|
error: { code: -32601, message: `Method "${method}" not found` },
|
||||||
|
}) + "\n"
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
return new Response(stream);
|
||||||
|
},
|
||||||
|
{
|
||||||
|
body: t.Object({
|
||||||
|
jsonrpc: t.Optional(t.String()),
|
||||||
|
method: t.String(),
|
||||||
|
params: t.Optional(t.Record(t.String(), t.Any())),
|
||||||
|
id: t.Optional(t.Union([t.String(), t.Number()])),
|
||||||
|
}),
|
||||||
}
|
}
|
||||||
}, {
|
);
|
||||||
body: t.Object({
|
|
||||||
jsonrpc: t.String(),
|
|
||||||
method: t.String(),
|
|
||||||
params: t.Optional(t.Any()),
|
|
||||||
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