Skip to content

Commit f4820b2

Browse files
authored
Merge pull request #2 from elizaOS/feat/x402-integration
feat(x402): optional x402 payment middleware for HTTP/SSE transports
2 parents 9592fee + 4e6f175 commit f4820b2

23 files changed

+2485
-1059
lines changed

.github/workflows/test.yml

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,43 +11,38 @@ jobs:
1111
name: E2E Tests
1212
runs-on: ubuntu-latest
1313

14-
strategy:
15-
matrix:
16-
node-version: [18.x, 20.x, 22.x]
17-
1814
steps:
1915
- name: Checkout repository
2016
uses: actions/checkout@v4
2117

22-
- name: Setup Node.js ${{ matrix.node-version }}
23-
uses: actions/setup-node@v4
18+
- name: Setup Bun
19+
uses: oven-sh/setup-bun@v1
2420
with:
25-
node-version: ${{ matrix.node-version }}
26-
cache: 'npm'
21+
bun-version: 'latest'
2722

2823
- name: Install dependencies
29-
run: npm ci
24+
run: bun install --frozen-lockfile
3025

3126
- name: Type check
32-
run: npm run type-check
27+
run: bun run type-check
3328

3429
- name: Build project
35-
run: npm run build
30+
run: bun run build
3631

3732
- name: Run E2E tests
38-
run: npm run test:quick
33+
run: bun run test:quick
3934

4035
- name: Test configuration loading
4136
run: |
4237
echo "Testing basic configuration..."
43-
timeout 10s node build/index.js --config=tests/configs/basic.yaml || true
38+
timeout 10s bun build/index.js --config=tests/configs/basic.yaml || true
4439
echo "Testing JSON configuration..."
45-
timeout 10s node build/index.js --config=tests/configs/basic.json || true
40+
timeout 10s bun build/index.js --config=tests/configs/basic.json || true
4641
4742
- name: Test environment configuration
4843
run: |
4944
echo "Testing environment variables..."
50-
timeout 10s env MCP_SERVERS="test:npx:user-review-mcp" node build/index.js || true
45+
timeout 10s env MCP_SERVERS="test:npx:user-review-mcp" bun build/index.js || true
5146
5247
build-check:
5348
name: Build Verification
@@ -57,17 +52,16 @@ jobs:
5752
- name: Checkout repository
5853
uses: actions/checkout@v4
5954

60-
- name: Setup Node.js
61-
uses: actions/setup-node@v4
55+
- name: Setup Bun
56+
uses: oven-sh/setup-bun@v1
6257
with:
63-
node-version: '20.x'
64-
cache: 'npm'
58+
bun-version: 'latest'
6559

6660
- name: Install dependencies
67-
run: npm ci
61+
run: bun install --frozen-lockfile
6862

6963
- name: Build project
70-
run: npm run build
64+
run: bun run build
7165

