import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; import { z } from "zod"; import { Env } from "../types.js"; import { kbdbFetch } from "../lib/kbdb-client.js"; export function registerGetWorkflow(server: McpServer, env: Env, orgNamespace: string) { server.tool( "u6u_get_workflow", "取得指定工作流的 metadata,包含名稱、部署時間與 tag 列表。", { workflow_id: z.string().describe("工作流 ID") }, async ({ workflow_id }) => { try { if (!env.KBDB) { return { content: [{ type: "text", text: "Error: KBDB service binding unavailable" }], isError: true }; } const resp = await kbdbFetch(env, `/records/wf-${encodeURIComponent(workflow_id)}`); if (resp.status === 404) { return { content: [{ type: "text", text: `Error: Workflow '${workflow_id}' not found` }], isError: true }; } if (!resp.ok) { return { content: [{ type: "text", text: `Error querying workflow: ${await resp.text()}` }], isError: true }; } const record = await resp.json<{ slots: { workflow_id: string; name: string; deployed_at: string; org_namespace: string } }>(); if (record.slots.org_namespace !== orgNamespace) { return { content: [{ type: "text", text: `Error: Workflow '${workflow_id}' not found` }], isError: true }; } return { content: [{ type: "text", text: JSON.stringify(record.slots, null, 2) }] }; } catch (error) { return { content: [{ type: "text", text: `Internal Error: ${error instanceof Error ? error.message : String(error)}` }], isError: true }; } } ); }