diff --git a/src/server/routes/mcp_route.ts b/src/server/routes/mcp_route.ts index b7a3130..9e9537b 100644 --- a/src/server/routes/mcp_route.ts +++ b/src/server/routes/mcp_route.ts @@ -128,7 +128,8 @@ function convertToMcpContent(payload: any) { export async function executeTool( tool: any, args: Record = {}, - baseUrl: string + baseUrl: string, + xPayload: Record = {} ) { const x = tool["x-props"] || {}; const method = (x.method || "GET").toUpperCase(); @@ -247,6 +248,9 @@ export async function executeTool( // Execute fetch console.log(`[MCP] → ${method} ${url}`); + for(const [key, value] of Object.entries(xPayload)) { + opts.headers![key] = value; + } const res = await fetch(url, opts); const resContentType = (res.headers.get("content-type") || "").toLowerCase(); @@ -281,7 +285,7 @@ export async function executeTool( /* ------------------------- JSON-RPC Handler ------------------------- */ -async function handleMCPRequestAsync(request: JSONRPCRequest): Promise { +async function handleMCPRequestAsync(request: JSONRPCRequest, xPayload: Record): Promise { const { id, method, params } = request; const makeError = (code: number, message: string, data?: any): JSONRPCResponse => ({ @@ -331,7 +335,7 @@ async function handleMCPRequestAsync(request: JSONRPCRequest): Promise { + .post("/mcp", async ({ request, set, headers }) => { set.headers["Content-Type"] = "application/json"; set.headers["Access-Control-Allow-Origin"] = "*"; @@ -378,12 +382,17 @@ export const MCPRoute = new Elysia({ tags: ["MCP Server"] }) } } + const xPayload = { + ['x-user']: headers['x-user'] || "", + ['x-phone']: headers['x-phone'] || "" + } + try { const body = await request.json(); // If batch array -> allSettled for resilience if (Array.isArray(body)) { - const promises = body.map((req: JSONRPCRequest) => handleMCPRequestAsync(req)); + const promises = body.map((req: JSONRPCRequest) => handleMCPRequestAsync(req, xPayload)); const settled = await Promise.allSettled(promises); const responses = settled.map((s) => s.status === "fulfilled" @@ -401,7 +410,7 @@ export const MCPRoute = new Elysia({ tags: ["MCP Server"] }) return responses; } - const single = await handleMCPRequestAsync(body as JSONRPCRequest); + const single = await handleMCPRequestAsync(body as JSONRPCRequest, xPayload); return single; } catch (err: any) { set.status = 400;