Skip to content

AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents

3.1 KB
Raw Blame
import { isNil, Permission } from '@activepieces/core-utils'
import { FlowOperationRequest, FlowOperationType, FlowStatus, flowStructureUtil, McpToolContext, McpToolDefinition } from '@activepieces/shared'
import { FastifyBaseLogger } from 'fastify'
import { z } from 'zod'
import { flowService } from '../../flows/flow/flow.service'
import { projectService } from '../../project/project-service'
import { mcpUtils } from './mcp-utils'

const lockAndPublishInput = z.object({
    flowId: z.string(),
})

export const apLockAndPublishTool = ({ mcp, userId }: McpToolContext, log: FastifyBaseLogger): McpToolDefinition => {
    return {
        title: 'ap_lock_and_publish',
        permission: Permission.UPDATE_FLOW_STATUS,
        description: 'Publish and enable the current draft version of a flow. This locks the draft, sets it as the published version, and enables the flow. Returns validation errors if the flow is not ready.',
        inputSchema: {
            flowId: z.string().describe('The id of the flow to publish'),
        },
        annotations: { readOnlyHint: false, destructiveHint: true, idempotentHint: false, openWorldHint: true },
        execute: async (args) => {
            const { flowId } = lockAndPublishInput.parse(args)

            const [flow, project] = await Promise.all([
                flowService(log).getOnePopulated({ id: flowId, projectId: mcp.projectId }),
                projectService(log).getOneOrThrow(mcp.projectId),
            ])
            if (isNil(flow)) {
                return { content: [{ type: 'text', text: '❌ Flow not found' }] }
            }

            const allSteps = flowStructureUtil.getAllSteps(flow.version.trigger)
            const skippedStepNames = flowStructureUtil.getSkippedStepNames({ trigger: flow.version.trigger })
            const invalidSteps = allSteps.filter(s => !s.valid && !skippedStepNames.has(s.name))
            if (invalidSteps.length > 0) {
                const stepList = invalidSteps.map(s => `"${s.name}" (${s.displayName})`).join(', ')
                return {
                    content: [{
                        type: 'text',
                        text: `❌ Flow has invalid steps: ${stepList}. Fix these steps using ap_update_step or ap_update_trigger before publishing.`,
                    }],
                }
            }

            const operation: FlowOperationRequest = {
                type: FlowOperationType.LOCK_AND_PUBLISH,
                request: { status: FlowStatus.ENABLED },
            }

            try {
                await flowService(log).update({
                    id: flow.id,
                    projectId: mcp.projectId,
                    userId,
                    previousFlow: flow,
                    platformId: project.platformId,
                    operation,
                })
                return {
                    content: [{ type: 'text', text: `✅ Flow "${flow.version.displayName}" published and enabled successfully.` }],
                }
            }
            catch (err) {
                return mcpUtils.mcpToolError('Publish failed', err)
            }
        },
    }
}