tambahan
This commit is contained in:
@@ -20,17 +20,28 @@ const sessions = new Map<string, Set<Client>>();
|
|||||||
// Helper Functions
|
// Helper Functions
|
||||||
// =====================
|
// =====================
|
||||||
function isAuthorized(headers: Headers) {
|
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;
|
return headers.get("x-api-key") === API_KEY;
|
||||||
}
|
}
|
||||||
|
|
||||||
function formatSSE(event: string, data: any, id?: string) {
|
function formatSSE(event: string, data: any, id?: string) {
|
||||||
const payload = typeof data === "string" ? data : JSON.stringify(data);
|
const payload = typeof data === "string" ? data : JSON.stringify(data);
|
||||||
return [
|
const lines: string[] = [];
|
||||||
id ? `id: ${id}` : "",
|
|
||||||
event ? `event: ${event}` : "",
|
if (id) lines.push(`id: ${id}`);
|
||||||
...payload.split("\n").map((line) => `data: ${line}`),
|
if (event) lines.push(`event: ${event}`);
|
||||||
"",
|
|
||||||
].join("\n");
|
// 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) {
|
function broadcast(sessionId: string, event: string, data: any) {
|
||||||
@@ -55,24 +66,45 @@ function broadcast(sessionId: string, event: string, data: any) {
|
|||||||
type Tool = {
|
type Tool = {
|
||||||
name: string;
|
name: string;
|
||||||
description: string;
|
description: string;
|
||||||
|
inputSchema?: {
|
||||||
|
type: string;
|
||||||
|
properties?: Record<string, any>;
|
||||||
|
required?: string[];
|
||||||
|
};
|
||||||
run: (input?: any) => Promise<any>;
|
run: (input?: any) => Promise<any>;
|
||||||
};
|
};
|
||||||
|
|
||||||
// contoh tools sederhana (bisa dikembangkan)
|
|
||||||
const tools: Tool[] = [
|
const tools: Tool[] = [
|
||||||
{
|
{
|
||||||
name: "ping",
|
name: "ping",
|
||||||
description: "Mengembalikan timestamp saat ini dari server.",
|
description: "Mengembalikan timestamp saat ini dari server.",
|
||||||
|
inputSchema: {
|
||||||
|
type: "object",
|
||||||
|
properties: {},
|
||||||
|
},
|
||||||
run: async () => ({ pong: Date.now() }),
|
run: async () => ({ pong: Date.now() }),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "uuid",
|
name: "uuid",
|
||||||
description: "Menghasilkan UUID v4 unik.",
|
description: "Menghasilkan UUID v4 unik.",
|
||||||
|
inputSchema: {
|
||||||
|
type: "object",
|
||||||
|
properties: {},
|
||||||
|
},
|
||||||
run: async () => ({ uuid: uuidv4() }),
|
run: async () => ({ uuid: uuidv4() }),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "echo",
|
name: "echo",
|
||||||
description: "Mengembalikan data yang dikirim.",
|
description: "Mengembalikan data yang dikirim.",
|
||||||
|
inputSchema: {
|
||||||
|
type: "object",
|
||||||
|
properties: {
|
||||||
|
message: {
|
||||||
|
type: "string",
|
||||||
|
description: "Message to echo back",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
run: async (input) => ({ echo: input }),
|
run: async (input) => ({ echo: input }),
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
@@ -84,22 +116,40 @@ export const MCPRoute = new Elysia()
|
|||||||
// =====================
|
// =====================
|
||||||
// SSE Stream
|
// SSE Stream
|
||||||
// =====================
|
// =====================
|
||||||
.get("/mcp/:sessionId", ({ params, set }) => {
|
.get("/mcp/:sessionId", ({ params, set, request }) => {
|
||||||
const { sessionId } = params;
|
const { sessionId } = params;
|
||||||
|
|
||||||
set.headers["Content-Type"] = "text/event-stream; charset=utf-8";
|
// Check authorization for SSE connection
|
||||||
set.headers["Cache-Control"] = "no-cache";
|
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["Connection"] = "keep-alive";
|
||||||
set.headers["Access-Control-Allow-Origin"] = "*";
|
set.headers["Access-Control-Allow-Origin"] = "*";
|
||||||
|
set.headers["X-Accel-Buffering"] = "no";
|
||||||
|
|
||||||
const stream = new TransformStream();
|
const stream = new TransformStream();
|
||||||
const writer = stream.writable.getWriter();
|
const writer = stream.writable.getWriter();
|
||||||
|
const encoder = new TextEncoder();
|
||||||
|
|
||||||
const client: Client = {
|
const client: Client = {
|
||||||
id: uuidv4(),
|
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: () => {
|
close: () => {
|
||||||
writer.close();
|
try {
|
||||||
|
writer.close();
|
||||||
|
} catch (e) {
|
||||||
|
// Stream already closed
|
||||||
|
}
|
||||||
const set = sessions.get(sessionId);
|
const set = sessions.get(sessionId);
|
||||||
if (set) {
|
if (set) {
|
||||||
set.delete(client);
|
set.delete(client);
|
||||||
@@ -111,22 +161,47 @@ export const MCPRoute = new Elysia()
|
|||||||
if (!sessions.has(sessionId)) sessions.set(sessionId, new Set());
|
if (!sessions.has(sessionId)) sessions.set(sessionId, new Set());
|
||||||
sessions.get(sessionId)!.add(client);
|
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(() => {
|
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);
|
}, PING_INTERVAL_MS);
|
||||||
|
|
||||||
const readable = stream.readable;
|
const readable = stream.readable;
|
||||||
const abort = new AbortController();
|
|
||||||
|
|
||||||
abort.signal.addEventListener("abort", () => {
|
// Cleanup on connection close
|
||||||
|
readable.pipeTo(new WritableStream()).catch(() => {
|
||||||
clearInterval(ping);
|
clearInterval(ping);
|
||||||
client.close();
|
client.close();
|
||||||
});
|
});
|
||||||
|
|
||||||
return new Response(readable, {
|
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,
|
status: 200,
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
@@ -140,6 +215,7 @@ export const MCPRoute = new Elysia()
|
|||||||
return {
|
return {
|
||||||
sessionId: params.sessionId,
|
sessionId: params.sessionId,
|
||||||
connected: clients?.size ?? 0,
|
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 }) => {
|
.post("/mcp/:sessionId", async ({ params, request, set }) => {
|
||||||
set.headers["Access-Control-Allow-Origin"] = "*";
|
set.headers["Access-Control-Allow-Origin"] = "*";
|
||||||
if (!isAuthorized(request.headers)) {
|
if (!isAuthorized(request.headers)) {
|
||||||
return new Response("Unauthorized", { status: 401 });
|
set.status = 401;
|
||||||
|
return { error: "Unauthorized" };
|
||||||
}
|
}
|
||||||
|
|
||||||
const body = await request.json();
|
const body = await request.json();
|
||||||
@@ -166,7 +243,8 @@ export const MCPRoute = new Elysia()
|
|||||||
.delete("/mcp/:sessionId", ({ params, request, set }) => {
|
.delete("/mcp/:sessionId", ({ params, request, set }) => {
|
||||||
set.headers["Access-Control-Allow-Origin"] = "*";
|
set.headers["Access-Control-Allow-Origin"] = "*";
|
||||||
if (!isAuthorized(request.headers)) {
|
if (!isAuthorized(request.headers)) {
|
||||||
return new Response("Unauthorized", { status: 401 });
|
set.status = 401;
|
||||||
|
return { error: "Unauthorized" };
|
||||||
}
|
}
|
||||||
|
|
||||||
const clients = sessions.get(params.sessionId);
|
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"] = "*";
|
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"] = "*";
|
set.headers["Access-Control-Allow-Origin"] = "*";
|
||||||
if (!isAuthorized(request.headers)) {
|
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);
|
const tool = tools.find((t) => t.name === params.toolName);
|
||||||
if (!tool) {
|
if (!tool) {
|
||||||
return new Response(
|
set.status = 404;
|
||||||
JSON.stringify({ error: `Tool '${params.toolName}' not found` }),
|
return { error: `Tool '${params.toolName}' not found` };
|
||||||
{ status: 404 }
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const body = await request.json().catch(() => ({}));
|
try {
|
||||||
const result = await tool.run(body);
|
const body = await request.json().catch(() => ({}));
|
||||||
return { ok: true, tool: tool.name, result };
|
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 }) => {
|
.options("/mcp/:sessionId", ({ set }) => {
|
||||||
set.headers["Access-Control-Allow-Origin"] = "*";
|
set.headers["Access-Control-Allow-Origin"] = "*";
|
||||||
set.headers["Access-Control-Allow-Methods"] = "GET,POST,DELETE,OPTIONS";
|
set.headers["Access-Control-Allow-Methods"] = "GET,POST,DELETE,OPTIONS";
|
||||||
set.headers["Access-Control-Allow-Headers"] = "Content-Type,X-API-Key";
|
set.headers["Access-Control-Allow-Headers"] = "Content-Type,X-API-Key,Authorization";
|
||||||
return new Response(null, { status: 204 });
|
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 "";
|
||||||
});
|
});
|
||||||
Reference in New Issue
Block a user