This commit is contained in:
2025-10-28 15:00:57 +08:00
parent bf0083e678
commit 6a9ce54311
3 changed files with 815 additions and 231 deletions

View File

@@ -1,102 +1,8 @@
import { Elysia } from "elysia";
import { v4 as uuidv4 } from "uuid";
import { getMcpTools } from "../lib/mcp_tool_convert";
// import tools from "./../../../tools.json";
// const API_KEY = process.env.MCP_API_KEY ?? "super-secret-key";
// const PORT = Number(process.env.PORT ?? 3000);
// // =====================
// // 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;
// }
// =====================
// Tools Definition
// =====================
type Tool = {
name: string;
description: string;
inputSchema: {
type: string;
properties: Record<string, any>;
required?: string[];
additionalProperties?: boolean;
$schema?: string;
};
run: (input?: any) => Promise<any>;
};
const tools: Tool[] = [
{
name: "perbekal_darmasaba",
description: "Mengembalikan nama perbekal darmasaba",
inputSchema: {
type: "object",
properties: {},
additionalProperties: true,
$schema: "http://json-schema.org/draft-07/schema#",
},
run: async () => ({ perbekal_darmasaba: "malik kurosaki" }),
},
{
name: "uuid",
description: "Menghasilkan UUID v4 unik.",
inputSchema: {
type: "object",
properties: {},
additionalProperties: true,
$schema: "http://json-schema.org/draft-07/schema#",
},
run: async () => ({ uuid: uuidv4() }),
},
{
name: "echo",
description: "Mengembalikan data yang dikirim.",
inputSchema: {
type: "object",
properties: {
input: {
type: "string",
description: "Message to echo back",
},
},
required: ["input"],
additionalProperties: true,
$schema: "http://json-schema.org/draft-07/schema#",
},
run: async (input) => ({ echo: input }),
},
{
name: "Calculator",
description: "Useful for getting the result of a math expression. The input to this tool should be a valid mathematical expression that could be executed by a simple calculator.",
inputSchema: {
type: "object",
properties: {
input: {
type: "string",
},
},
required: ["input"],
additionalProperties: true,
$schema: "http://json-schema.org/draft-07/schema#",
},
run: async (input) => {
try {
// Simple math evaluation (be careful in production!)
const result = Function(`"use strict"; return (${input.input})`)();
return { result: String(result) };
} catch (error: any) {
throw new Error(`Invalid expression: ${error.message}`);
}
},
},
];
var tools = [] as any[];
// =====================
// MCP Protocol Types
@@ -119,16 +25,50 @@ type JSONRPCResponse = {
};
};
type JSONRPCNotification = {
jsonrpc: "2.0";
method: string;
params?: any;
};
// =====================
// Tool Executor
// =====================
export async function executeTool(
tool: any,
args: Record<string, any> = {},
baseUrl: string
) {
const x = tool["x-props"] || {};
const method = (x.method || "GET").toUpperCase();
const path = x.path || `/${tool.name}`;
const url = `${baseUrl}${path}`;
const opts: RequestInit = {
method,
headers: { "Content-Type": "application/json" },
};
if (["POST", "PUT", "PATCH", "DELETE"].includes(method)) {
opts.body = JSON.stringify(args || {});
}
const res = await fetch(url, opts);
const contentType = res.headers.get("content-type") || "";
const data = contentType.includes("application/json")
? await res.json()
: await res.text();
return {
success: res.ok,
status: res.status,
method,
path,
data,
};
}
// =====================
// MCP Handler
// MCP Handler (Async)
// =====================
function handleMCPRequest(request: JSONRPCRequest): JSONRPCResponse {
async function handleMCPRequestAsync(
request: JSONRPCRequest
): Promise<JSONRPCResponse> {
const { id, method, params } = request;
switch (method) {
@@ -138,13 +78,8 @@ function handleMCPRequest(request: JSONRPCRequest): JSONRPCResponse {
id,
result: {
protocolVersion: "2024-11-05",
capabilities: {
tools: {},
},
serverInfo: {
name: "elysia-mcp-server",
version: "1.0.0",
},
capabilities: { tools: {} },
serverInfo: { name: "elysia-mcp-server", version: "1.0.0" },
},
};
@@ -153,15 +88,16 @@ function handleMCPRequest(request: JSONRPCRequest): JSONRPCResponse {
jsonrpc: "2.0",
id,
result: {
tools: tools.map(({ name, description, inputSchema }) => ({
tools: tools.map(({ name, description, inputSchema, ["x-props"]: x }) => ({
name,
description,
inputSchema,
"x-props": x,
})),
},
};
case "tools/call":
case "tools/call": {
const toolName = params?.name;
const tool = tools.find((t) => t.name === toolName);
@@ -169,18 +105,14 @@ function handleMCPRequest(request: JSONRPCRequest): JSONRPCResponse {
return {
jsonrpc: "2.0",
id,
error: {
code: -32601,
message: `Tool '${toolName}' not found`,
},
error: { code: -32601, message: `Tool '${toolName}' not found` },
};
}
try {
// Note: This is synchronous for simplicity
// In real implementation, you'd need to handle async properly
let result: any;
tool.run(params?.arguments || {}).then((r) => (result = r));
const baseUrl =
process.env.BUN_PUBLIC_BASE_URL || "http://localhost:3000";
const result = await executeTool(tool, params?.arguments || {}, baseUrl);
return {
jsonrpc: "2.0",
@@ -189,7 +121,7 @@ function handleMCPRequest(request: JSONRPCRequest): JSONRPCResponse {
content: [
{
type: "text",
text: JSON.stringify(result || { pending: true }),
text: JSON.stringify(result, null, 2),
},
],
},
@@ -198,111 +130,48 @@ function handleMCPRequest(request: JSONRPCRequest): JSONRPCResponse {
return {
jsonrpc: "2.0",
id,
error: {
code: -32603,
message: error.message,
},
error: { code: -32603, message: error.message },
};
}
}
case "ping":
return {
jsonrpc: "2.0",
id,
result: {},
};
return { jsonrpc: "2.0", id, result: {} };
default:
return {
jsonrpc: "2.0",
id,
error: {
code: -32601,
message: `Method '${method}' not found`,
},
error: { code: -32601, message: `Method '${method}' not found` },
};
}
}
async function handleMCPRequestAsync(request: JSONRPCRequest): Promise<JSONRPCResponse> {
const { id, method, params } = request;
if (method === "tools/call") {
const toolName = params?.name;
const tool = tools.find((t) => t.name === toolName);
if (!tool) {
return {
jsonrpc: "2.0",
id,
error: {
code: -32601,
message: `Tool '${toolName}' not found`,
},
};
}
try {
const result = await tool.run(params?.arguments || {});
return {
jsonrpc: "2.0",
id,
result: {
content: [
{
type: "text",
text: JSON.stringify(result),
},
],
},
};
} catch (error: any) {
return {
jsonrpc: "2.0",
id,
error: {
code: -32603,
message: error.message,
},
};
}
}
// For other methods, use sync handler
return handleMCPRequest(request);
}
// =====================
// Server Initialization
// Elysia MCP Server
// =====================
export const MCPRoute = new Elysia()
// =====================
// MCP HTTP Streamable Endpoint
// =====================
.post("/mcp/:sessionId", async ({ params, request, set }) => {
export const MCPRoute = new Elysia({
tags: ["MCP"]
})
.post("/mcp", async ({ request, set }) => {
if (!tools.length) {
tools = await getMcpTools();
}
set.headers["Content-Type"] = "application/json";
set.headers["Access-Control-Allow-Origin"] = "*";
// Optional: Check authorization
// if (!isAuthorized(request.headers)) {
// set.status = 401;
// return { error: "Unauthorized" };
// }
try {
const body = await request.json();
// Handle single request
if (!Array.isArray(body)) {
const response = await handleMCPRequestAsync(body as JSONRPCRequest);
return response;
const res = await handleMCPRequestAsync(body);
return res;
}
// Handle batch requests
const responses = await Promise.all(
body.map((req) => handleMCPRequestAsync(req as JSONRPCRequest))
const results = await Promise.all(
body.map((req) => handleMCPRequestAsync(req))
);
return responses;
return results;
} catch (error: any) {
set.status = 400;
return {
@@ -317,60 +186,58 @@ export const MCPRoute = new Elysia()
}
})
// =====================
// Simple tools list endpoint (for debugging)
// =====================
.get("/mcp/:sessionId/tools", ({ set }) => {
// Tools list (debug)
.get("/mcp/tools", async ({ set }) => {
if (!tools.length) {
tools = await getMcpTools();
}
set.headers["Access-Control-Allow-Origin"] = "*";
return {
data: tools.map(({ name, description, inputSchema }) => ({
tools: tools.map(({ name, description, inputSchema, ["x-props"]: x }) => ({
name,
value: name,
description,
inputSchema,
"x-props": x,
})),
};
})
// =====================
// Session Status
// =====================
.get("/mcp/:sessionId/status", ({ params, set }) => {
// MCP status
.get("/mcp/status", ({ set }) => {
set.headers["Access-Control-Allow-Origin"] = "*";
return {
sessionId: params.sessionId,
status: "active",
timestamp: Date.now(),
};
return { status: "active", timestamp: Date.now() };
})
// =====================
// Health Check
// =====================
// Health check
.get("/health", ({ set }) => {
set.headers["Access-Control-Allow-Origin"] = "*";
return { status: "ok", timestamp: Date.now(), tools: tools.length };
})
.get("/mcp/init", async ({ set }) => {
const _tools = await getMcpTools();
tools = _tools;
return {
status: "ok",
timestamp: Date.now(),
success: true,
message: "MCP initialized",
tools: tools.length,
};
})
// =====================
// CORS preflight
// =====================
.options("/mcp/:sessionId", ({ set }) => {
// CORS
.options("/mcp", ({ set }) => {
set.headers["Access-Control-Allow-Origin"] = "*";
set.headers["Access-Control-Allow-Methods"] = "GET,POST,OPTIONS";
set.headers["Access-Control-Allow-Headers"] = "Content-Type,Authorization,X-API-Key";
set.headers["Access-Control-Allow-Headers"] =
"Content-Type,Authorization,X-API-Key";
set.status = 204;
return "";
})
.options("/mcp/:sessionId/tools", ({ set }) => {
.options("/mcp/tools", ({ set }) => {
set.headers["Access-Control-Allow-Origin"] = "*";
set.headers["Access-Control-Allow-Methods"] = "GET,OPTIONS";
set.headers["Access-Control-Allow-Headers"] = "Content-Type,Authorization,X-API-Key";
set.headers["Access-Control-Allow-Headers"] =
"Content-Type,Authorization,X-API-Key";
set.status = 204;
return "";
});
});