A production-ready MCP (Model Context Protocol) gateway that aggregates multiple MCP servers into a unified interface with complete x402 blockchain payment support. Enables AI agents to autonomously pay for tools and services while allowing gateway operators to monetize MCP tool access.
- 🔄 Multi-Server Aggregation: Connect to unlimited MCP servers simultaneously
- 🏷️ Automatic Namespacing: Prevents tool/resource/prompt name conflicts
- 🚀 Multi-Transport Support: STDIO, HTTP, SSE, WebSocket - mix and match
- 💪 Health Monitoring: Automatic reconnection and health checks
- 🛡️ Conflict Resolution: Intelligent handling of duplicate tool names
- ⚡ Dynamic Discovery: Real-time capability updates and registry refresh
- 📋 Configuration-Based: YAML/JSON configuration for easy management
- 💰 x402 Blockchain Payments: Full EIP-712 signature validation with USDC
- 🔑 API Key Authentication: Tiered access with custom pricing
- 🌐 HTTP/SSE Transport: Proper 402 status codes for payment challenges
- ✅ Facilitator Integration: Official x402.org verification and settlement
- 💸 Three Payment Modes: Passthrough, Markup, Absorb
- 📊 Flexible Pricing: Per-tool, per-server, or global defaults
- 🧪 Testnet Ready: Base Sepolia support for safe testing
The gateway enables monetization of MCP tools using blockchain payments and API keys. Here's how it works:
┌──────────────────┐
│ AI Agent │ Anthropic Claude, OpenAI, etc.
│ (with wallet) │ Uses x402-fetch or x402-axios
└────────┬─────────┘
│ MCP Protocol
│ Auto-pays on HTTP 402
↓
┌──────────────────┐
│ HTTP Wrapper │ Port 8000 (optional, for x402)
│ 402 Gateway │ Returns HTTP 402 Payment Required
└────────┬─────────┘ Verifies payments with facilitator
│ OR Settles transactions on blockchain
┌──────────────────┐
│ STDIO Gateway │ Direct MCP (no payment support)
└────────┬─────────┘
│
│ MCP Protocol (authenticated)
↓
┌──────────────────┐
│ Gateway Core │ • Payment verification
│ │ • Tool routing
│ │ • Namespace management
└────────┬─────────┘
│
├─→ Free MCP Server (CoinGecko, filesystem, etc.)
├─→ Paid MCP Server (MCPay, premium APIs)
└─→ Your Custom MCP Server
Step 1: Client Requests Tool
POST http://localhost:8000/message
Content-Type: application/json
{"method": "tools/call", "params": {"name": "crypto:get_search"}}
Step 2: Gateway Returns 402 (Payment Required)
HTTP/1.1 402 Payment Required
Content-Type: application/json
X-Accept-Payment: {...}
{
"x402Version": 1,
"accepts": [{
"scheme": "exact",
"network": "base-sepolia",
"maxAmountRequired": "10000",
"resource": "http://localhost:8000/message",
"payTo": "0xYourAddress",
"asset": "0x036CbD...USDC",
"extra": {"name": "USDC", "version": "2"}
}]
}
Step 3: Agent Creates Payment (Automatic)
- Agent's x402-fetch/x402-axios intercepts 402
- Creates EIP-712 signature with wallet
- Signs USDC transfer authorization
- Encodes payment as base64
Step 4: Agent Retries with Payment
POST http://localhost:8000/message
X-PAYMENT: <base64-encoded-payment-signature>
{"method": "tools/call", "params": {"name": "crypto:get_search"}}
Step 5: Gateway Verifies & Settles
- Decodes payment signature
- Verifies with x402 facilitator
- Settles transaction on blockchain
- Forwards request to downstream MCP server
Step 6: Tool Executes
- MCP server processes request
- Returns result to gateway
- Gateway forwards to client
Use Case: Aggregate paid MCP servers, earn discovery fees
servers:
- name: "mcpay-server"
transport:
type: "http"
url: "https://paid-api.com"
paymentMode: "passthrough"
Flow: Client Payment → Gateway (takes fee) → Downstream Server Revenue: Discovery/routing fees, no downstream costs
Use Case: Resell paid APIs with margin
payment:
enabled: true
recipient: "0xYourWallet"
outboundWallet: "0xYourPrivateKey"
servers:
- name: "premium-api"
paymentMode: "markup"
markup: "20%"
Flow: Client pays (downstream + 20%) → Gateway verifies → Gateway pays downstream → Earn 20% margin Revenue: Percentage markup on every transaction
Use Case: Subscription/membership model
payment:
enabled: true
outboundWallet: "0xYourPrivateKey"
apiKeys:
- key: "premium_subscriber"
tier: "premium"
servers:
- name: "expensive-api"
paymentMode: "absorb"
defaultPricing:
apiKeyTiers:
premium: "free"
x402: "$1.00"
Flow: Client authenticated with API key → Gateway pays all downstream costs → Subscription revenue Revenue: Monthly/annual subscriptions, unlimited usage
servers:
- name: "myserver"
tools:
- name: "basic-tool"
pricing:
free: true
- name: "premium-tool"
pricing:
x402: "$0.10"
apiKeyTiers:
basic: "$0.05"
premium: "free"
servers:
- name: "myserver"
defaultPricing:
x402: "$0.01"
servers:
- name: "freemium-server"
defaultPricing:
x402: "$0.01"
tools:
- name: "premium-feature"
pricing:
x402: "$1.00"
- name: "free-sample"
pricing:
free: true
npm install -g @elizaos/mcp-gateway
Or run directly with npx:
npx @elizaos/mcp-gateway --config=config.yaml
# Install dependencies
bun install
# Start HTTP wrapper with x402 support
bun run src/transports/http-wrapper.ts --config=your-config.yaml --port=8000
This exposes:
- SSE endpoint:
http://localhost:8000/sse
- Message endpoint:
http://localhost:8000/message
- Supports: HTTP 402 status codes, x402 payments, API keys
name: "Development MCP Gateway"
version: "1.0.0"
servers:
# Local STDIO server
- name: "filesystem"
transport:
type: "stdio"
command: "npx"
args: ["-y", "@modelcontextprotocol/server-filesystem", "/tmp"]
namespace: "fs"
enabled: true
# Remote SSE server
- name: "coingecko"
transport:
type: "sse"
sseUrl: "https://mcp.api.coingecko.com/sse"
postUrl: "https://mcp.api.coingecko.com/message"
namespace: "crypto"
enabled: true
settings:
enableToolConflictResolution: true
logLevel: "info"
name: "Paid MCP Gateway"
version: "1.0.0"
# Payment Configuration
payment:
enabled: true
recipient: "0xYourEthereumAddress"
network: "base-sepolia" # or "base" for mainnet
facilitator: "https://x402.org/facilitator"
# Optional: API keys for subscription model
apiKeys:
- key: "eliza_premium_abc123"
tier: "premium"
- key: "eliza_basic_xyz789"
tier: "basic"
servers:
- name: "coingecko"
transport:
type: "sse"
sseUrl: "https://mcp.api.coingecko.com/sse"
postUrl: "https://mcp.api.coingecko.com/message"
namespace: "crypto"
# Charge $0.01 per tool call
defaultPricing:
x402: "$0.01"
apiKeyTiers:
premium: "free"
basic: "$0.005"
# Or per-tool pricing
tools:
- name: "get_search"
pricing:
x402: "$0.02"
- name: "get_simple_price"
pricing:
free: true
settings:
logLevel: "info"
payment:
enabled: true
recipient: "0xYourWallet"
network: "base"
# Wallet for paying downstream services
outboundWallet: "0xYourPrivateKey"
servers:
# Passthrough mode - forward client payments
- name: "mcpay-search"
transport:
type: "http"
url: "https://mcpay.tech/search"
paymentMode: "passthrough"
# Markup mode - add 20% margin
- name: "premium-api"
transport:
type: "http"
url: "https://premium-api.com"
paymentMode: "markup"
markup: "20%"
# Absorb mode - subscription model
- name: "subscriber-api"
transport:
type: "http"
url: "https://expensive-api.com"
paymentMode: "absorb"
defaultPricing:
apiKeyTiers:
premium: "free"
Payment Fields:
payment.enabled
: Enable payment gating (true/false)payment.recipient
: Your Ethereum address for receiving paymentspayment.network
: Blockchain network (base-sepolia
,base
,ethereum
)payment.facilitator
: x402 facilitator URL (default:https://x402.org/facilitator
)payment.outboundWallet
: Private key for paying downstream (markup/absorb modes)payment.apiKeys
: Array of API keys with tiers
Server Fields:
transport.type
:stdio
,http
,sse
, orwebsocket
namespace
: Prefix for tool names (prevents conflicts)paymentMode
:passthrough
,markup
, orabsorb
markup
: Percentage (20%
) or fixed amount ($0.05
)defaultPricing
: Server-wide pricing defaultstools
: Per-tool configuration and pricing
Pricing Fields:
x402
: Price for blockchain payments (e.g.,$0.01
)apiKeyTiers
: Tier-specific pricing ({"premium": "free", "basic": "$0.05"}
)free
: Mark tool as free (true/false)
Free, no payments:
# Start gateway
mcp-gateway --config=config.yaml
Claude Desktop configuration:
{
"mcpServers": {
"eliza-gateway": {
"command": "npx",
"args": ["-y", "@elizaos/mcp-gateway", "--config", "/path/to/config.yaml"]
}
}
}
With x402 payment support:
# Start HTTP wrapper with x402
bun run src/transports/http-wrapper.ts --config=paid-config.yaml --port=8000
Agents connect to:
- SSE endpoint:
http://localhost:8000/sse
- Message endpoint:
http://localhost:8000/message
Client integration:
import { createMCPClient } from 'ai';
import { SSEClientTransport } from '@modelcontextprotocol/sdk/client/sse.js';
import { wrapFetchWithPayment } from 'x402-fetch';
import { privateKeyToAccount } from 'viem/accounts';
// Setup wallet for payments
const account = privateKeyToAccount('0xYourPrivateKey');
const fetchWithPayment = wrapFetchWithPayment(fetch, account);
// Create MCP client with payment support
const mcpClient = await createMCPClient({
transport: new SSEClientTransport(
new URL('http://localhost:8000/sse'),
{ fetch: fetchWithPayment }
)
});
MCP_GATEWAY_NAME
- Gateway name (default: "Eliza MCP Gateway")MCP_LOG_LEVEL
- Log level: error, warn, info, debug (default: info)MCP_SERVERS
- Semicolon-separated server specsMCP_ENABLE_TOOL_CONFLICT_RESOLUTION
- Enable conflict resolution (default: true)
-
📟 STDIO Transport - Local MCP servers via stdin/stdout
- Perfect for command-line MCP servers, Claude Desktop, Cursor
- Configuration:
command
,args
,env
,cwd
-
🌐 HTTP Transport - Remote MCP servers via HTTP/HTTPS
- For web-based MCP servers and API integrations
- Configuration:
url
,apiKey
,headers
-
📡 SSE Transport - Server-Sent Events for streaming
- Real-time streaming capabilities
- Configuration:
sseUrl
,postUrl
,apiKey
,headers
-
🔌 WebSocket Transport - Real-time bidirectional communication
- Low-latency, persistent connections
- Configuration:
url
,apiKey
,headers
# Modern transport configuration
servers:
# Local STDIO server
- name: "local-server"
transport:
type: "stdio"
command: "npx"
args: ["-y", "user-review-mcp"]
# Remote HTTP server
- name: "remote-server"
transport:
type: "http"
url: "https://mcp.example.com/api"
apiKey: "YOUR_API_KEY"
# Legacy format (still supported)
- name: "legacy-server"
command: "npx"
args: ["-y", "@upstash/context7-mcp"]
See the examples/
directory for complete, tested configurations:
examples/mixed-transports.yaml
- STDIO + HTTP combinationexamples/http-remote.yaml
- Pure HTTP setupexamples/future-multi-transport.yaml
- All 4 transport typesexamples/config.yaml
- Basic STDIO configuration
The gateway includes a comprehensive End-to-End test suite that validates all functionality across different configurations and transport types.
# Run essential tests (fastest)
bun run test:quick
# Run all tests including payment tests
bun run test:all
# Or run test suites individually:
bun run test:quick # Quick smoke tests (~45s)
bun run test # Full E2E tests (~180s)
bun run test:payment # Payment E2E tests (~31s)
# Test with specific config
bun run start --config=tests/configs/basic.yaml
# Available test configs:
# Core functionality:
# - basic.yaml - Single server, STDIO transport
# - basic.json - Same as basic.yaml but JSON format
# - namespaced.yaml - Single server with namespace
# - multi-server.yaml - Multiple servers, different namespaces
# - invalid.yaml - Invalid config for error testing
# - failing-server.yaml - Server connection failure testing
#
# Payment features:
# - paid-free-tools.yaml - Mix of free and paid tools
# - paid-api-key-only.yaml - API key authentication only
# - paid-x402-only.yaml - x402 blockchain payments only
# - paid-default-pricing.yaml - Server-wide default pricing
# - paid-disabled.yaml - Payment disabled (backward compatibility)
# Manual testing with MCP Inspector
npx @modelcontextprotocol/inspector bun run src/index.ts --config=examples/config.yaml
The E2E test suite validates:
- ✅ Configuration Loading: YAML, JSON, and environment variables
- ✅ All Transport Types: STDIO, HTTP, SSE, WebSocket
- ✅ Multi-Server Support: Multiple servers with namespace handling
- ✅ Payment Features: x402 verification, API keys, tiered pricing
- ✅ Error Handling: Invalid configs, connection failures, graceful degradation
- ✅ Tool Execution: MCP protocol communication and tool calls
- ✅ Process Management: Startup, shutdown, cleanup
Latest Test Results:
- Quick E2E: ✅ 11/11 passed (45s)
- Full E2E: ✅ 28/28 passed (180s)
- Payment E2E: ✅ 15/15 passed (31s)
- Total: ✅ 54/54 tests (100% success rate)
# For continuous integration pipelines
bun run test:quick # Fast, essential tests only
bun run test:all # Complete test suite
See TESTING.md for detailed testing guide.
When running with multiple servers, you'll see:
[INFO] Starting Eliza MCP Gateway Server: Development MCP Gateway v1.0.0
[INFO] Initializing 2 servers...
[INFO] Successfully initialized server context7
[INFO] Successfully initialized server user-review
[INFO] Initialized 2/2 servers successfully
[INFO] Registry refreshed: 3 tools, 0 resources, 0 prompts
[INFO] === Eliza MCP Gateway Status ===
[INFO] Server Connections: 2/2 active
[INFO] Tools by Server:
[INFO] - context7: 2 tools
[INFO] - user-review: 1 tools
Available tools with namespacing:
docs:resolve-library-id
(Context7)docs:get-library-docs
(Context7)review:get-user-review
(User Review MCP)
┌─────────────────────────────────────────────────────────┐
│ AI Agents / Clients │
│ (Claude Desktop, Cursor, Custom Agents, AI Frameworks) │
└──────────────────────┬──────────────────────────────────┘
│
┌──────────────┴──────────────┐
│ │
STDIO Mode HTTP/SSE Mode
(No Payments) (With x402 Payments)
│ │
│ ┌───────┴────────┐
│ │ HTTP Wrapper │
│ │ Port 8000 │
│ │ • Returns 402 │
│ │ • Verifies x402
│ │ • Settles txn │
│ └───────┬────────┘
│ │
└──────────────┬──────────────┘
│
┌────────┴────────┐
│ Gateway Core │
│ • Registry │
│ • Routing │
│ • Namespacing │
└────────┬────────┘
│
┌──────────────┼──────────────┐
│ │ │
┌────┴────┐ ┌────┴────┐ ┌────┴────┐
│ STDIO │ │ HTTP │ │ SSE │
│ Server │ │ Server │ │ Server │
└─────────┘ └─────────┘ └─────────┘
Local Remote Streaming
sequenceDiagram
participant Agent as AI Agent<br/>(x402-fetch)
participant Wrapper as HTTP Wrapper<br/>(Port 8000)
participant PM as Payment<br/>Middleware
participant Facilitator as x402.org<br/>Facilitator
participant Gateway as Gateway Core<br/>(stdio)
participant MCP as Downstream<br/>MCP Server
Agent->>Wrapper: POST /message (CallTool)
Wrapper->>PM: Check payment required?
PM-->>Wrapper: Yes, $0.01 required
Wrapper-->>Agent: 402 Payment Required<br/>+ payment requirements
Note over Agent: x402-fetch intercepts 402<br/>Creates EIP-712 signature<br/>Signs USDC authorization
Agent->>Wrapper: POST /message + X-PAYMENT header
Wrapper->>PM: Decode & verify payment
PM->>Facilitator: Verify signature
Facilitator-->>PM: ✅ Valid, payer verified
PM->>Facilitator: Settle transaction
Facilitator-->>PM: ✅ Settlement queued
PM-->>Wrapper: ✅ Payment authorized
Wrapper->>Gateway: Forward CallTool (stdio)
Gateway->>MCP: Execute tool
MCP-->>Gateway: Tool result
Gateway-->>Wrapper: MCP response
Wrapper-->>Agent: Tool result (SSE stream)
- Handles HTTP/SSE protocol
- Returns proper HTTP 402 status codes
- Integrates x402 SDK for payment verification
- Manages gateway subprocesses per session
- Required for x402 payments
- MCP protocol implementation
- Tool/resource/prompt routing
- Namespace management
- Server connection management
- Works over stdio (no HTTP needed)
- x402 payment verification (EIP-712 signatures)
- API key tier validation
- Facilitator integration
- Payment settlement
- Pricing calculation (markup, passthrough, absorb)
- Multi-transport support (STDIO, HTTP, SSE, WebSocket)
- Health monitoring
- Automatic reconnection
- Connection pooling
- Capability aggregation
- Conflict resolution
- Namespace application
- Dynamic updates
Turn free public APIs into paid services:
- Gateway connects to CoinGecko (free)
- Charges agents $0.01 per tool call
- Revenue: 46 tools × $0.01 = $0.46 per full query
Become a discovery/routing layer for paid services:
- Connect to multiple MCPay servers
- Use passthrough mode (forward payments)
- Revenue: Discovery fees, routing fees
Add value and earn markup:
- Connect to paid APIs
- Add 20-40% markup
- Revenue: Margin on every transaction
Bundle multiple paid APIs:
- Absorb downstream costs
- Charge monthly subscription via API keys
- Revenue: Predictable recurring income
Mix free and paid tools:
- Free tools for discovery
- Premium tools for revenue
- Revenue: Conversion from free to paid users
payment:
network: "base-sepolia"
recipient: "0xTestAddress"
facilitator: "https://x402.org/facilitator"
- Get free USDC: https://www.coinbase.com/faucets/base-sepolia-faucet
- Safe for testing, no real money
payment:
network: "base"
recipient: "0xProductionAddress"
facilitator: "https://x402.org/facilitator"
- Real USDC transactions
- Monitor settlement status
- Secure key management essential
- ✅ Use checksummed Ethereum addresses
- ✅ Store private keys in secure vaults
- ✅ Monitor payment verification logs
- ✅ Set reasonable timeout values
- ✅ Test on testnet first
- ✅ Implement rate limiting
- ✅ Log all payment attempts
- Protocol: EIP-712 typed signatures
- Asset: USDC (6 decimals)
- Networks: Base, Base Sepolia, Ethereum, Optimism, Polygon
- Min Amount: $0.001 (1000 atomic units)
- Settlement: Gasless via facilitator
- Timeout: 30-60 seconds
Network | USDC Contract | Chain ID | Type |
---|---|---|---|
base-sepolia | 0x036CbD...3dCF7e | 84532 | Testnet |
base | 0x833589...02913 | 8453 | Mainnet |
ethereum | 0xA0b869...eB48 | 1 | Mainnet |
optimism | 0x0b2C63...Ff85 | 10 | Mainnet |
polygon | 0x3c499c...c3359 | 137 | Mainnet |
Transport | Payment Support | Use Case |
---|---|---|
STDIO | ❌ No HTTP headers | Claude Desktop, Cursor (free only) |
HTTP | ✅ Full x402 | Remote APIs, agents with wallets |
SSE | ✅ Full x402 | Streaming, real-time agents |
WebSocket | Low-latency applications |
- Connection: ~500ms (SSE handshake)
- Tool Discovery: ~1s (46 tools)
- Payment Verification: ~200ms (facilitator)
- Tool Execution: 1-5s (depends on downstream)
- Total Latency: 2-7s per paid query
- Payment Implementation - Payment system details
- Passthrough Guide - Passthrough mode setup
- Anthropic + x402 Integration - Complete guide
- Testing Guide - Test suite documentation
- Feature Map - All features and capabilities
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature
) - Make your changes with tests
- Commit your changes (
git commit -m 'feat: Add amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
MIT License - see LICENSE file for details.
- Model Context Protocol - MCP specification
- x402 Protocol - Blockchain payment protocol
- Anthropic SDK - Claude AI integration
- Viem - Ethereum library
- CoinGecko MCP - Cryptocurrency data (46 tools)
- Context7 - Code documentation
- MCPay Servers - Paid MCP marketplace
- Filesystem, Fetch, GitHub - Standard MCP servers
Built with ❤️ for the AI Agent ecosystem