-
Notifications
You must be signed in to change notification settings - Fork 16.2k
[Agents] Improve MPP payment guides #32185
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
irvinebroque
merged 9 commits into
cloudflare:production
from
parvahuja:parv/mpp-agents-docs
Aug 5, 2026
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
2241b9b
[Agents] Expand MPP payment documentation
parvahuja bebdacf
[Agents] Refine MPP payment how-to guides
parvahuja c620f3a
[Agents] Tighten MPP payment guides
parvahuja 669d6f3
[Agents] Restore MPP origin proxy guide
parvahuja d934428
[Agents] Refine MPP guide navigation
parvahuja 8ffc5aa
[Agents] Restore HTTP content guide placement
parvahuja ddcc397
[Agents] Link to the MPP Worker guide
parvahuja 002cc8f
[Agents] Address MPP guide feedback
parvahuja 373a4eb
[Agents] Clarify payment flow reuse
parvahuja File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
182 changes: 182 additions & 0 deletions
182
src/content/docs/agents/tools/payments/mpp/accept-payments.mdx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,182 @@ | ||
| --- | ||
| title: Accept payments with MPP | ||
| pcx_content_type: how-to | ||
| sidebar: | ||
| order: 2 | ||
| description: Accept Machine Payments Protocol (MPP) payments from an origin, Cloudflare Worker route, or Model Context Protocol (MCP) tool. | ||
| products: | ||
| - agents | ||
| --- | ||
|
|
||
| import { | ||
| PackageManagers, | ||
| Steps, | ||
| TypeScriptExample, | ||
| WranglerConfig, | ||
| } from "~/components"; | ||
|
|
||
| Use Cloudflare Workers to accept Machine Payments Protocol (MPP) payments. Choose an integration based on the service you want to protect: | ||
|
|
||
| - [`mpp-proxy`](https://github.com/cloudflare/mpp-proxy) — Charge for HTTP content without changing your origin code. Refer to [Charge for HTTP content](/agents/tools/payments/mpp-charge-for-http-content/). | ||
| - **Worker route** — Add `mppx` payment middleware to a Worker application. | ||
| - **MCP tool** — Require payment before an MCP tool returns its result. | ||
|
|
||
| ## Prerequisites | ||
|
|
||
| Create a [Cloudflare account](/fundamentals/account/create-account/). You also need a payment recipient and an MPP secret key. | ||
|
|
||
| The examples use a stablecoin payment method on testnet. For other methods, refer to [MPP payment methods](https://mpp.dev/payment-methods/). | ||
|
|
||
| ## Charge for a Worker route | ||
|
|
||
| Add `mppx` middleware when you control the Worker application: | ||
|
|
||
| <Steps> | ||
|
|
||
| 1. Install Hono and `mppx` in the Worker project: | ||
|
|
||
| <PackageManagers pkg="hono mppx" /> | ||
|
|
||
| 2. Store the MPP signing key as a [Worker secret](/workers/configuration/secrets/): | ||
|
|
||
| <PackageManagers type="exec" pkg="wrangler" args="secret put MPP_SECRET_KEY" /> | ||
|
|
||
| 3. Add the payment middleware before the paid route handler: | ||
|
|
||
| <TypeScriptExample filename="src/index.ts"> | ||
|
|
||
| ```ts | ||
| import { env } from "cloudflare:workers"; | ||
| import { Hono } from "hono"; | ||
| import { Mppx, tempo } from "mppx/hono"; | ||
|
|
||
| const workerEnv = env as Env & { MPP_SECRET_KEY: string }; | ||
| const app = new Hono(); | ||
| const mppx = Mppx.create({ | ||
| methods: [tempo.charge({ testnet: true })], | ||
| secretKey: workerEnv.MPP_SECRET_KEY, | ||
| }); | ||
|
|
||
| app.get( | ||
| "/premium", | ||
| mppx.charge({ | ||
| amount: "0.01", | ||
| currency: "0x20c0000000000000000000000000000000000000", | ||
| description: "Premium API access", | ||
| recipient: "<YOUR_WALLET_ADDRESS>", | ||
| }), | ||
| (c) => c.json({ access: "paid" }), | ||
| ); | ||
|
|
||
| export default app; | ||
| ``` | ||
|
|
||
| </TypeScriptExample> | ||
|
|
||
| 4. Deploy the Worker: | ||
|
|
||
| <PackageManagers type="exec" pkg="wrangler" args="deploy" /> | ||
|
|
||
| 5. Request the paid route without a payment: | ||
|
|
||
| ```sh | ||
| curl -i https://<YOUR_WORKER>.workers.dev/premium | ||
| ``` | ||
|
|
||
| The Worker returns `402 Payment Required` and a `WWW-Authenticate: Payment` header. A paid retry reaches the handler and returns a `Payment-Receipt` header. | ||
|
|
||
| </Steps> | ||
|
|
||
| ## Charge for an MCP tool | ||
|
|
||
| Add the MPP transport to an [`McpAgent`](/agents/model-context-protocol/apis/agent-api/): | ||
|
|
||
| <Steps> | ||
|
|
||
| 1. Install the required packages in an Agents project: | ||
|
|
||
| <PackageManagers pkg="agents mppx @modelcontextprotocol/sdk zod" /> | ||
|
|
||
| 2. Bind the `McpAgent` Durable Object in the Wrangler configuration: | ||
|
|
||
| <WranglerConfig> | ||
|
|
||
| ```toml | ||
| name = "mpp-server" | ||
| main = "src/index.ts" | ||
| compatibility_date = "$today" | ||
| compatibility_flags = ["nodejs_compat"] | ||
|
|
||
| [[durable_objects.bindings]] | ||
| name = "MCP_OBJECT" | ||
| class_name = "PaidMCP" | ||
|
|
||
| [[migrations]] | ||
| tag = "v1" | ||
| new_sqlite_classes = ["PaidMCP"] | ||
| ``` | ||
|
|
||
| </WranglerConfig> | ||
|
|
||
| 3. Store `MPP_SECRET_KEY` as a Worker secret: | ||
|
|
||
| <PackageManagers type="exec" pkg="wrangler" args="secret put MPP_SECRET_KEY" /> | ||
|
|
||
| 4. Check payment before returning the tool result: | ||
|
|
||
| <TypeScriptExample filename="src/index.ts"> | ||
|
|
||
| ```ts | ||
| import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; | ||
| import { McpAgent } from "agents/mcp"; | ||
| import { Mppx, tempo, Transport } from "mppx/server"; | ||
| import { z } from "zod"; | ||
|
|
||
| type PaymentEnv = Env & { MPP_SECRET_KEY: string }; | ||
|
|
||
| export class PaidMCP extends McpAgent<PaymentEnv> { | ||
| server = new McpServer({ name: "paid-search", version: "1.0.0" }); | ||
|
|
||
| async init() { | ||
| const mppx = Mppx.create({ | ||
| methods: [tempo.charge({ testnet: true })], | ||
| secretKey: this.env.MPP_SECRET_KEY, | ||
| transport: Transport.mcpSdk(), | ||
| }); | ||
|
|
||
| this.server.tool( | ||
| "premium_search", | ||
| "Search premium content", | ||
| { query: z.string() }, | ||
| async ({ query }, extra) => { | ||
| const payment = await mppx.charge({ | ||
| amount: "0.01", | ||
| currency: "0x20c0000000000000000000000000000000000000", | ||
| description: "Premium search", | ||
| recipient: "<YOUR_WALLET_ADDRESS>", | ||
| })(extra); | ||
|
|
||
| if (payment.status === 402) throw payment.challenge; | ||
|
|
||
| return payment.withReceipt({ | ||
| content: [{ type: "text", text: `Results for: ${query}` }], | ||
| }); | ||
| }, | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| export default PaidMCP.serve("/mcp"); | ||
| ``` | ||
|
|
||
| </TypeScriptExample> | ||
|
|
||
| 5. Deploy the Worker: | ||
|
|
||
| <PackageManagers type="exec" pkg="wrangler" args="deploy" /> | ||
|
|
||
| An unpaid `premium_search` call returns an MPP Challenge. A paid retry returns the tool result and an MPP Receipt in `_meta`. | ||
|
|
||
| </Steps> | ||
|
|
||
| To test both payment flows from a Cloudflare Agent, refer to [Pay from the Agents SDK](/agents/tools/payments/mpp/pay-from-agents-sdk/). For production billing patterns, refer to [MPP payment intents](https://mpp.dev/intents/). |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.