tambahan untuk handle true user
This commit is contained in:
@@ -128,7 +128,8 @@ function convertToMcpContent(payload: any) {
|
|||||||
export async function executeTool(
|
export async function executeTool(
|
||||||
tool: any,
|
tool: any,
|
||||||
args: Record<string, any> = {},
|
args: Record<string, any> = {},
|
||||||
baseUrl: string
|
baseUrl: string,
|
||||||
|
xPayload: Record<string, any> = {}
|
||||||
) {
|
) {
|
||||||
const x = tool["x-props"] || {};
|
const x = tool["x-props"] || {};
|
||||||
const method = (x.method || "GET").toUpperCase();
|
const method = (x.method || "GET").toUpperCase();
|
||||||
@@ -247,6 +248,9 @@ export async function executeTool(
|
|||||||
|
|
||||||
// Execute fetch
|
// Execute fetch
|
||||||
console.log(`[MCP] → ${method} ${url}`);
|
console.log(`[MCP] → ${method} ${url}`);
|
||||||
|
for(const [key, value] of Object.entries(xPayload)) {
|
||||||
|
opts.headers![key] = value;
|
||||||
|
}
|
||||||
const res = await fetch(url, opts);
|
const res = await fetch(url, opts);
|
||||||
|
|
||||||
const resContentType = (res.headers.get("content-type") || "").toLowerCase();
|
const resContentType = (res.headers.get("content-type") || "").toLowerCase();
|
||||||
@@ -281,7 +285,7 @@ export async function executeTool(
|
|||||||
/* -------------------------
|
/* -------------------------
|
||||||
JSON-RPC Handler
|
JSON-RPC Handler
|
||||||
------------------------- */
|
------------------------- */
|
||||||
async function handleMCPRequestAsync(request: JSONRPCRequest): Promise<JSONRPCResponse> {
|
async function handleMCPRequestAsync(request: JSONRPCRequest, xPayload: Record<string, any>): Promise<JSONRPCResponse> {
|
||||||
const { id, method, params } = request;
|
const { id, method, params } = request;
|
||||||
|
|
||||||
const makeError = (code: number, message: string, data?: any): JSONRPCResponse => ({
|
const makeError = (code: number, message: string, data?: any): JSONRPCResponse => ({
|
||||||
@@ -331,7 +335,7 @@ async function handleMCPRequestAsync(request: JSONRPCRequest): Promise<JSONRPCRe
|
|||||||
const baseUrl = (params?.credentials?.baseUrl as string) || process.env.BUN_PUBLIC_BASE_URL || "http://localhost:3000";
|
const baseUrl = (params?.credentials?.baseUrl as string) || process.env.BUN_PUBLIC_BASE_URL || "http://localhost:3000";
|
||||||
const args = params?.arguments || {};
|
const args = params?.arguments || {};
|
||||||
|
|
||||||
const result = await executeTool(tool, args, baseUrl);
|
const result = await executeTool(tool, args, baseUrl, xPayload);
|
||||||
|
|
||||||
// Extract the meaningful payload (prefer nested .data if present)
|
// Extract the meaningful payload (prefer nested .data if present)
|
||||||
const raw = extractRaw(result.data);
|
const raw = extractRaw(result.data);
|
||||||
@@ -365,7 +369,7 @@ async function handleMCPRequestAsync(request: JSONRPCRequest): Promise<JSONRPCRe
|
|||||||
Elysia App & Routes
|
Elysia App & Routes
|
||||||
------------------------- */
|
------------------------- */
|
||||||
export const MCPRoute = new Elysia({ tags: ["MCP Server"] })
|
export const MCPRoute = new Elysia({ tags: ["MCP Server"] })
|
||||||
.post("/mcp", async ({ request, set }) => {
|
.post("/mcp", async ({ request, set, headers }) => {
|
||||||
set.headers["Content-Type"] = "application/json";
|
set.headers["Content-Type"] = "application/json";
|
||||||
set.headers["Access-Control-Allow-Origin"] = "*";
|
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 {
|
try {
|
||||||
const body = await request.json();
|
const body = await request.json();
|
||||||
|
|
||||||
// If batch array -> allSettled for resilience
|
// If batch array -> allSettled for resilience
|
||||||
if (Array.isArray(body)) {
|
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 settled = await Promise.allSettled(promises);
|
||||||
const responses = settled.map((s) =>
|
const responses = settled.map((s) =>
|
||||||
s.status === "fulfilled"
|
s.status === "fulfilled"
|
||||||
@@ -401,7 +410,7 @@ export const MCPRoute = new Elysia({ tags: ["MCP Server"] })
|
|||||||
return responses;
|
return responses;
|
||||||
}
|
}
|
||||||
|
|
||||||
const single = await handleMCPRequestAsync(body as JSONRPCRequest);
|
const single = await handleMCPRequestAsync(body as JSONRPCRequest, xPayload);
|
||||||
return single;
|
return single;
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
set.status = 400;
|
set.status = 400;
|
||||||
|
|||||||
Reference in New Issue
Block a user