Skip to content
Dashboard

Introducing x402-mcp: Open protocol payments for MCP tools

Software Engineer

Link to headingHow does x402 help?

Paid API routes

import { paymentMiddleware } from "x402-next";
export const middleware = paymentMiddleware(
{
"/protected": {
price: 0.01,
config: {
description: "Access to protected content"
}
},
}
);

import { wrapFetchWithPayment } from "x402-fetch";
const fetchWithPay = wrapFetchWithPayment(fetch, client);
const response = await fetchWithPay("https://api.example.com/paid-endpoint");

Paid MCP tools

api/mcp/route.ts
import { createPaidMcpHandler } from "x402-mcp";
import z from "zod";
const handler = createPaidMcpHandler(
(server) => {
server.paidTool(
"add_numbers",
{
// declare a price of $0.001
price: 0.001
},
{ a: z.number(), b: z.number() },
async (args) => {
// ...your tool call
}
);
},
{ recipient: process.env.WALLET_ADDRESS }
);
export { handler as GET, handler as POST };

import { experimental_createMCPClient as createMCPClient } from "ai";
import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js";
import { withPayment } from "x402-mcp";
const mcpClient = await createMCPClient({
transport: new StreamableHTTPClientTransport(url),
}).then((client) => withPayment(client, { account }));
const tools = await mcpClient.tools();

Link to headingHow the protocol works

Link to headingTry the starter template

Link to headingWhat's next

Ready to deploy?