import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { z } from "zod";
import type { ToolContext } from "../utils/api.js";
const json = (d: unknown) => ({ type: "text" as const, text: JSON.stringify(d, null, 2) });
export function registerWorkflows(server: McpServer, ctx: ToolContext): void {
const { api, requireConfig } = ctx;
server.registerTool(
"fetch_workflows",
{ description: "List workflows in the space." },
async () => {
requireConfig();
const d = await api("/workflows");
return { content: [json(d)] };
}
);
server.registerTool(
"get_workflow",
{ description: "Get a workflow by ID.", inputSchema: { workflow_id: z.string() } },
async ({ workflow_id }) => {
requireConfig();
const d = await api(`/workflows/${workflow_id}`);
return { content: [json(d)] };
}
);
server.registerTool(
"create_workflow",
{
description: "Create a workflow. Pass name and optional stages.",
inputSchema: { name: z.string(), stages: z.array(z.record(z.unknown())).optional() },
},
async (body) => {
requireConfig();
const d = await api("/workflows", { method: "POST", body: JSON.stringify({ workflow: body }) });
return { content: [json(d)] };
}
);
server.registerTool(
"update_workflow",
{
description: "Update a workflow.",
inputSchema: { workflow_id: z.string(), name: z.string().optional(), stages: z.array(z.record(z.unknown())).optional() },
},
async ({ workflow_id, ...rest }) => {
requireConfig();
const d = await api(`/workflows/${workflow_id}`, { method: "PUT", body: JSON.stringify({ workflow: rest }) });
return { content: [json(d)] };
}
);
server.registerTool(
"delete_workflow",
{ description: "Delete a workflow by ID.", inputSchema: { workflow_id: z.string() } },
async ({ workflow_id }) => {
requireConfig();
await api(`/workflows/${workflow_id}`, { method: "DELETE" });
return { content: [json({ success: true })] };
}
);
server.registerTool(
"fetch_workflow_stages",
{ description: "List workflow stages for a workflow.", inputSchema: { workflow_id: z.string() } },
async ({ workflow_id }) => {
requireConfig();
const d = await api(`/workflows/${workflow_id}/workflow_stages`);
return { content: [json(d)] };
}
);
}