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 { ActivepiecesError, apId, ErrorCode, isNil, tryCatch } from '@activepieces/core-utils'
import { McpToolDefinition, McpToolResult, PopulatedMcpServer } from '@activepieces/shared'
import { FastifyBaseLogger } from 'fastify'
import { platformPlanService } from '../ee/platform/platform-plan/platform-plan.service'
import { rejectedPromiseHandler } from '../helper/promise-handler'
import { trackBillingAndSendTelemetry } from '../platform/billing-and-telemetry'
import { assertCreditsNotExceeded, CreditUsageSource, McpCallCreditConsumptionProperties } from '../platform/billing-provider'
import { projectService } from '../project/project-service'
import { INTERNAL_CHAT_CLIENT_ID } from './oauth/token/mcp-oauth-token.service'

export const mcpUsageTracker = (log: FastifyBaseLogger) => ({
    async resolveCallBilling({ mcp, clientId }: ResolveCallBillingParams): Promise<McpCallBilling> {
        if (clientId === INTERNAL_CHAT_CLIENT_ID) {
            return EXEMPT_FROM_BILLING
        }
        const platformId = await resolvePlatformId({ mcp, log })
        if (isNil(platformId)) {
            log.warn({ mcp: { id: mcp.id } }, '[mcpUsageTracker] Could not tell which platform an MCP server belongs to, so its calls are neither gated nor billed')
            return EXEMPT_FROM_BILLING
        }
        return {
            refusalWhenOutOfCredits: ({ toolName }) => refusalWhenOutOfCredits({ platformId, toolName, log }),
            charge: ({ toolName, projectId }) => {
                rejectedPromiseHandler(chargeCall({ platformId, projectId, toolName, clientId, log }), log)
            },
        }
    },
})

export function withCallBilling({ execute, toolName, projectId, billing }: WithCallBillingParams): McpToolDefinition['execute'] {
    if (!BILLABLE_TOOL_NAMES.includes(toolName)) {
        return execute
    }
    return async (args) => {
        const refusal = await billing.refusalWhenOutOfCredits({ toolName })
        if (!isNil(refusal)) {
            return refusal
        }
        const result = await execute(args)
        if (actionRan(result)) {
            billing.charge({ toolName, projectId })
        }
        return result
    }
}

function actionRan(result: McpToolResult): boolean {
    return result.isError !== true || typeof result.structuredContent?.runId === 'string'
}

async function resolvePlatformId({ mcp, log }: { mcp: PopulatedMcpServer, log: FastifyBaseLogger }): Promise<string | null> {
    if (!isNil(mcp.platformId)) {
        return mcp.platformId
    }
    const { projectId } = mcp
    if (isNil(projectId)) {
        return null
    }
    const { data: project } = await tryCatch(() => projectService(log).getOneOrThrow(projectId))
    return project?.platformId ?? null
}

// A credits lookup that fails for its own reasons must not take the tool down with it, so anything
// that is not a definite "out of credits" lets the call through, as the chat personalization path does.
async function refusalWhenOutOfCredits({ platformId, toolName, log }: RefusalParams): Promise<McpToolResult | null> {
    const { error } = await tryCatch(() => assertCreditsNotExceeded({ platformId, log }))
    if (isNil(error)) {
        return null
    }
    const exhausted = error instanceof ActivepiecesError && error.error.code === ErrorCode.QUOTA_EXCEEDED
    if (!exhausted) {
        log.warn({ error, platform: { id: platformId }, tool: { name: toolName } }, '[mcpUsageTracker] Credits check failed, allowing the call')
        return null
    }
    return {
        content: [{ type: 'text', text: `❌ Out of credits: this platform has used all of its Activepieces credits, so "${toolName}" cannot run. Add credits or upgrade the plan, then try again.` }],
        isError: true,
    }
}

async function chargeCall({ platformId, projectId, toolName, clientId, log }: ChargeCallParams): Promise<void> {
    const { error } = await tryCatch(async () => {
        const platformPlan = await platformPlanService(log).getOrCreateForPlatform(platformId)
        const properties: McpCallCreditConsumptionProperties = { platformId, projectId, toolName, clientId }
        const usage = {
            platformId,
            value: MCP_CALL_CREDITS,
            source: CreditUsageSource.MCP as const,
            idempotencyKey: `mcp:${apId()}`,
            properties,
        }
        await trackBillingAndSendTelemetry({
            log,
            licenseKey: platformPlan.licenseKey,
            credits: usage,
        })
    })
    if (!isNil(error)) {
        log.warn({ error, platform: { id: platformId }, tool: { name: toolName } }, '[mcpUsageTracker] An MCP call was not billed')
    }
}

const EXEMPT_FROM_BILLING: McpCallBilling = {
    refusalWhenOutOfCredits: async () => null,
    charge: () => undefined,
}

export const MCP_CALL_CREDITS = 1

export const BILLABLE_TOOL_NAMES: string[] = ['ap_run_action']

export type McpCallBilling = {
    refusalWhenOutOfCredits: (params: { toolName: string }) => Promise<McpToolResult | null>
    charge: (params: { toolName: string, projectId: string | null }) => void
}

type WithCallBillingParams = {
    execute: McpToolDefinition['execute']
    toolName: string
    projectId: string | null
    billing: McpCallBilling
}

type ResolveCallBillingParams = {
    mcp: PopulatedMcpServer
    clientId: string
}

type RefusalParams = {
    platformId: string
    toolName: string
    log: FastifyBaseLogger
}

type ChargeCallParams = {
    platformId: string
    projectId: string | null
    toolName: string
    clientId: string
    log: FastifyBaseLogger
}