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

fix(mcp): bill only ap_run_action, after the action runs (#16154)

Abdul999a75fdcca9
diff --git a/brain/knowledge/decisions/000047-mcp-bills-only-ap-run-action.md b/brain/knowledge/decisions/000047-mcp-bills-only-ap-run-action.md
new file mode 100644
index 00000000000..df7197883c6
--- /dev/null
+++ b/brain/knowledge/decisions/000047-mcp-bills-only-ap-run-action.md
@@ -0,0 +1,23 @@
+---
+title: MCP bills only ap_run_action
+icon: 🧾
+status: accepted
+---
+
+# MCP bills only ap_run_action
+
+## Decision
+
+An external MCP client pays 1 credit for `ap_run_action`, charged after the action job runs, also when the third-party call failed. Every other MCP tool is free and skips the out-of-credits check. A flow tool pays no MCP credit, because its production run is already billed. MCP calls do not touch the AppSumo AI-credit meter. This narrows the flat 1-credit-per-call charge from #15492.
+
+## Context
+
+#15492 charged every MCP call 1 credit, before the tool ran. Building one flow over MCP cost about 12 credits, while the same build in the builder or the in-app chat costs nothing. Out of credits, a client could not even list flows or turn one off. Flow tools were charged twice: once by MCP, once by the run meter.
+
+## Why
+
+An MCP call costs what the same work costs in the builder: runs and AI cost credits, building does not. `ap_run_action` is the only tool that runs work no other meter sees, so it keeps a flat credit, the same as chat's `ap_execute_action` (000042). The rejected option was to keep the flat per-call charge and lower its price; it still charges the channel we want customers to adopt and teaches models to skip validation.
+
+## Consequences
+
+`BILLABLE_TOOL_NAMES` in `mcpUsageTracker` lists the billed tools; a new tool is free unless added there. `ap_run_action` charges when its result succeeded or carries a `runId`, so a refusal before dispatch costs nothing. Five tools that run sandbox jobs (`ap_get_piece_props`, `ap_resolve_property_options`, `ap_resolve_property_chain`, `ap_test_flow`, `ap_test_step`) are free and watched through `MCP_TOOL_CALLED` telemetry.
diff --git a/docs/mcp/overview.mdx b/docs/mcp/overview.mdx
index 797aa243608..6def49880b5 100644
--- a/docs/mcp/overview.mdx
+++ b/docs/mcp/overview.mdx
@@ -61,6 +61,10 @@ Tools are organized into categories. **Discovery tools** are always available. O
See the [Tools Reference](/mcp/tools) for the complete catalog with input schemas, and [Tool Search](/mcp/tool-search) for how agents find the right action or trigger by describing a task in plain language.
+<Note>
+ MCP tools are free, except `ap_run_action`, which costs 1 credit per call. A flow that a tool call starts is billed as a normal flow run.
+</Note>
+
## Self-Hosting Behind a Reverse Proxy
The MCP server and its OAuth endpoints are reachable both at the host root (`https://your-instance.com/mcp`) and under a path prefix (`https://your-instance.com/<prefix>/mcp`) when your reverse proxy routes `https://host/<prefix>/*` to Activepieces.
diff --git a/packages/server/api/src/app/action-run/action-run-outcome.ts b/packages/server/api/src/app/action-run/action-run-outcome.ts
index ca90b15c2fb..ae724544056 100644
--- a/packages/server/api/src/app/action-run/action-run-outcome.ts
+++ b/packages/server/api/src/app/action-run/action-run-outcome.ts
@@ -34,12 +34,13 @@ function isWatcherTimeout(error: unknown): boolean {
export function deriveActionRunOutcome({ result }: { result: Result<EngineActionResponse, unknown> }): ActionRunOutcome {
if (!isNil(result.error) || isNil(result.data)) {
+ const watcherTimedOut = isWatcherTimeout(result.error)
return {
- status: isWatcherTimeout(result.error) ? FlowRunStatus.TIMEOUT : FlowRunStatus.INTERNAL_ERROR,
+ status: watcherTimedOut ? FlowRunStatus.TIMEOUT : FlowRunStatus.INTERNAL_ERROR,
output: null,
logs: null,
errorMessage: result.error instanceof Error ? result.error.message : String(result.error),
- neverStarted: false,
+ neverStarted: !watcherTimedOut,
}
}
const engineResponse = result.data
diff --git a/packages/server/api/src/app/mcp/mcp-server-builder.ts b/packages/server/api/src/app/mcp/mcp-server-builder.ts
index e1404f2795d..be57617c0b0 100644
--- a/packages/server/api/src/app/mcp/mcp-server-builder.ts
+++ b/packages/server/api/src/app/mcp/mcp-server-builder.ts
@@ -13,7 +13,7 @@ import { mcpAccess } from './mcp-access'
import { ALLOW_ALL, deny, PermissionChecker, resolveMcpPermissionChecker, resolvePermissionChecker } from './mcp-permissions'
import { mcpProjectSelection, ProjectSelectionScope } from './mcp-project-selection'
import { mcpToolInput } from './mcp-tool-input'
-import { McpCallBilling, mcpUsageTracker } from './mcp-usage-tracker'
+import { McpCallBilling, mcpUsageTracker, withCallBilling } from './mcp-usage-tracker'
import { activepiecesTools, ALL_CONTROLLABLE_TOOL_NAMES, LOCKED_TOOL_NAMES, PLATFORM_LEVEL_TOOL_NAMES } from './tools'
import { apSetProjectContextTool, SET_PROJECT_CONTEXT_TOOL_NAME } from './tools/ap-set-project-context'
@@ -106,14 +106,14 @@ function registerPlatformTools({ server, mcp, platformId, userId, clientKey, sel
log: FastifyBaseLogger
}): void {
const contextTool = apSetProjectContextTool({ platformId, userId, selectionScope, log })
- server.registerTool(contextTool.title, buildToolConfig(contextTool), (args: Record<string, unknown>) => withMcpReach({ execute: charged({ execute: contextTool.execute, toolName: contextTool.title, projectId: null, billing }), toolTitle: contextTool.title, platformId, userId, log })(args))
+ server.registerTool(contextTool.title, buildToolConfig(contextTool), (args: Record<string, unknown>) => withMcpReach({ execute: withCallBilling({ execute: contextTool.execute, toolName: contextTool.title, projectId: null, billing }), toolTitle: contextTool.title, platformId, userId, log })(args))
const templateMcp: ProjectScopedMcpServer = { ...mcp, projectId: platformId }
const tools = filterEnabledTools({ tools: activepiecesTools(templateMcp, userId, log), disabledTools: mcp.disabledTools })
tools.forEach((tool) => {
if (PLATFORM_LEVEL_TOOL_SET.has(tool.title)) {
- server.registerTool(tool.title, buildToolConfig(tool), (args: Record<string, unknown>) => withMcpReach({ execute: charged({ execute: tool.execute, toolName: tool.title, projectId: null, billing }), toolTitle: tool.title, platformId, userId, log })(args))
+ server.registerTool(tool.title, buildToolConfig(tool), (args: Record<string, unknown>) => withMcpReach({ execute: withCallBilling({ execute: tool.execute, toolName: tool.title, projectId: null, billing }), toolTitle: tool.title, platformId, userId, log })(args))
return
}
@@ -153,7 +153,7 @@ async function executeInSelectedProject({ toolTitle, args, projectId, userId, re
}
const execute = permissionChecker.wrapExecute({
execute: isToolEnabled({ toolTitle: realTool.title, disabledTools: projectMcp.disabledTools })
- ? charged({ execute: realTool.execute, toolName: realTool.title, projectId, billing })
+ ? withCallBilling({ execute: realTool.execute, toolName: realTool.title, projectId, billing })
: async () => toolSwitchedOffResult(realTool.title),
permission: realTool.permission,
toolTitle: realTool.title,
@@ -217,7 +217,6 @@ function registerFlowTools({ server, mcp, projectId, permissionChecker, billing,
if (!isNil(refusal)) {
return refusal
}
- billing.charge({ toolName, projectId })
const result = await runFlowAsTool({ flow, properties: mcpInputs, payload: args, returnsResponse, log })
@@ -298,7 +297,7 @@ function registerStaticTools({ server, mcp, projectId, userId, platformDisabledT
tools.forEach((tool) => {
const execute = permissionChecker.wrapExecute({
- execute: charged({ execute: tool.execute, toolName: tool.title, projectId, billing }),
+ execute: withCallBilling({ execute: tool.execute, toolName: tool.title, projectId, billing }),
permission: tool.permission,
toolTitle: tool.title,
})
@@ -307,22 +306,6 @@ function registerStaticTools({ server, mcp, projectId, userId, platformDisabledT
})
}
-function charged({ execute, toolName, projectId, billing }: {
- execute: McpToolDefinition['execute']
- toolName: string
- projectId: string | null
- billing: McpCallBilling
-}): McpToolDefinition['execute'] {
- return async (args) => {
- const refusal = await billing.refusalWhenOutOfCredits({ toolName })
- if (!isNil(refusal)) {
- return refusal
- }
- billing.charge({ toolName, projectId })
- return execute(args)
- }
-}
-
function registerPlaceholderTools(server: McpServer): void {
const lockedToolSet = new Set<string>(LOCKED_TOOL_NAMES)
const allToolNames = [...LOCKED_TOOL_NAMES, ...ALL_CONTROLLABLE_TOOL_NAMES]
diff --git a/packages/server/api/src/app/mcp/mcp-usage-tracker.ts b/packages/server/api/src/app/mcp/mcp-usage-tracker.ts
index 85067ae42ee..664495e6aa1 100644
--- a/packages/server/api/src/app/mcp/mcp-usage-tracker.ts
+++ b/packages/server/api/src/app/mcp/mcp-usage-tracker.ts
@@ -1,5 +1,5 @@
import { ActivepiecesError, apId, ErrorCode, isNil, tryCatch } from '@activepieces/core-utils'
-import { isAppSumoCreditedPlan, McpToolResult, PopulatedMcpServer } from '@activepieces/shared'
+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'
@@ -27,6 +27,27 @@ export const mcpUsageTracker = (log: FastifyBaseLogger) => ({
},
})
+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
@@ -72,7 +93,6 @@ async function chargeCall({ platformId, projectId, toolName, clientId, log }: Ch
log,
licenseKey: platformPlan.licenseKey,
credits: usage,
- ...(isAppSumoCreditedPlan(platformPlan.plan) ? { appSumo: { ...usage, idempotencyKey: `mcpAppSumo:${apId()}` } } : {}),
})
})
if (!isNil(error)) {
@@ -87,11 +107,20 @@ const EXEMPT_FROM_BILLING: McpCallBilling = {
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
diff --git a/packages/server/api/src/app/mcp/tools/flow-run-utils.ts b/packages/server/api/src/app/mcp/tools/flow-run-utils.ts
index e5e33963127..afd37da491d 100644
--- a/packages/server/api/src/app/mcp/tools/flow-run-utils.ts
+++ b/packages/server/api/src/app/mcp/tools/flow-run-utils.ts
@@ -250,7 +250,7 @@ export async function executePieceActionRun({
type: 'text',
text: `⏳ ${action.displayName} timed out before it finished. Run ID: ${actionRun.id}. It may have partially completed, so do not re-run it blindly — check the app for a write from this attempt first, or ask the user.`,
}],
- structuredContent: { errorSummary: 'The action timed out before it finished and may have partially completed.' },
+ structuredContent: { errorSummary: 'The action timed out before it finished and may have partially completed.', runId: actionRun.id },
isError: true,
}
}
@@ -261,7 +261,7 @@ export async function executePieceActionRun({
type: 'text',
text: `❌ ${action.displayName} failed with INTERNAL_ERROR — the engine crashed while loading or executing the piece. Run ID: ${actionRun.id}.`,
}],
- structuredContent: { errorSummary: 'The step couldn’t start — something went wrong loading it.' },
+ structuredContent: { errorSummary: 'The step couldn\'t start — something went wrong loading it.', ...(actionRun.neverStarted ? {} : { runId: actionRun.id }) },
isError: true,
}
}
@@ -487,7 +487,7 @@ export function formatPieceActionRunResult({ outcome, runId, displayName, action
const summary = isNil(outcome.errorMessage) ? 'The step failed without an error message.' : summarizeActionError(outcome.errorMessage)
return {
content: [{ type: 'text', text: `❌ ${displayName} failed (run ${runId}): ${summary}\n\nRetry suggestion: Check the error above. If it mentions missing criteria, try adding a broad filter (e.g., after_date with a recent date, or a common search term). If it mentions auth, verify the connection.` }],
- structuredContent: { errorSummary: summary },
+ structuredContent: { errorSummary: summary, runId },
isError: true,
}
}
diff --git a/packages/server/api/test/integration/cloud/mcp/mcp-reach-before-billing.test.ts b/packages/server/api/test/integration/cloud/mcp/mcp-reach-before-billing.test.ts
index 97398c1f263..7f4c5c70866 100644
--- a/packages/server/api/test/integration/cloud/mcp/mcp-reach-before-billing.test.ts
+++ b/packages/server/api/test/integration/cloud/mcp/mcp-reach-before-billing.test.ts
@@ -21,6 +21,7 @@ let app: FastifyInstance
const SET_PROJECT_CONTEXT = 'ap_set_project_context'
const PIECE_CATALOG_TOOL = 'ap_research_pieces'
+const RUN_ACTION = 'ap_run_action'
beforeAll(async () => {
app = await setupTestEnvironment()
@@ -76,13 +77,25 @@ describe('a denied MCP call is refused before the platform is charged', () => {
expect(answer).not.toContain('Out of credits')
})
- it('still refuses a member who does have reach on credits', async () => {
+ it('lets a member with reach use a free tool when credits run out', async () => {
const ctx = await createTestContext(app)
const member = await createMemberContext(app, ctx, { projectRole: DefaultProjectRole.EDITOR })
const mcpClient = await connectAs({ ctx: member })
const answer = await call({ mcpClient, name: SET_PROJECT_CONTEXT })
+ expect(answer).toContain('Available projects')
+ expect(answer).not.toContain('Out of credits')
+ })
+
+ it('still refuses ap_run_action for a member with reach on credits', async () => {
+ const ctx = await createTestContext(app)
+ const member = await createMemberContext(app, ctx, { projectRole: DefaultProjectRole.EDITOR })
+ const mcpClient = await connectAs({ ctx: member })
+ await mcpClientHelpers.callTool({ app, mcpClient, name: SET_PROJECT_CONTEXT, args: { projectId: ctx.project.id } })
+
+ const answer = await mcpClientHelpers.callTool({ app, mcpClient, name: RUN_ACTION, args: { pieceName: 'slack', actionName: 'send_channel_message' } })
+
expect(answer).toContain('Out of credits')
})
})
diff --git a/packages/server/api/test/integration/cloud/mcp/mcp-switched-off-billing.test.ts b/packages/server/api/test/integration/cloud/mcp/mcp-switched-off-billing.test.ts
index 4d843cccfac..7de0ce3d4e9 100644
--- a/packages/server/api/test/integration/cloud/mcp/mcp-switched-off-billing.test.ts
+++ b/packages/server/api/test/integration/cloud/mcp/mcp-switched-off-billing.test.ts
@@ -18,7 +18,8 @@ vi.mock('../../../../src/app/platform/billing-and-telemetry', () => ({
let app: FastifyInstance
-const SWITCHED_OFF_TOOL = 'ap_create_flow'
+const SWITCHED_OFF_TOOL = 'ap_run_action'
+const FREE_TOOL = 'ap_create_flow'
beforeAll(async () => {
app = await setupTestEnvironment()
@@ -69,21 +70,29 @@ async function createRestrictedProject({ ctx }: { ctx: TestContext }): Promise<s
return project.id
}
-describe('a tool the selected project switched off', () => {
- it('is not charged, while the same tool is charged where it is on', async () => {
+describe('MCP tool billing per project', () => {
+ it('does not charge a billable tool the selected project switched off', async () => {
const ctx = await createTestContext(app)
const restrictedProjectId = await createRestrictedProject({ ctx })
const mcpClient = await connectAs({ ctx })
await call({ mcpClient, name: 'ap_set_project_context', args: { projectId: restrictedProjectId } })
- const refused = await call({ mcpClient, name: SWITCHED_OFF_TOOL, args: { flowName: 'blocked-flow' } })
- await call({ mcpClient, name: 'ap_set_project_context', args: { projectId: ctx.project.id } })
- await call({ mcpClient, name: SWITCHED_OFF_TOOL, args: { flowName: 'allowed-flow' } })
+ const refused = await call({ mcpClient, name: SWITCHED_OFF_TOOL, args: { pieceName: 'slack', actionName: 'send_channel_message' } })
- await vi.waitFor(() => expect(chargedProjectIds({ toolName: SWITCHED_OFF_TOOL })).toContain(ctx.project.id))
expect(refused).toContain('switched off')
expect(chargedProjectIds({ toolName: SWITCHED_OFF_TOOL })).not.toContain(restrictedProjectId)
})
+
+ it('does not charge a free tool where it is on', async () => {
+ const ctx = await createTestContext(app)
+ const mcpClient = await connectAs({ ctx })
+
+ await call({ mcpClient, name: 'ap_set_project_context', args: { projectId: ctx.project.id } })
+ const created = await call({ mcpClient, name: FREE_TOOL, args: { flowName: 'allowed-flow' } })
+
+ expect(created).not.toContain('switched off')
+ expect(mockTrackBilling).not.toHaveBeenCalled()
+ })
})
type TrackedBilling = {
diff --git a/packages/server/api/test/unit/app/action-run/action-run-outcome.test.ts b/packages/server/api/test/unit/app/action-run/action-run-outcome.test.ts
index 1bdb3fe3339..dd416b0b961 100644
--- a/packages/server/api/test/unit/app/action-run/action-run-outcome.test.ts
+++ b/packages/server/api/test/unit/app/action-run/action-run-outcome.test.ts
@@ -39,6 +39,7 @@ describe('deriveActionRunOutcome', () => {
it('error channel maps to INTERNAL_ERROR', () => {
const outcome = deriveActionRunOutcome({ result: { data: null, error: new Error('boom') } })
expect(outcome.status).toBe(FlowRunStatus.INTERNAL_ERROR)
+ expect(outcome.neverStarted).toBe(true)
})
it('watcher timeout maps to TIMEOUT, not INTERNAL_ERROR', () => {
const error = new ActivepiecesError({
diff --git a/packages/server/api/test/unit/app/mcp/flow-run-utils.test.ts b/packages/server/api/test/unit/app/mcp/flow-run-utils.test.ts
index a7abf993905..e427d047632 100644
--- a/packages/server/api/test/unit/app/mcp/flow-run-utils.test.ts
+++ b/packages/server/api/test/unit/app/mcp/flow-run-utils.test.ts
@@ -28,6 +28,7 @@ describe('piece action run result', () => {
expect(result.content[0].text).toContain('❌')
expect(result.content[0].text).toContain('failed (run run-1)')
expect(result.structuredContent?.errorSummary).toContain('invalid_auth')
+ expect(result.structuredContent?.runId).toBe('run-1')
})
it('flags a failed run with no error message', () => {
diff --git a/packages/server/api/test/unit/app/mcp/mcp-usage-tracker.test.ts b/packages/server/api/test/unit/app/mcp/mcp-usage-tracker.test.ts
index f74070dedc0..df1ed707bd4 100644
--- a/packages/server/api/test/unit/app/mcp/mcp-usage-tracker.test.ts
+++ b/packages/server/api/test/unit/app/mcp/mcp-usage-tracker.test.ts
@@ -1,8 +1,8 @@
import { ActivepiecesError, ErrorCode, PlatformUsageMetric } from '@activepieces/core-utils'
-import { McpServerType, PopulatedMcpServer } from '@activepieces/shared'
+import { McpServerType, McpToolResult, PopulatedMcpServer } from '@activepieces/shared'
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { INTERNAL_CHAT_CLIENT_ID } from '../../../../src/app/mcp/oauth/token/mcp-oauth-token.service'
-import { MCP_CALL_CREDITS, mcpUsageTracker } from '../../../../src/app/mcp/mcp-usage-tracker'
+import { MCP_CALL_CREDITS, McpCallBilling, mcpUsageTracker, withCallBilling } from '../../../../src/app/mcp/mcp-usage-tracker'
const { mockTrackBillingAndSendTelemetry, mockGetOrCreateForPlatform, mockGetProject, mockAssertCredits } = vi.hoisted(() => ({
mockTrackBillingAndSendTelemetry: vi.fn().mockResolvedValue(undefined),
@@ -113,13 +113,14 @@ describe('mcpUsageTracker.resolveCallBilling — charging', () => {
expect(first[0].credits.idempotencyKey).not.toBe(second[0].credits.idempotencyKey)
})
- it('charges AppSumo credits alongside the platform balance on a credited plan', async () => {
+ it('leaves the AppSumo AI-credit meter alone on a credited plan, as a production run does', async () => {
mockGetOrCreateForPlatform.mockResolvedValue({ plan: 'appsumo_activepieces_tier1', licenseKey: null })
await chargeOnce({ mcp: mcpServer({ platformId: 'platform-2', type: McpServerType.PLATFORM }), clientId: EXTERNAL_CLIENT_ID, projectId: null })
- const { appSumo } = mockTrackBillingAndSendTelemetry.mock.calls[0][0]
- expect(appSumo?.value).toBe(MCP_CALL_CREDITS)
+ const { appSumo, credits } = mockTrackBillingAndSendTelemetry.mock.calls[0][0]
+ expect(appSumo).toBeUndefined()
+ expect(credits.value).toBe(MCP_CALL_CREDITS)
})
it('lets the call through when billing fails, rather than failing the tool', async () => {
@@ -193,3 +194,71 @@ describe('mcpUsageTracker.resolveCallBilling — refusing a client with no credi
expect(mockAssertCredits).toHaveBeenCalledWith(expect.objectContaining({ platformId: 'platform-1' }))
})
})
+
+function fakeBilling({ refusal = null }: { refusal?: McpToolResult | null } = {}) {
+ const charge = vi.fn()
+ const refusalWhenOutOfCredits = vi.fn().mockResolvedValue(refusal)
+ const billing: McpCallBilling = { charge, refusalWhenOutOfCredits }
+ return { billing, charge, refusalWhenOutOfCredits }
+}
+
+function billedCall({ toolName, result, billing }: { toolName: string, result: McpToolResult, billing: McpCallBilling }) {
+ const execute = vi.fn().mockResolvedValue(result)
+ return { execute, call: withCallBilling({ execute, toolName, projectId: 'project-1', billing }) }
+}
+
+const RAN: McpToolResult = { content: [{ type: 'text', text: '✅ Send Message completed (run run-1).' }] }
+const FAILED_AT_VENDOR: McpToolResult = { content: [{ type: 'text', text: '❌ failed' }], structuredContent: { errorSummary: '401', runId: 'run-1' }, isError: true }
+const REFUSED_BEFORE_DISPATCH: McpToolResult = { content: [{ type: 'text', text: '❌ Piece not found' }], isError: true }
+
+describe('withCallBilling — which calls cost a credit', () => {
+ it('runs a free tool with no credit check and no charge, even when the platform is out of credits', async () => {
+ const { billing, charge, refusalWhenOutOfCredits } = fakeBilling({ refusal: REFUSED_BEFORE_DISPATCH })
+ const { execute, call } = billedCall({ toolName: 'ap_list_flows', result: RAN, billing })
+
+ await call({})
+
+ expect(execute).toHaveBeenCalled()
+ expect(refusalWhenOutOfCredits).not.toHaveBeenCalled()
+ expect(charge).not.toHaveBeenCalled()
+ })
+
+ it('charges ap_run_action once, after the action ran', async () => {
+ const { billing, charge } = fakeBilling()
+ const { call } = billedCall({ toolName: 'ap_run_action', result: RAN, billing })
+
+ await call({})
+
+ expect(charge).toHaveBeenCalledTimes(1)
+ expect(charge).toHaveBeenCalledWith({ toolName: 'ap_run_action', projectId: 'project-1' })
+ })
+
+ it('charges ap_run_action when the third-party call failed, because the run happened', async () => {
+ const { billing, charge } = fakeBilling()
+ const { call } = billedCall({ toolName: 'ap_run_action', result: FAILED_AT_VENDOR, billing })
+
+ await call({})
+
+ expect(charge).toHaveBeenCalledTimes(1)
+ })
+
+ it('charges nothing when ap_run_action refused before it ran anything', async () => {
+ const { billing, charge } = fakeBilling()
+ const { call } = billedCall({ toolName: 'ap_run_action', result: REFUSED_BEFORE_DISPATCH, billing })
+
+ await call({})
+
+ expect(charge).not.toHaveBeenCalled()
+ })
+
+ it('refuses ap_run_action without running it once the platform is out of credits', async () => {
+ const { billing, charge } = fakeBilling({ refusal: REFUSED_BEFORE_DISPATCH })
+ const { execute, call } = billedCall({ toolName: 'ap_run_action', result: RAN, billing })
+
+ const result = await call({})
+
+ expect(result).toBe(REFUSED_BEFORE_DISPATCH)
+ expect(execute).not.toHaveBeenCalled()
+ expect(charge).not.toHaveBeenCalled()
+ })
+})