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

2.5 KB
Raw Blame
import { isObject, parseToJsonIfPossible, Permission } from '@activepieces/core-utils'
import { McpToolDefinition, ProjectScopedMcpServer } from '@activepieces/shared'
import { FastifyBaseLogger } from 'fastify'
import { z } from 'zod'
import { executePieceActionRun } from './flow-run-utils'
import { mcpUtils } from './mcp-utils'

export const apRunActionTool = (mcp: ProjectScopedMcpServer, log: FastifyBaseLogger): McpToolDefinition => {
    return {
        title: 'ap_run_action',
        permission: Permission.WRITE_RUN,
        description: 'Execute a single piece action once, without building or saving a flow. Use this for one-shot tasks like "check my inbox" or "send one Slack message". For recurring/triggered work, build a flow with ap_build_flow instead.',
        inputSchema: runActionInput.shape,
        annotations: { readOnlyHint: false, destructiveHint: true, idempotentHint: false, openWorldHint: true },
        execute: async (args) => {
            try {
                const { pieceName, actionName, input, connectionExternalId } = runActionInput.parse(args)
                return await executePieceActionRun({
                    projectId: mcp.projectId,
                    pieceName,
                    actionName,
                    input,
                    connectionExternalId,
                    log,
                })
            }
            catch (err) {
                log.error({ error: err, project: { id: mcp.projectId } }, 'ap_run_action failed')
                return mcpUtils.mcpToolError('Failed to run action', err)
            }
        },
    }
}

export const runActionInput = z.object({
    pieceName: z.string().describe('Piece name, e.g. "slack" or "@activepieces/piece-slack". Use ap_research_pieces to discover.'),
    actionName: z.string().describe('Action to run, e.g. "send_channel_message". Use ap_get_piece_props for the input shape.'),
    input: z.preprocess((value) => {
        if (typeof value !== 'string') {
            return value
        }
        const parsed = parseToJsonIfPossible(value)
        return isObject(parsed) ? parsed : value
    }, z.record(z.string(), z.unknown()).optional()).describe('Fully-resolved input for the action. Keys must match the piece action\'s props. Pass raw values — do NOT wrap in {{...}}. Omit if the action has no props.'),
    connectionExternalId: z.string().optional().describe('externalId from ap_list_connections. Required if the piece needs auth. Pass the plain id, not a template.'),
})