This commit is contained in:
bipproduction
2025-10-27 10:12:27 +08:00
parent 2a2906081e
commit bbd12702aa

View File

@@ -20,17 +20,28 @@ const sessions = new Map<string, Set<Client>>();
// Helper Functions
// =====================
function isAuthorized(headers: Headers) {
const authHeader = headers.get("authorization");
if (authHeader?.startsWith("Bearer ")) {
const token = authHeader.substring(7);
return token === API_KEY;
}
return headers.get("x-api-key") === API_KEY;
}
function formatSSE(event: string, data: any, id?: string) {
const payload = typeof data === "string" ? data : JSON.stringify(data);
return [
id ? `id: ${id}` : "",
event ? `event: ${event}` : "",
...payload.split("\n").map((line) => `data: ${line}`),
"",
].join("\n");
const lines: string[] = [];
if (id) lines.push(`id: ${id}`);
if (event) lines.push(`event: ${event}`);
// Split data into multiple data: lines if needed
payload.split("\n").forEach(line => {
lines.push(`data: ${line}`);
});
lines.push(""); // Empty line to end the message
return lines.join("\n") + "\n";
}
function broadcast(sessionId: string, event: string, data: any) {
@@ -55,24 +66,45 @@ function broadcast(sessionId: string, event: string, data: any) {
type Tool = {
name: string;
description: string;
inputSchema?: {
type: string;
properties?: Record<string, any>;
required?: string[];
};
run: (input?: any) => Promise<any>;
};
// contoh tools sederhana (bisa dikembangkan)
const tools: Tool[] = [
{
name: "ping",
description: "Mengembalikan timestamp saat ini dari server.",
inputSchema: {
type: "object",
properties: {},
},
run: async () => ({ pong: Date.now() }),
},
{
name: "uuid",
description: "Menghasilkan UUID v4 unik.",
inputSchema: {
type: "object",
properties: {},
},
run: async () => ({ uuid: uuidv4() }),
},
{
name: "echo",
description: "Mengembalikan data yang dikirim.",
inputSchema: {
type: "object",
properties: {
message: {
type: "string",
description: "Message to echo back",
},
},
},
run: async (input) => ({ echo: input }),
},
];
@@ -84,22 +116,40 @@ export const MCPRoute = new Elysia()
// =====================
// SSE Stream
// =====================
.get("/mcp/:sessionId", ({ params, set }) => {
.get("/mcp/:sessionId", ({ params, set, request }) => {
const { sessionId } = params;
set.headers["Content-Type"] = "text/event-stream; charset=utf-8";
set.headers["Cache-Control"] = "no-cache";
// Check authorization for SSE connection
if (!isAuthorized(request.headers)) {
set.status = 401;
return { error: "Unauthorized" };
}
set.headers["Content-Type"] = "text/event-stream";
set.headers["Cache-Control"] = "no-cache, no-transform";
set.headers["Connection"] = "keep-alive";
set.headers["Access-Control-Allow-Origin"] = "*";
set.headers["X-Accel-Buffering"] = "no";
const stream = new TransformStream();
const writer = stream.writable.getWriter();
const encoder = new TextEncoder();
const client: Client = {
id: uuidv4(),
send: (data) => writer.write(new TextEncoder().encode(data + "\n")),
send: (data) => {
try {
writer.write(encoder.encode(data));
} catch (e) {
console.error("Error writing to stream:", e);
}
},
close: () => {
writer.close();
try {
writer.close();
} catch (e) {
// Stream already closed
}
const set = sessions.get(sessionId);
if (set) {
set.delete(client);
@@ -111,22 +161,47 @@ export const MCPRoute = new Elysia()
if (!sessions.has(sessionId)) sessions.set(sessionId, new Set());
sessions.get(sessionId)!.add(client);
client.send(formatSSE("connected", { sessionId, id: client.id }));
// Send initial connection message
client.send(formatSSE("connected", {
sessionId,
clientId: client.id,
timestamp: Date.now()
}));
// Send tools list on connection
client.send(formatSSE("tools", {
tools: tools.map(({ name, description, inputSchema }) => ({
name,
description,
inputSchema
}))
}));
// Setup ping interval
const ping = setInterval(() => {
client.send(formatSSE("ping", { ts: Date.now() }));
try {
client.send(formatSSE("ping", { ts: Date.now() }));
} catch (e) {
clearInterval(ping);
}
}, PING_INTERVAL_MS);
const readable = stream.readable;
const abort = new AbortController();
abort.signal.addEventListener("abort", () => {
// Cleanup on connection close
readable.pipeTo(new WritableStream()).catch(() => {
clearInterval(ping);
client.close();
});
return new Response(readable, {
headers: set.headers as HeadersInit,
headers: {
"Content-Type": "text/event-stream",
"Cache-Control": "no-cache, no-transform",
"Connection": "keep-alive",
"Access-Control-Allow-Origin": "*",
"X-Accel-Buffering": "no",
},
status: 200,
});
})
@@ -140,6 +215,7 @@ export const MCPRoute = new Elysia()
return {
sessionId: params.sessionId,
connected: clients?.size ?? 0,
clients: Array.from(clients || []).map(c => c.id),
};
})
@@ -149,7 +225,8 @@ export const MCPRoute = new Elysia()
.post("/mcp/:sessionId", async ({ params, request, set }) => {
set.headers["Access-Control-Allow-Origin"] = "*";
if (!isAuthorized(request.headers)) {
return new Response("Unauthorized", { status: 401 });
set.status = 401;
return { error: "Unauthorized" };
}
const body = await request.json();
@@ -166,7 +243,8 @@ export const MCPRoute = new Elysia()
.delete("/mcp/:sessionId", ({ params, request, set }) => {
set.headers["Access-Control-Allow-Origin"] = "*";
if (!isAuthorized(request.headers)) {
return new Response("Unauthorized", { status: 401 });
set.status = 401;
return { error: "Unauthorized" };
}
const clients = sessions.get(params.sessionId);
@@ -178,33 +256,108 @@ export const MCPRoute = new Elysia()
})
// =====================
// Tools Introspection
// Tools Introspection (Fixed path)
// =====================
.get("/mcp/tools", ({ set }) => {
.get("/mcp/:sessionId/tools", ({ params, set, request }) => {
set.headers["Access-Control-Allow-Origin"] = "*";
return tools.map(({ name, description }) => ({ name, description }));
// Optional: Check auth if needed
// if (!isAuthorized(request.headers)) {
// set.status = 401;
// return { error: "Unauthorized" };
// }
return {
tools: tools.map(({ name, description, inputSchema }) => ({
name,
description,
inputSchema
}))
};
})
// Alternative global tools endpoint
.get("/tools", ({ set }) => {
set.headers["Access-Control-Allow-Origin"] = "*";
return {
tools: tools.map(({ name, description, inputSchema }) => ({
name,
description,
inputSchema
}))
};
})
// =====================
// Run Tool
// Run Tool (Fixed path)
// =====================
.post("/mcp/tools/:toolName", async ({ params, request, set }) => {
.post("/mcp/:sessionId/tools/:toolName", async ({ params, request, set }) => {
set.headers["Access-Control-Allow-Origin"] = "*";
if (!isAuthorized(request.headers)) {
return new Response("Unauthorized", { status: 401 });
set.status = 401;
return { error: "Unauthorized" };
}
const tool = tools.find((t) => t.name === params.toolName);
if (!tool) {
return new Response(
JSON.stringify({ error: `Tool '${params.toolName}' not found` }),
{ status: 404 }
);
set.status = 404;
return { error: `Tool '${params.toolName}' not found` };
}
const body = await request.json().catch(() => ({}));
const result = await tool.run(body);
return { ok: true, tool: tool.name, result };
try {
const body = await request.json().catch(() => ({}));
const result = await tool.run(body);
// Broadcast tool execution result to session
broadcast(params.sessionId, "tool_result", {
tool: tool.name,
result,
timestamp: Date.now(),
});
return {
ok: true,
tool: tool.name,
result
};
} catch (error: any) {
set.status = 500;
return {
error: "Tool execution failed",
message: error.message
};
}
})
// Alternative global tool execution
.post("/tools/:toolName", async ({ params, request, set }) => {
set.headers["Access-Control-Allow-Origin"] = "*";
if (!isAuthorized(request.headers)) {
set.status = 401;
return { error: "Unauthorized" };
}
const tool = tools.find((t) => t.name === params.toolName);
if (!tool) {
set.status = 404;
return { error: `Tool '${params.toolName}' not found` };
}
try {
const body = await request.json().catch(() => ({}));
const result = await tool.run(body);
return {
ok: true,
tool: tool.name,
result
};
} catch (error: any) {
set.status = 500;
return {
error: "Tool execution failed",
message: error.message
};
}
})
// =====================
@@ -213,6 +366,23 @@ export const MCPRoute = new Elysia()
.options("/mcp/:sessionId", ({ set }) => {
set.headers["Access-Control-Allow-Origin"] = "*";
set.headers["Access-Control-Allow-Methods"] = "GET,POST,DELETE,OPTIONS";
set.headers["Access-Control-Allow-Headers"] = "Content-Type,X-API-Key";
return new Response(null, { status: 204 });
});
set.headers["Access-Control-Allow-Headers"] = "Content-Type,X-API-Key,Authorization";
set.status = 204;
return "";
})
.options("/mcp/:sessionId/tools", ({ set }) => {
set.headers["Access-Control-Allow-Origin"] = "*";
set.headers["Access-Control-Allow-Methods"] = "GET,POST,OPTIONS";
set.headers["Access-Control-Allow-Headers"] = "Content-Type,X-API-Key,Authorization";
set.status = 204;
return "";
})
.options("/mcp/:sessionId/tools/:toolName", ({ set }) => {
set.headers["Access-Control-Allow-Origin"] = "*";
set.headers["Access-Control-Allow-Methods"] = "POST,OPTIONS";
set.headers["Access-Control-Allow-Headers"] = "Content-Type,X-API-Key,Authorization";
set.status = 204;
return "";
});