MCP Server Example
Build an MCP server that makes paid API requests
x402 MCP Example Client
This is an example client that demonstrates how to use the x402 payment protocol (v2) with the Model Context Protocol (MCP) to make paid API requests through an MCP server.
Prerequisites
- Node.js v20+ (install via nvm)
- pnpm v10 (install via pnpm.io/installation)
- A running x402 server (you can use the example express server at
examples/typescript/servers/express) - A valid Ethereum private key and/or Solana private key for making payments
- Claude Desktop with MCP support
Setup
- Install and build all packages from the typescript examples root:
cd ../../
pnpm install
pnpm build
cd clients/mcp
- Configure Claude Desktop MCP settings:
{
"mcpServers": {
"demo": {
"command": "pnpm",
"args": [
"--silent",
"-C",
"<absolute path to this repo>/examples/typescript/clients/mcp",
"dev"
],
"env": {
"EVM_PRIVATE_KEY": "<private key of a wallet with USDC on Base Sepolia>",
"SVM_PRIVATE_KEY": "<base58-encoded private key of a Solana wallet with USDC on Devnet>",
"RESOURCE_SERVER_URL": "http://localhost:4021",
"ENDPOINT_PATH": "/weather"
}
}
}
}
-
Make sure your x402 server is running at the URL specified in
RESOURCE_SERVER_URL(e.g., the example express server atexamples/typescript/servers/express) -
Restart Claude Desktop to load the new MCP server
-
Ask Claude to use the
get-data-from-resource-servertool
How It Works
The example demonstrates how to:
- Create an x402 client with EVM and SVM scheme support
- Register payment schemes using
@x402/evmand@x402/svm - Set up an MCP server with x402 payment handling
- Create a tool that makes paid API requests
- Handle responses and errors through the MCP protocol
Example Code
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import axios from "axios";
import { x402Client, wrapAxiosWithPayment } from "@x402/axios";
import { registerExactEvmScheme } from "@x402/evm/exact/client";
import { registerExactSvmScheme } from "@x402/svm/exact/client";
import { privateKeyToAccount } from "viem/accounts";
import { createKeyPairSignerFromBytes } from "@solana/kit";
import { base58 } from "@scure/base";
// Create x402 client with payment schemes
const client = new x402Client();
// Register EVM scheme
const evmSigner = privateKeyToAccount(EVM_PRIVATE_KEY);
registerExactEvmScheme(client, { signer: evmSigner });
// Register SVM scheme
const svmSigner = await createKeyPairSignerFromBytes(base58.decode(SVM_PRIVATE_KEY));
registerExactSvmScheme(client, { signer: svmSigner });
// Create Axios instance with payment handling
const api = wrapAxiosWithPayment(axios.create({ baseURL: RESOURCE_SERVER_URL }), client);
// Create MCP server
const server = new McpServer({
name: "x402 MCP Client Demo",
version: "2.0.0",
});
// Add tool for making paid requests
server.tool(
"get-data-from-resource-server",
"Get data from the resource server (in this example, the weather)",
{},
async () => {
const res = await api.get(ENDPOINT_PATH);
return {
content: [{ type: "text", text: JSON.stringify(res.data) }],
};
},
);
// Connect to MCP transport
const transport = new StdioServerTransport();
await server.connect(transport);
Response Handling
Payment Required (402)
When a payment is required, the x402 client will:
- Receive the 402 response
- Parse the payment requirements
- Create and sign a payment header using the appropriate scheme (EVM or SVM)
- Automatically retry the request with the payment header
Successful Response
After payment is processed, the MCP server will return the response data through the MCP protocol:
{
"content": [
{
"type": "text",
"text": "{\"report\":{\"weather\":\"sunny\",\"temperature\":70}}"
}
]
}
Integration with Claude Desktop
This example is designed to work with Claude Desktop's MCP support. The MCP server will:
- Listen for tool requests from Claude
- Handle the payment process automatically using x402 v2 protocol
- Return the response data through the MCP protocol
- Allow Claude to process and display the results
Related Content
Looking for more? Check out our other typescript examples or browse by client content.