import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; import { z } from "zod"; import { Env } from "../types.js"; export function registerExecuteWorkflow(server: McpServer, env: Env, orgNamespace: string, partnerToken: string) { server.tool( "u6u_execute_workflow", "在沙盒環境中即時執行工作流,驗證 triplets 邏輯是否正確。每個 config 鍵對應 triplets 中的節點名,內含 component(零件 canonical_id)、recipe(prompt_recipe:xxx,選用)、與該節點的其他靜態參數。", { triplets: z.array(z.string()).describe("工作流三元組,例:['input >> 完成後 >> synth']"), context: z.record(z.string(), z.any()).describe("初始變數(測試資料 / 上游節點輸出模擬)"), config: z.record(z.string(), z.record(z.string(), z.any())).optional().describe("每節點配置:{ node_name: { component, recipe?, ...params } }") }, async ({ triplets, context, config }) => { try { if (!env.CYPHER_EXECUTOR) { return { content: [{ type: "text", text: "Error: CYPHER_EXECUTOR service binding is not configured." }], isError: true }; } // KI-12 修:改打 /cypher/execute(吃 triplets+config),原 /execute 是吃完整 graph 的舊路徑 // KI-15 修:轉發 partner token 給 cypher-executor,讓 recipe expander 能用 ak_ key 抓 KBDB const response = await env.CYPHER_EXECUTOR.fetch("http://cypher-executor/cypher/execute", { method: "POST", headers: { "Content-Type": "application/json", "X-Arcrun-API-Key": partnerToken }, body: JSON.stringify({ triplets, context, config }) }); if (!response.ok) { const errorText = await response.text(); return { content: [{ type: "text", text: `Execution failed: ${errorText}` }], isError: true }; } const result = await response.json(); return { content: [{ type: "text", text: JSON.stringify(result, null, 2) }] }; } catch (error) { return { content: [{ type: "text", text: `Internal Error: ${error instanceof Error ? error.message : String(error)}` }], isError: true }; } } ); }