Charging for MCP tools: a paid-tool starter you can ship today
How to turn any MCP tool into a paid tool with x402 — gate tool invocations behind HTTP 402, expose the same MCP schema, and bill per call in USDC. Plus the open-source starter repo that does this end-to-end.
The Model Context Protocol is becoming the standard way an agent calls a tool. Most MCP servers today are free, which is fine until your tool costs money to run — paid inference, paid data, paid API calls under the hood. Here’s how to turn any MCP tool into a paid tool, with a starter repo you can clone and ship.
What “paid MCP tool” means
Three things, all required:
- The tool still speaks MCP. Agents that use MCP shouldn’t need a new client. The auth happens at the transport layer, not in the schema.
- Every invocation is metered. Each
tools/callis a billable event — not a subscription, not a quota bucket. Per-call is the only unit that maps to “I called this tool once.” - The payment is settled before the tool responds. No “I’ll bill you later.” If the payment isn’t settled, the tool returns an error. Agents shouldn’t be able to incur debt against a wallet.
The transport pattern that makes this work
MCP runs over stdio or HTTP/SSE. For a paid tool, you run it over HTTP/SSE and insert an x402 gate between the MCP client and the server:
agent ──MCP-over-HTTP──> [x402 gate] ──MCP-over-stdio──> real MCP tool
↑ pays $0.01 per call
The gate is a tiny HTTP server that:
- Listens on a public URL
- Speaks MCP-over-HTTP to clients
- For each
tools/callrequest, returns402with the payment challenge - On retry with a valid payment header, verifies the settlement and proxies the call to the real MCP tool over stdio
- Emits the before/after events to
@harpd/observe
The agent’s MCP client doesn’t know it’s paying. It just sees a 402, notices the WWW-Authenticate: x402 challenge, and (if it’s x402-aware) signs and retries. Non-x402 clients get the 402 and surface the cost to whatever orchestrates them.
The starter repo
harpd-dev/mcp-paid-tool-starter is the open-source implementation of the pattern above. Structure:
mcp-paid-tool-starter/
├── gate.ts # the x402-aware MCP-over-HTTP proxy
├── tools/
│ └── example-search.ts # a sample paid tool you can rename
├── budget.ts # per-agent budget policy (copy-paste from template post)
├── package.json
└── README.md
The gate itself is ~80 lines:
// gate.ts — sketch; see repo for the working file
import express from 'express'
import { x402Middleware } from 'x402-express'
import { harpdPlugin } from '@harpd/observe'
import { spawn } from 'node:child_process'
const observe = harpdPlugin({
apiKey: process.env.HARPD_KEY ?? '',
endpoint: 'https://api.harpd.com',
budgetControl: false,
})
const app = express()
// Charge $0.01 per MCP call. Adjust per-tool if you want variable pricing.
app.use(x402Middleware({
payTo: process.env.SERVER_ADDRESS!,
token: '0xUSDC_base',
amount: 10000, // micro-USDC → $0.01
chainId: 8453,
facilitator: 'https://facilitator.example.com',
hooks: observe.hooks,
}))
// Proxy every MCP-over-HTTP request to the stdio MCP tool behind the gate.
app.post('/mcp', async (req, res) => {
const child = spawn('node', ['tools/example-search.js'])
// pipe req → child.stdin, child.stdout → res (real implementation handles JSON-RPC framing)
req.pipe(child.stdin)
child.stdout.pipe(res)
})
app.listen(3000, () => console.log('paid MCP gate on :3000/mcp'))
That’s the whole starter. The tool behind it is whatever MCP tool you already have — the gate doesn’t care what your tool does, only that it speaks MCP-over-stdio.
Pointing an agent at the paid tool
Anthropic’s Claude and most modern agents support MCP-over-HTTP. The agent config looks like:
{
"mcpServers": {
"paid-search": {
"url": "https://paid-search.example.com/mcp",
"headers": {
"X-Payment-Enabled": "true"
}
}
}
}
The X-Payment-Enabled header tells the x402-aware MCP client to handle 402 responses automatically. Non-aware clients just see a 402 and fail — which is fine; the human configuring them sees it as “this tool needs a paying agent.”
Why per-call is the only unit that works
MCP tools have two properties that rule out other billing units:
- Latency varies wildly. A search tool returns in 200ms; a research tool in 30 seconds. Subscription tiers based on “calls per minute” punish the slow tools.
- Cost varies wildly. A 5-second tool calls a 10-cent API; another 5-second tool calls a 5-cent LLM. Flat per-call pricing is the only thing that lets you bill each tool at its own real cost.
The gate’s amount field is set per-tool, not per-server. You can have a 1-cent search tool, a 5-cent research tool, and a 20-cent synthesis tool all running behind the same gate.
The upgrade path
The starter runs entirely on open source. When you need more, you upgrade by adding Harpd centrally:
- Multiple gate operators → publish to Harpd’s marketplace, get discovered by agents that aren’t yours.
- Team-wide budgets → flip
budgetControl: trueinharpdPlugin, edit the policy from the dashboard without redeploying gates. - Compliance export → every call already emitted an event; the export is a
SELECTagainst the events table.
The open-source starter is Harpd’s first draft. The hosted layer is the multi-tenant, dashboard-managed, audit-ready version of the same thing.