You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
description: "Use mppx to let a Cloudflare Agent pay for MPP-enabled MCP tools."
3
+
imageDescription: "Connect Cloudflare Agents to paid MPP MCP tools"
4
+
---
5
+
6
+
# Cloudflare Agents [Pay for MCP tools from your agent]
7
+
8
+
Use [`McpClient.wrap`](/sdk/typescript/client/McpClient.wrap) to let a [Cloudflare Agent](https://developers.cloudflare.com/agents/) call free and paid MCP tools through the same MCP client. Free tools pass through untouched. When a paid tool returns an MPP Challenge, the wrapped client creates a Credential, retries the call, and returns the result—the Agent never needs to know which tools are paid.
9
+
10
+
## Install
11
+
12
+
:::code-group
13
+
```bash [npm]
14
+
$ npm install agents mppx accounts
15
+
```
16
+
```bash [pnpm]
17
+
$ pnpm add agents mppx accounts
18
+
```
19
+
```bash [bun]
20
+
$ bun add agents mppx accounts
21
+
```
22
+
:::
23
+
24
+
## Wrap the MCP client
25
+
26
+
Connect an MCP server in `onStart`, then inject payment handling into the connected client. Payments are authorized inside your wallet through the `wallet_authorizeChallenge` RPC using your [Tempo Accounts](https://accounts.tempo.xyz/docs) access keys—mppx discovers wallet support via `wallet_getCapabilities` and falls back to local signing for accounts that aren't wallet-backed.
onPaymentRequired: (challenge) =>Number(challenge.request.amount) <1_000_000, // approve based on amount/intent
43
+
})
44
+
}
45
+
}
46
+
```
47
+
48
+
`McpClient.wrap` mutates the client in place, so there is nothing to store: every surface built on the connection—including `this.mcp.getAITools(...)` and `this.mcp.callTool(...)`—stays payment-aware. Paid calls expose the MPP Receipt on `result.receipt`. `onPaymentRequired` is the spend-policy / human-in-the-loop hook: it runs before each Credential is created, and returning (or resolving) `false` declines the payment—`callTool` rejects with `Payment declined.`
49
+
50
+
:::note
51
+
If you also use Cloudflare's `withX402Client`, apply `McpClient.wrap` first—each wrapper then handles only its own protocol's challenges (the reverse order breaks tool calls).
52
+
:::
53
+
54
+
## Accounts access keys
55
+
56
+
`connectWallet` runs a headless [Tempo Accounts](https://accounts.tempo.xyz/docs) wallet next to the Agent: the `secp256k1` adapter pins a server-side key, `wallet_connect` authorizes an access key, and wrapping the provider in a viem client gives `tempo(...)` a `json-rpc` account whose challenges route to the wallet:
Access keys are chain-scoped—the authorized chain must match the chain ID in the MPP Challenge. `Storage.memory()` re-authorizes a key on each cold start; wrap KV or a Durable Object with `Storage.from(...)` to persist keys. The provider also exposes pre-wired method clients at `provider.mpp` for direct use. For provisioning accounts and access-key policies (spend limits, scopes, expiry), see the [Accounts docs](https://accounts.tempo.xyz/docs).
75
+
76
+
## Local development with a viem account
77
+
78
+
For local development or a simple server wallet, skip Accounts and pass a viem account directly (store the key with `npx wrangler secret put MPP_PRIVATE_KEY`):
Agents can automatically interact with MPP-enabled services, paying for API calls without human intervention. Learn more about [agentic payments](/use-cases/agentic-payments) or get started below.
11
11
12
+
If you're building an agent with a partner SDK, see [Cloudflare Agents](/partner-sdks/cloudflare-agents) for a framework-specific integration.
13
+
12
14
| Tool | Best for | Setup |
13
15
|------|----------|-------|
14
16
|[Tempo Wallet](#tempo-wallet)| MPP services with spend controls and service discovery |`tempo wallet login`|
Copy file name to clipboardExpand all lines: src/pages/sdk/typescript/client/McpClient.wrap.mdx
+39-8Lines changed: 39 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,12 +1,15 @@
1
1
# `McpClient.wrap`[Payment-aware MCP client]
2
2
3
-
Wraps an MCP SDK client with automatic payment handling. When a tool call returns a `-32042` payment required error, the wrapper creates a Credential and retries the call.
3
+
Adds automatic payment handling to an MCP SDK client in place. When a tool call returns a `-32042` payment required error—or a tool result carrying payment-required metadata—the client creates a Credential and retries the call.
4
+
5
+
`McpClient.wrap` mutates the provided client and returns the same reference, so there is nothing to store: surfaces built on the original client stay payment-aware. This includes setups where another SDK owns the MCP client reference, for example [Cloudflare Agents](/partner-sdks/cloudflare-agents). Calling `wrap` on the same client again replaces its payment configuration.
`mcp` is the same object as `client`, retyped with the payment-aware `callTool`.
27
+
23
28
### With call options
24
29
25
-
Pass `context`and `timeout` through the second argument to `callTool`.
30
+
`callTool` keeps the MCP SDK's `(params, resultSchema?, options?)` signature—pass `context`, a per-call `onPaymentRequired` approval hook, and a request `timeout` through the third argument. `context` and `onPaymentRequired` are stripped before the remaining request options are forwarded to the SDK.
A per-call `onPaymentRequired` overrides the configured hook; pass `null` to bypass a configured hook for one call.
45
+
34
46
## Return type
35
47
36
-
`McpClient.wrap` returns an object that spreads the original client and overrides`callTool`with a payment-aware version.
48
+
`McpClient.wrap` returns the same client reference, with`callTool`retyped to the payment-aware version. The MCP SDK's three-argument `callTool` signature is preserved, so surfaces built on it—such as Cloudflare's `MCPClientManager.callTool(params, resultSchema, options)`—keep working.
Array of payment methods to use when handling payment Challenges. Accepts individual method clients or tuples (e.g. from `tempo()`). The client matches Challenges from the server against installed methods by name and intent.
Optional approval hook called before creating a payment credential. Receives the selected Challenge; return `false` to decline—`callTool` then rejects with `Paymentdeclined.`
Optional. Filters and sorts the supported Challenges before a Credential is created; the first candidate is used.
97
+
98
+
### config.paymentPreferences
99
+
100
+
- **Type:** `AcceptPayment.Config`
70
101
71
-
Array of payment methods to use when handling payment Challenges. The wrapper matches Challenges from the server against installed methodsby name and intent.
102
+
Optional. Client-declared supported payment methods, keyed by typed `method/intent` strings.
0 commit comments