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.7 KB
Raw Blame
import { Permission } from '@activepieces/core-utils'
import { AppConnectionStatus, McpToolDefinition, ProjectScopedMcpServer } from '@activepieces/shared'
import { FastifyBaseLogger } from 'fastify'
import { z } from 'zod'
import { appConnectionService } from '../../app-connection/app-connection-service/app-connection-service'
import { projectService } from '../../project/project-service'
import { mcpUtils } from './mcp-utils'

const statusEnum = z.enum(Object.values(AppConnectionStatus) as [AppConnectionStatus, ...AppConnectionStatus[]])

const listConnectionsSchema = z.object({
    pieceName: z
        .string()
        .optional()
        .describe(
            'Filter by piece name. Short names like "slack" or "google-drive" are auto-expanded to full format (e.g. "@activepieces/piece-slack"). You can also pass the full name directly.',
        ),
    displayName: z
        .string()
        .optional()
        .describe(
            'Filter by connection display name (partial, case-insensitive match). Use to find a connection by its label, e.g. "My Gmail" or "Slack workspace".',
        ),
    status: z
        .array(statusEnum)
        .optional()
        .describe(
            'Filter by status: ACTIVE (working), MISSING (deleted or inaccessible), ERROR (auth/refresh failed). Omit to return all statuses.',
        ),
})

export const apListConnectionsTool = (mcp: ProjectScopedMcpServer, log: FastifyBaseLogger): McpToolDefinition => {
    return {
        title: 'ap_list_connections',
        permission: Permission.READ_APP_CONNECTION,
        description:
            'List OAuth/app connections in the project. Returns externalId needed for the auth parameter on steps.',
        inputSchema: {
            pieceName: listConnectionsSchema.shape.pieceName,
            displayName: listConnectionsSchema.shape.displayName,
            status: listConnectionsSchema.shape.status,
        },
        annotations: { readOnlyHint: true, destructiveHint: false, idempotentHint: true, openWorldHint: false },
        execute: async (args) => {
            try {
                const params = listConnectionsSchema.parse(args ?? {})
                const project = await projectService(log).getOneOrThrow(mcp.projectId)
                const connections = await appConnectionService(log).list({
                    projectId: mcp.projectId,
                    platformId: project.platformId,
                    cursorRequest: null,
                    scope: undefined,
                    displayName: params.displayName,
                    status: params.status,
                    pieceName: mcpUtils.normalizePieceName(params.pieceName),
                    limit: 200,
                    externalIds: undefined,
                })
                const lines = connections.data.map(c => `- externalId: ${c.externalId} | displayName: "${c.displayName}" | piece: ${c.pieceName} | status: ${c.status} | scope: ${c.scope}`)
                const structured = {
                    connections: connections.data.map(c => ({
                        externalId: c.externalId,
                        displayName: c.displayName,
                        pieceName: c.pieceName,
                        status: c.status,
                        scope: c.scope,
                    })),
                    count: connections.data.length,
                }
                return {
                    content: [{
                        type: 'text',
                        text: `✅ Listed ${lines.length} connection(s):\n${lines.join('\n')}`,
                    }],
                    structuredContent: structured,
                }
            }
            catch (err) {
                return mcpUtils.mcpToolError('Failed to list connections', err)
            }
        },
    }
}