Skip to main content
If you’ve built an MCP server with valuable tools or data, you can monetize it with x402. Instead of managing API keys, subscriptions, or billing systems, you simply add payment middleware to your server. Agents pay per-request automatically, and payments go directly to your wallet. This is for MCP developers who want to charge for their tools - whether it’s a database query tool, an API wrapper, or any other MCP capability.

Installation

npm install express @modelcontextprotocol/sdk @faremeter/middleware @faremeter/info

Create a paywalled MCP server

import express from "express";
import { randomUUID } from "node:crypto";
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
import { express as faremeter } from "@faremeter/middleware";
import { solana } from "@faremeter/info";

const app = express();
app.use(express.json());

const sessions = new Map();

// Create payment middleware
const paywalledMiddleware = await faremeter.createMiddleware({
  facilitatorURL: "https://facilitator.corbits.dev",
  accepts: [
    {
      ...solana.x402Exact({
        network: "devnet",
        asset: "USDC",
        amount: 100, // $0.0001 per request
        payTo: "YOUR_WALLET_ADDRESS",
      }),
      resource: "http://localhost:3000/mcp/premium",
      description: "Premium MCP tools",
    },
  ],
});

// Premium MCP endpoint with payment
app.post(
  "/mcp/premium",
  (req, res, next) => {
    // Bypass payment for MCP protocol methods
    const bypassMethods = [
      "initialize",
      "initialized",
      "notifications/initialized",
      "tools/list",
      "prompts/list",
      "resources/list",
    ];
    if (req.body?.method && bypassMethods.includes(req.body.method)) {
      next();
    } else {
      paywalledMiddleware(req, res, next);
    }
  },
  async (req, res) => {
    const sessionId = req.headers["mcp-session-id"];
    let transport = sessionId ? sessions.get(sessionId) : undefined;

    // Create new session on initialize
    if (!transport && req.body?.method === "initialize") {
      transport = new StreamableHTTPServerTransport({
        sessionIdGenerator: () => randomUUID(),
        onsessioninitialized: (sessionId) => {
          sessions.set(sessionId, transport);
        },
        enableDnsRebindingProtection: true,
        allowedHosts: ["127.0.0.1", "localhost", "localhost:3000"],
      });

      transport.onclose = () => {
        if (transport.sessionId) sessions.delete(transport.sessionId);
      };

      const server = new McpServer({
        name: "my-premium-server",
        version: "1.0.0",
      });

      // Register your tools
      server.registerTool(
        "get_data",
        {
          title: "Get premium data",
          description: "Returns premium data",
          inputSchema: {
            type: "object",
            properties: {
              query: { type: "string" },
            },
          },
        },
        async ({ query }) => {
          return {
            content: [{ type: "text", text: `Premium result for: ${query}` }],
          };
        }
      );

      await server.connect(transport);
    } else if (!transport) {
      res.status(400).json({ error: "Invalid session" });
      return;
    }

    await transport.handleRequest(req, res, req.body);
  }
);

// Session management
const handleSession = async (req, res) => {
  const sessionId = req.headers["mcp-session-id"];
  const transport = sessionId ? sessions.get(sessionId) : undefined;
  if (!transport) {
    res.status(400).send("Invalid session");
    return;
  }
  await transport.handleRequest(req, res);
};

app.get("/mcp/premium", handleSession);
app.delete("/mcp/premium", handleSession);

app.listen(3000, () => {
  console.log("Paywalled MCP server running on http://localhost:3000/mcp/premium");
});
Now agents must pay per-request to use your MCP tools!

View Full MCP Demo

Complete MCP server implementation with multiple tools

Don’t want to code payment logic?

If you want to monetize your MCP server without writing payment code, we can proxy it for you. You run your MCP server however you want, and we handle all the x402 payment verification and settlement. Agents pay through our infrastructure, and payments go directly to your wallet.

Let us help

Add your MCP servers to Corbits, and enable agents to pay for your MCP server using x402

Build your own MCP products