7266
- name: Verify build artifacts
7367
run: |
@@ -76,9 +70,3 @@ jobs:
7670
[ -f "build/aggregator-server.js" ] || { echo "Missing aggregator-server.js"; exit 1; }
7771
[ -f "build/config.js" ] || { echo "Missing config.js"; exit 1; }
7872
echo "All build artifacts present ✅"
79-
80-
- name: Test package installation
81-
run: |
82-
npm pack
83-
npm install -g ./mcp-gateway-*.tgz
84-
echo "Package installation successful ✅"

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,26 @@ npm run test:quick # Fast, essential tests only
234234
npm run test # Full test suite (slower but comprehensive)
235235
```
236236

237+
## 🔐 Environment Variables
238+
239+
When using x402 payment middleware or certain MCP servers, you'll need to set environment variables:
240+
241+
```bash
242+
# Copy the example environment file
243+
cp env.example .env
244+
245+
# Edit .env and add your keys:
246+
# - CONTEXT7_API_KEY: Get from https://context7.com/dashboard
247+
# - X402_EVM_PRIVATE_KEY: Your EVM wallet private key for payments
248+
# - X402_TEST_PRIVATE_KEY: Test wallet key (for testing only)
249+
# - TEST_PRIVATE_KEY: Additional test key for unit tests
250+
```
251+
252+
**Security Notes:**
253+
- Never commit `.env` files or actual private keys to version control
254+
- Use environment variables or secure key management systems in production
255+
- For testing, generate test-only keys that don't hold real funds
256+
237257
## 📊 Example Output
238258

239259
When running with multiple servers, you'll see:

bun.lock

Lines changed: 972 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

env.example

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Environment variables required for x402 payment gateway
2+
3+
# Context7 API key for documentation MCP server
4+
# Get your API key from https://context7.com/dashboard
5+
CONTEXT7_API_KEY=
6+
7+
# EVM wallet private keys for x402 payments
8+
# WARNING: Never commit actual private keys to version control!
9+
# For production, use a secure key management system
10+
X402_EVM_PRIVATE_KEY=
11+
X402_TEST_PRIVATE_KEY=
12+
13+
# For testing only - use test network private keys
14+
# You can generate test keys using: npx ethereum-wallet-cli generate
15+
TEST_PRIVATE_KEY=
16+
17+
# Test API key for testing SSE configurations
18+
TEST_API_KEY=test-api-key-123
19+
20+
# Optional: Network configuration
21+
# X402_NETWORK=base-sepolia # or 'base' for mainnet
22+
23+
# Optional: Additional x402 configuration
24+
# X402_MAX_VALUE_MICRO_USDC=100000 # Max 0.10 USDC per request
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Production example: Context7 MCP server with x402 payment middleware
2+
#
3+
# Context7 provides up-to-date documentation for various libraries and frameworks.
4+
# By wrapping it with x402, you can monetize documentation lookups.
5+
#
6+
# Prerequisites:
7+
# 1. Get a Context7 API key from https://context7.com/dashboard
8+
# 2. Set up an EVM wallet with USDC on Base/Base-Sepolia
9+
# 3. Set environment variables:
10+
# - CONTEXT7_API_KEY: Your Context7 API key
11+
# - X402_CONTEXT7_PRIVATE_KEY: Your EVM wallet private key
12+
13+
mcpServers:
14+
# Context7 documentation service with x402 payments
15+
context7-docs:
16+
transport:
17+
type: stdio
18+
command: npx
19+
args:
20+
- "-y"
21+
- "@upstash/context7-mcp"
22+
- "--api-key"
23+
- "${CONTEXT7_API_KEY}"
24+
25+
# x402 payment configuration
26+
x402Middleware:
27+
enabled: true
28+
wallet:
29+
type: evm
30+
network: base # Use base-sepolia for testing
31+
privateKeyEnv: X402_CONTEXT7_PRIVATE_KEY
32+
maxValueMicroUSDC: "50000" # $0.05 per documentation lookup
33+
34+
# Optional namespace to avoid tool name conflicts
35+
namespace: "docs"
36+
37+
# Enable this server
38+
enabled: true
39+
40+
# Connection settings
41+
timeout: 60000 # 60 seconds - documentation lookups can take time
42+
retryAttempts: 3
43+
retryDelay: 2000
44+
45+
# Example usage from your AI coding assistant:
46+
#
47+
# User: "use docs.resolve-library-id to find the react library"
48+
# Assistant calls: docs.resolve-library-id with { libraryName: "react" }
49+
# Result: Returns Context7 library ID like "/facebook/react"
50+
#
51+
# User: "use docs.get-library-docs to get react hooks documentation"
52+
# Assistant calls: docs.get-library-docs with {
53+
# context7CompatibleLibraryID: "/facebook/react",
54+
# topic: "hooks",
55+
# tokens: 15000
56+
# }
57+
# Result: Returns comprehensive React hooks documentation
58+
#
59+
# Each successful call triggers a micro-payment via x402!

examples/mixed-transports.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ servers:
2121
apiKey: "YOUR_CONTEXT7_API_KEY"
2222
namespace: "docs"
2323
enabled: true
24+
# Enable per-server x402 payments (HTTP 402 handling with wallet)
25+
x402Middleware: true
2426

2527
# Local filesystem access
2628
- name: "filesystem"
@@ -41,6 +43,14 @@ servers:
4143
X-Company-Client: "mcp-aggregator"
4244
namespace: "db"
4345
enabled: false
46+
# Advanced configuration example
47+
x402Middleware:
48+
enabled: true
49+
wallet:
50+
type: "evm"
51+
network: "base" # or base-sepolia, solana-devnet, etc.
52+
privateKeyEnv: "X402_EVM_PRIVATE_KEY"
53+
maxValueMicroUSDC: "100000"
4454

4555
settings:
4656
enableToolConflictResolution: true

examples/x402-example.yaml

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Example configuration for x402 payment-enabled MCP servers
2+
3+
mcpServers:
4+
# Example 1: Simple x402 enablement
5+
# Uses default settings: EVM wallet, base-sepolia network, 0.10 USDC limit
6+
paid-weather-api:
7+
transport:
8+
type: http
9+
url: https://api.weather-provider.com/mcp
10+
x402Middleware: true
11+
# Requires X402_EVM_PRIVATE_KEY environment variable
12+
13+
# Example 2: Detailed x402 configuration
14+
paid-llm-api:
15+
transport:
16+
type: http
17+
url: https://api.llm-provider.com/v1/mcp
18+
headers:
19+
X-API-Version: "2024-01"
20+
x402Middleware:
21+
enabled: true
22+
wallet:
23+
type: evm # Wallet type (currently only 'evm' supported)
24+
network: base # Network: base, base-sepolia
25+
privateKeyEnv: LLM_WALLET_KEY # Custom env var for private key
26+
maxValueMicroUSDC: "500000" # Max 0.50 USDC per request
27+
28+
# Example 3: SSE endpoint with x402
29+
paid-realtime-data:
30+
transport:
31+
type: sse
32+
sseUrl: https://stream.data-provider.com/events
33+
x402Middleware:
34+
enabled: true
35+
wallet:
36+
type: evm
37+
network: base-sepolia
38+
privateKeyEnv: X402_EVM_PRIVATE_KEY
39+
maxValueMicroUSDC: "100000" # Max 0.10 USDC per request
40+
41+
# Example 4: Mixed setup - some servers with x402, some without
42+
free-server:
43+
transport:
44+
type: stdio
45+
command: node
46+
args: ["./free-mcp-server.js"]
47+
# No x402Middleware - this server is free to use
48+
49+
premium-server:
50+
transport:
51+
type: http
52+
url: https://premium.service.com/mcp
53+
x402Middleware: true # This server requires payment

0 commit comments

Comments
 (0)