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.6 KB
Raw Blame
import { isNil, Permission } from '@activepieces/core-utils'
import { FlowRetryStrategy, FlowRunStatus, isFlowRunStateTerminal, McpToolDefinition, ProjectScopedMcpServer } from '@activepieces/shared'
import { FastifyBaseLogger } from 'fastify'
import { z } from 'zod'
import { flowService } from '../../flows/flow/flow.service'
import { flowRunService } from '../../flows/flow-run/flow-run-service'
import { formatRunResult, pollForRunCompletion } from './flow-run-utils'
import { mcpUtils } from './mcp-utils'

const retryStrategyValues = Object.values(FlowRetryStrategy) as [FlowRetryStrategy, ...FlowRetryStrategy[]]

const retryRunInput = z.object({
    flowRunId: z.string().describe('The ID of the failed flow run to retry. Use ap_list_runs to find it.'),
    strategy: z.enum(retryStrategyValues).describe('FROM_FAILED_STEP to resume where it failed, ON_LATEST_VERSION to re-run with the current published flow.'),
})

export const apRetryRunTool = (mcp: ProjectScopedMcpServer, log: FastifyBaseLogger): McpToolDefinition => {
    return {
        title: 'ap_retry_run',
        permission: Permission.WRITE_RUN,
        description: 'Retry a failed flow run. FROM_FAILED_STEP resumes at failure point, ON_LATEST_VERSION re-runs entirely.',
        inputSchema: retryRunInput.shape,
        annotations: { readOnlyHint: false, destructiveHint: true, idempotentHint: false, openWorldHint: true },
        execute: async (args) => {
            try {
                const { flowRunId, strategy } = retryRunInput.parse(args)

                const existingRun = await flowRunService(log).getOneOrThrow({ id: flowRunId, projectId: mcp.projectId })

                if (!isFlowRunStateTerminal({ status: existingRun.status, ignoreInternalError: false })) {
                    return { content: [{ type: 'text', text: `❌ Run is ${existingRun.status} — can only retry runs in a terminal state.` }] }
                }

                if (existingRun.status === FlowRunStatus.SUCCEEDED) {
                    return { content: [{ type: 'text', text: '⚠️ Run already succeeded. Use ap_test_flow to run a new test instead.' }] }
                }

                if (strategy === FlowRetryStrategy.ON_LATEST_VERSION) {
                    const flow = await flowService(log).getOneOrThrow({ id: existingRun.flowId, projectId: mcp.projectId })
                    if (isNil(flow.publishedVersionId)) {
                        return { content: [{ type: 'text', text: '❌ Cannot retry with ON_LATEST_VERSION — this flow has not been published yet. Use ap_lock_and_publish first.' }] }
                    }
                }

                const retriedRun = await flowRunService(log).retry({
                    flowRunId,
                    projectId: mcp.projectId,
                    strategy,
                })

                const completedRun = await pollForRunCompletion(log, retriedRun.id, mcp.projectId)

                if (!isFlowRunStateTerminal({ status: completedRun.status, ignoreInternalError: false })) {
                    return {
                        content: [{
                            type: 'text',
                            text: `⏳ Retry still running after 120s. Run ID: ${completedRun.id} (status: ${completedRun.status}). Use ap_get_run to check results later.`,
                        }],
                    }
                }

                return { content: [{ type: 'text', text: formatRunResult(completedRun) }] }
            }
            catch (err) {
                log.error({ error: err, project: { id: mcp.projectId } }, 'ap_retry_run failed')
                return mcpUtils.mcpToolError('Failed to retry run', err)
            }
        },
    }
}