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

5.4 KB
Raw Blame
import { isNil, Permission } from '@activepieces/core-utils'
import { FieldType, McpToolDefinition, ProjectScopedMcpServer } from '@activepieces/shared'
import { FastifyBaseLogger } from 'fastify'
import { z } from 'zod'
import { fieldService } from '../../tables/field/field.service'
import { mcpUtils } from './mcp-utils'
import { fieldTypeSchema, formatFieldInfo, resolveInternalTableId, tableNotFoundError } from './table-utils'

const manageFieldsInput = z.object({
    tableId: z.string().describe('The table ID'),
    operation: z.enum(['ADD', 'UPDATE', 'DELETE']).describe('ADD a new field, UPDATE (rename) an existing field, or DELETE a field'),
    fieldId: z.string().optional().describe('The field ID (required for UPDATE and DELETE). Use ap_list_tables to find it.'),
    name: z.string().optional().describe('Field name (required for ADD and UPDATE)'),
    type: fieldTypeSchema.optional().describe('Field type (required for ADD only)'),
    options: z.array(z.string()).optional().describe('Dropdown options (required for ADD with STATIC_DROPDOWN type)'),
})

export const apManageFieldsTool = (mcp: ProjectScopedMcpServer, log: FastifyBaseLogger): McpToolDefinition => {
    return {
        title: 'ap_manage_fields',
        permission: Permission.WRITE_TABLE,
        description: 'Add, rename, or delete fields on a table. Max 100 fields per table.',
        inputSchema: manageFieldsInput.shape,
        annotations: { readOnlyHint: false, destructiveHint: true, idempotentHint: false, openWorldHint: false },
        execute: async (args) => {
            try {
                const { tableId, operation, fieldId, name, type, options } = manageFieldsInput.parse(args)

                const resolvedTableId = await resolveInternalTableId({ projectId: mcp.projectId, tableId })
                if (isNil(resolvedTableId)) {
                    return tableNotFoundError(tableId)
                }

                switch (operation) {
                    case 'ADD': {
                        if (isNil(name)) {
                            return { content: [{ type: 'text', text: '❌ name is required for ADD operation' }] }
                        }
                        if (isNil(type)) {
                            return { content: [{ type: 'text', text: '❌ type is required for ADD operation' }] }
                        }
                        if (type === FieldType.STATIC_DROPDOWN && (isNil(options) || options.length === 0)) {
                            return { content: [{ type: 'text', text: '❌ options are required for STATIC_DROPDOWN type' }] }
                        }

                        const request = type === FieldType.STATIC_DROPDOWN
                            ? { name, type, tableId: resolvedTableId, data: { options: (options ?? []).map(v => ({ value: v })) } }
                            : { name, type, tableId: resolvedTableId }

                        const field = await fieldService.create({
                            projectId: mcp.projectId,
                            request,
                        })
                        return { content: [{ type: 'text', text: `✅ Field added: ${formatFieldInfo(field)}` }] }
                    }
                    case 'UPDATE': {
                        if (isNil(fieldId)) {
                            return { content: [{ type: 'text', text: '❌ fieldId is required for UPDATE operation' }] }
                        }
                        if (isNil(name)) {
                            return { content: [{ type: 'text', text: '❌ name is required for UPDATE operation' }] }
                        }
                        const existing = await fieldService.getById({ id: fieldId, projectId: mcp.projectId })
                        if (existing.tableId !== resolvedTableId) {
                            return { content: [{ type: 'text', text: `❌ Field (id: ${fieldId}) does not belong to table (id: ${resolvedTableId})` }] }
                        }
                        const field = await fieldService.update({
                            id: fieldId,
                            projectId: mcp.projectId,
                            request: { name },
                        })
                        return { content: [{ type: 'text', text: `✅ Field renamed: ${formatFieldInfo(field)}` }] }
                    }
                    case 'DELETE': {
                        if (isNil(fieldId)) {
                            return { content: [{ type: 'text', text: '❌ fieldId is required for DELETE operation' }] }
                        }
                        const toDelete = await fieldService.getById({ id: fieldId, projectId: mcp.projectId })
                        if (toDelete.tableId !== resolvedTableId) {
                            return { content: [{ type: 'text', text: `❌ Field (id: ${fieldId}) does not belong to table (id: ${resolvedTableId})` }] }
                        }
                        await fieldService.delete({
                            id: fieldId,
                            projectId: mcp.projectId,
                        })
                        return { content: [{ type: 'text', text: `✅ Field "${toDelete.name}" deleted successfully.` }] }
                    }
                }
            }
            catch (err) {
                log.error({ error: err, project: { id: mcp.projectId } }, 'ap_manage_fields failed')
                return mcpUtils.mcpToolError('Field operation failed', err)
            }
        },
    }
}