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
TypeScript ai-agentai-agent-toolsai-agentsai-agents-frameworkmcpmcp-servermcp-toolsmcpsn8n-alternativeno-code-automationworkflowworkflow-automationworkflows
import { isNil, Permission } from '@activepieces/core-utils'
import { BranchCondition, BranchExecutionType, FlowActionType, FlowOperationRequest, FlowOperationType, McpToolContext, McpToolDefinition, RouterActionSettings } 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 updateBranchInput = z.object({
flowId: z.string(),
routerStepName: z.string(),
branchIndex: z.number().int().min(0),
branchName: z.string().optional(),
conditions: mcpUtils.BRANCH_CONDITIONS_INPUT_SCHEMA.optional(),
})
export const apUpdateBranchTool = ({ mcp, userId }: McpToolContext, log: FastifyBaseLogger): McpToolDefinition => {
return {
title: 'ap_update_branch',
permission: Permission.WRITE_FLOW,
description: 'Update the conditions and/or name of an existing router branch. Does not affect the steps inside the branch.',
inputSchema: {
flowId: z.string().describe('The id of the flow'),
routerStepName: z.string().describe('The name of the ROUTER step. Use ap_flow_structure to get valid values.'),
branchIndex: z.number().describe('The index of the branch to update (0-based). Cannot update the fallback branch conditions.'),
branchName: z.string().optional().describe('New display name for the branch'),
conditions: mcpUtils.BRANCH_CONDITIONS_INPUT_SCHEMA.optional().describe('New conditions array (outer array = OR groups, inner array = AND conditions). Replaces the existing conditions entirely.'),
},
annotations: { readOnlyHint: false, destructiveHint: true, idempotentHint: true, openWorldHint: false },
execute: async (args) => {
try {
const { flowId, routerStepName, branchIndex, branchName, conditions } = updateBranchInput.parse(args)
if (isNil(branchName) && isNil(conditions)) {
return { content: [{ type: 'text', text: '❌ Nothing to update. Provide at least branchName or conditions.' }] }
}
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 rewritten = mcpUtils.rewriteAllReferences({ conditions, trigger: flow.version.trigger })
const resolved = mcpUtils.resolveRouterStep({ stepName: routerStepName, trigger: flow.version.trigger })
if (resolved.error) {
return resolved.error
}
const routerStep = resolved.routerStep
if (routerStep.type === FlowActionType.AI_ROUTER) {
return { content: [{ type: 'text', text: `❌ "${routerStepName}" is an AI Router. Its routes have descriptions, not conditions, and can only be edited in the builder for now.` }] }
}
const branches = [...routerStep.settings.branches]
if (branchIndex < 0 || branchIndex >= branches.length) {
return {
content: [{
type: 'text',
text: `❌ branchIndex ${branchIndex} is out of range. Valid indices are 0–${branches.length - 1}.`,
}],
}
}
const targetBranch = branches[branchIndex]
if (targetBranch.branchType === BranchExecutionType.FALLBACK) {
if (conditions !== undefined) {
return {
content: [{
type: 'text',
text: `❌ Cannot set conditions on the fallback branch (index ${branchIndex}). Only branchName can be updated for fallback branches.`,
}],
}
}
branches[branchIndex] = {
...targetBranch,
...(branchName !== undefined && { branchName }),
}
}
else {
branches[branchIndex] = {
...targetBranch,
...(branchName !== undefined && { branchName }),
// .min(1) and .superRefine on the input schema align the runtime shape with BranchCondition's discriminated union.
...(rewritten.conditions !== undefined && { conditions: rewritten.conditions as BranchCondition[][] }),
}
}
const updatedSettings: RouterActionSettings = {
...routerStep.settings,
branches,
}
const operation: FlowOperationRequest = {
type: FlowOperationType.UPDATE_ACTION,
request: {
type: FlowActionType.ROUTER,
name: routerStep.name,
displayName: routerStep.displayName,
valid: routerStep.valid,
settings: updatedSettings,
},
}
const updatedFlow = await flowService(log).update({
id: flow.id,
projectId: mcp.projectId,
userId,
previousFlow: flow,
platformId: project.platformId,
operation,
})
const updatedParts: string[] = []
if (branchName !== undefined) updatedParts.push(`name → "${branchName}"`)
if (conditions !== undefined) updatedParts.push(`conditions (${conditions.length} OR group(s))`)
const invalidWarning = mcpUtils.routerInvalidWarning({ stepName: routerStepName, trigger: updatedFlow.version.trigger })
return {
content: [{
type: 'text',
text: `✅ Branch ${branchIndex} of router "${routerStepName}" updated: ${updatedParts.join(', ')}. Steps inside the branch are unchanged.${invalidWarning}`,
}],
}
}
catch (err) {
return mcpUtils.mcpToolError('Update branch failed', err)
}
},
}
}