Charge for MCP tools

Last updated Jun 3, 2026 The Agents SDK provides paidTool, a drop-in replacement for tool that adds x402 payment requirements. Clients pay per tool call, and you can mix free and paid tools in the same server.

Setup

Wrap your McpServer with withX402 and use paidTool for tools you want to charge for:

import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { McpAgent } from "agents/mcp";
import { withX402, type X402Config } from "agents/x402";
import { z } from "zod";

const X402_CONFIG: X402Config = {
    network: "base",
    recipient: "0xYourWalletAddress",
    facilitator: { url: "https://x402.org/facilitator" }, // Payment facilitator URL
    // To learn more about facilitators: https://docs.x402.org/core-concepts/facilitator
};

export class PaidMCP extends McpAgent<Env> {
    server = withX402(
        new McpServer({ name: "PaidMCP", version: "1.0.0" }),
        X402_CONFIG,
    );

    async init() {
        // Paid tool — $0.01 per call
        this.server.paidTool(
            "square",
            "Squares a number",
            0.01, // USD
            { number: z.number() },
            {},
            async ({ number }) => {
                return { content: [{ type: "text", text: String(number ** 2) }] };
            },
        );

        // Free tool
        this.server.tool(
            "echo",
            "Echo a message",
            { message: z.string() },
            async ({ message }) => {
                return { content: [{ type: "text", text: message }] };
            },
        );
    }
}

Configuration

Field Description
network base for production, base-sepolia for testing
recipient Wallet address to receive payments
facilitator Payment facilitator URL (use https://x402.org/facilitator)

paidTool signature

this.server.paidTool(
    name, // Tool name
    description, // Tool description
    price, // Price in USD (e.g., 0.01)
    inputSchema, // Zod schema for inputs
    annotations, // MCP annotations
    handler, // Async function that executes the tool
);

When a client calls a paid tool without payment, the server returns 402 with payment requirements. The client pays via x402, retries with payment proof, and receives the result.

Testing

Use base-sepolia and get test USDC from the Circle faucet ↗︎.

For a complete working example, refer to x402-mcp on GitHub ↗︎.

Was this helpful?

YesNo

On this page

Charge for MCP tools - 图1Docs

{"@context":"https://schema.org","@type":"TechArticle","@id":"https://developers.cloudflare.com/agents/tools/payments/x402/charge-for-mcp-tools/#page","headline":"Charge for MCP tools","description":"Charge per tool call in an MCP server using paidTool.","url":"https://developers.cloudflare.com/agents/tools/payments/x402/charge-for-mcp-tools/","inLanguage":"en","image":"https://developers.cloudflare.com/agents/tools/payments/x402/charge-for-mcp-tools/og.png?v=08d95a07be324c1a","dateModified":"2026-06-03","publisher":{"@type":"Organization","name":"Cloudflare","description":"One platform for your apps, agents, and workforce. Build, secure, and scale without managing infrastructure","url":"https://www.cloudflare.com/","sameAs":["https://github.com/cloudflare","https://www.linkedin.com/company/cloudflare","https://x.com/cloudflare"],"logo":{"@type":"ImageObject","url":"https://developers.cloudflare.com/logo.svg"},"address":{"@type":"PostalAddress","streetAddress":"101 Townsend St","addressLocality":"San Francisco","addressRegion":"CA","postalCode":"94107","addressCountry":"US"},"contactPoint":[{"@type":"ContactPoint","contactType":"Customer Support","url":"https://support.cloudflare.com/","availableLanguage":["English"]},{"@type":"ContactPoint","contactType":"Sales","url":"https://www.cloudflare.com/contact/","availableLanguage":["English"]}]},"isPartOf":{"@type":"WebSite","@id":"https://developers.cloudflare.com/#website","name":"Cloudflare Docs","url":"https://developers.cloudflare.com/"}}


description: Use withX402Client to pay for resources from a Cloudflare Agent. title: Pay from Agents SDK image: https://developers.cloudflare.com/agents/tools/payments/x402/pay-from-agents-sdk/og.png?v=f5ec664c407240c0


Skip to content

Documentation Index
Fetch the complete documentation index at: https://developers.cloudflare.com/agents/llms.txt
Use this file to discover all available pages before exploring further.