summaryrefslogtreecommitdiff
path: root/mcp-servers/token-gated-mcp-server/README.md
blob: a319b55683404188ff2d59923348f877010bebd9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
# Token-Gated MCP Server Claude Code Configuration 🔐

A comprehensive Claude Code configuration for building token-gated MCP servers using FastMCP and the Radius MCP SDK. Enable decentralized, token-based access control for your AI tools with just 3 lines of code.

## ✨ Features

This configuration provides comprehensive support for:

- **Token-Gated Access** - ERC-1155 token-based authorization
- **FastMCP Integration** - Rapid MCP server development  
- **Radius MCP SDK** - Cryptographic proof verification
- **Web3 Authentication** - EIP-712 signature validation
- **Multi-Tier Pricing** - Different token requirements per tool
- **Complete Development Environment** - Agents, commands, hooks, and settings

## 📦 Installation

1. Copy the complete configuration to your token-gated MCP server project:

```bash
# Copy the entire configuration
cp -r token-gated-mcp-server/.claude your-mcp-project/
cp token-gated-mcp-server/CLAUDE.md your-mcp-project/

# Make hook scripts executable
chmod +x your-mcp-project/.claude/hooks/*.sh
```

2. The configuration will be automatically loaded when you start Claude Code.

## 📁 Configuration Structure

```text
.claude/
├── settings.json           # Main configuration with permissions, env vars, hooks
├── agents/                 # Specialized AI subagents
│   ├── radius-sdk-expert.md      # Radius MCP SDK implementation expert
│   ├── fastmcp-builder.md        # FastMCP server development specialist
│   ├── auth-flow-debugger.md     # Authentication debugging expert
│   ├── token-economics-designer.md # Token tier design specialist
│   └── web3-security-auditor.md  # Web3 security expert
├── commands/               # Custom slash commands
│   ├── setup-token-gate.md       # Set up complete token-gated server
│   ├── test-auth.md              # Test authentication flow
│   ├── create-tool.md            # Create new token-gated tool
│   ├── debug-proof.md            # Debug proof verification
│   ├── deploy-local.md           # Deploy locally with ngrok
│   └── validate-config.md        # Validate configuration
└── hooks/                  # Automation scripts
    ├── validate-token-config.sh  # Validate token configuration
    ├── format-typescript.sh      # Auto-format TypeScript files
    ├── log-mcp-commands.sh       # Log MCP commands for debugging
    └── production-safety.sh      # Production safety checks

## 🤖 Specialized Agents (5 Expert Agents)

| Agent | Description | Use Cases |
|-------|-------------|-----------|
| `radius-sdk-expert` | Radius MCP SDK implementation expert | Token protection, proof verification, multi-token patterns |
| `fastmcp-builder` | FastMCP server development specialist | Server setup, tool/resource/prompt creation, transport configuration |
| `auth-flow-debugger` | Authentication flow debugging expert | EVMAUTH errors, proof validation, token verification |
| `token-economics-designer` | Token economics and tier design specialist | Pricing models, access tiers, token distribution |
| `web3-security-auditor` | Web3 security expert | Smart contract safety, cryptographic operations, security audits |

## 🛠️ Commands (6 Powerful Commands)

| Command | Description | Usage |
|---------|-------------|-------|
| `/setup-token-gate` | Set up complete token-gated MCP server | `/setup-token-gate [basic\|full\|testnet]` |
| `/test-auth` | Test authentication flow end-to-end | `/test-auth [tool-name] [token-id]` |
| `/create-tool` | Create new token-gated tool | `/create-tool <tool-name> <token-id> [tier]` |
| `/debug-proof` | Debug proof verification issues | `/debug-proof` |
| `/deploy-local` | Deploy locally with ngrok | `/deploy-local` |
| `/validate-config` | Validate token-gating configuration | `/validate-config` |

## 🪝 Automation Hooks

### Pre-Tool Use Hooks
- **Token Configuration Validator** (`validate-token-config.sh`)
  - Validates contract address format (0x + 40 hex)
  - Checks for hardcoded private keys
  - Warns about debug mode in production
  - Verifies __evmauth parameter inclusion

- **MCP Command Logger** (`log-mcp-commands.sh`)
  - Logs FastMCP and ngrok commands
  - Tracks token configuration checks
  - Blocks dangerous commands (rm -rf, npm publish in dev)
  - Provides helpful tips for development

### Post-Tool Use Hooks
- **TypeScript Formatter** (`format-typescript.sh`)
  - Auto-formats with Prettier
  - Validates token protection implementation
  - Checks for proper error handling
  - Counts protected tools in server files

### Stop Hooks
- **Production Safety Check** (`production-safety.sh`)
  - Environment-specific warnings
  - Debug mode detection
  - Uncommitted changes reminder
  - Token configuration summary

## ⚙️ Configuration Details

### Security Permissions

The configuration includes comprehensive permissions for safe development:

**Allowed Operations:**
- All standard file operations (Read, Write, Edit, MultiEdit)
- Search and navigation tools (Grep, Glob, LS)
- Development commands (npm run dev/build/test/lint/typecheck)
- Package management (npm install, npm ci)
- FastMCP and ngrok for testing
- Git operations for version control
- Docker commands for containerization
- RPC endpoint testing

**Denied Operations:**
- Reading/writing private keys or secrets
- Destructive commands (rm -rf)
- Publishing to npm in development
- Modifying production environment files
- Unsafe curl operations (POST, PUT, DELETE)

### Environment Variables

Pre-configured for token-gated development:

- `EVMAUTH_CONTRACT_ADDRESS` - ERC-1155 contract (0x5448Dc20ad9e0cDb5Dd0db25e814545d1aa08D96)
- `EVMAUTH_CHAIN_ID` - Radius Testnet (1223953)
- `EVMAUTH_RPC_URL` - Blockchain RPC endpoint (https://rpc.testnet.radiustech.xyz)
- `EVMAUTH_TOKEN_ID` - Required token ID (default: 1)
- `NODE_ENV` - Environment mode (development/production)
- `DEBUG` - Debug mode (false by default, never enable in production!)
- `RADIUS_TESTNET` - Testnet indicator (true)

### Status Line

A custom status line displays real-time token-gating information:
```text

🔐 Token-Gated MCP | Chain: 1223953 | Token: 1 | dev

```

## 🚀 Usage Examples

### Building a Token-Gated MCP Server

```bash
# 1. Set up the project
> /setup full

# 2. Create a token-gated tool
> /create-tool premium_analytics 101

# 3. Test authentication flow
> /test-auth

# 4. Deploy locally with ngrok
> /deploy-local

# 5. Connect in claude.ai
# Use the ngrok URL + /mcp endpoint
```

### The 3-Line Integration

```typescript
// That's all you need!
const radius = new RadiusMcpSdk({ 
  contractAddress: '0x5448Dc20ad9e0cDb5Dd0db25e814545d1aa08D96' 
});

server.addTool({
  name: 'premium_tool',
  handler: radius.protect(101, yourHandler)  // Token-gated!
});
```

### Testing Authentication Flow

```bash
# Debug authentication issues
> /debug-proof

# The debugger will:
# - Validate proof structure
# - Check signature verification
# - Test token ownership
# - Identify configuration issues
```

## 📊 Technology Stack

Optimized for:

- **TypeScript** & Node.js
- **FastMCP v3.0+** for MCP servers
- **Radius MCP SDK** for token-gating
- **Viem** for Ethereum interactions
- **Zod** for schema validation
- **EIP-712** for cryptographic signatures
- **Radius Network Testnet** (Chain ID: 1223953)

## 🎯 Key Features

### Token-Based Access Control

- ERC-1155 multi-token support
- ANY token logic (user needs one of many)
- Tiered access patterns
- Dynamic token requirements

### Authentication Flow

- Cryptographic proof verification
- EIP-712 signature validation
- 30-second proof expiry
- Replay attack prevention

### Developer Experience

- 3-line integration
- AI-friendly error messages
- Automatic retry guidance
- Built-in caching

### Production Ready

- Docker containerization
- Health check endpoints
- Structured logging
- Performance monitoring

## 🔧 Customization

Edit `.claude/settings.json` to customize:

- Token contract addresses
- Chain ID for different networks
- Token tier configurations
- Cache settings
- Debug options

## 📝 Best Practices

This configuration enforces:

1. **Security First** - Cryptographic verification, fail-closed design
2. **Simple Integration** - Minimal code for maximum protection
3. **AI-Friendly Errors** - Clear guidance for authentication
4. **Performance** - Caching, batch checks, optimization
5. **Testing** - Comprehensive auth flow validation
6. **Production Ready** - Monitoring, health checks, logging

## 🐛 Troubleshooting

### Common Issues

**EVMAUTH_PROOF_MISSING errors:**

```bash
# Check Radius MCP Server connection
# Ensure __evmauth parameter is included
# Verify proof hasn't expired (30 seconds)
```

**Token verification failures:**

```bash
# Check contract address configuration
echo $EVMAUTH_CONTRACT_ADDRESS

# Verify chain ID
echo $EVMAUTH_CHAIN_ID

# Test RPC connection
curl $EVMAUTH_RPC_URL
```

**Debug authentication flow:**

```bash
/debug-proof
```

## 🌟 Example Projects

### Simple Token-Gated Timestamp

```typescript
server.addTool({
  name: 'get_timestamp',
  description: `Get current time (requires token ${TOKEN_ID})`,
  parameters: z.object({
    format: z.enum(['unix', 'iso', 'readable'])
  }),
  handler: radius.protect(TOKEN_ID, async (args) => {
    return new Date().toISOString();
  })
});
```

### Multi-Tier Analytics

```typescript
const TOKENS = {
  BASIC: 101,
  PREMIUM: 102,
  ENTERPRISE: [201, 202, 203]
};

// Different tools for different tiers
server.addTool({
  name: 'basic_analytics',
  handler: radius.protect(TOKENS.BASIC, basicHandler)
});

server.addTool({
  name: 'enterprise_analytics',
  handler: radius.protect(TOKENS.ENTERPRISE, enterpriseHandler)
});
```

## 🔗 Integration with Radius MCP Server

This SDK works with the **Radius MCP Server** for complete token-gating:

1. **Radius MCP Server** (one per AI client)
   - OAuth authentication
   - Wallet management via Privy
   - Proof generation
   - Token purchases

2. **Your MCP Server** (using this config)
   - Proof verification
   - Token ownership checks
   - Tool execution
   - Error guidance

## 📚 Resources

- [FastMCP Documentation](https://github.com/punkpeye/fastmcp)
- [Radius MCP SDK](https://github.com/radiustechsystems/mcp-sdk)
- [Model Context Protocol](https://modelcontextprotocol.io)
- [Radius Network Testnet](https://docs.radiustech.xyz)
- [EIP-712 Specification](https://eips.ethereum.org/EIPS/eip-712)

## 🎉 Quick Start Example

```bash
# 1. Install dependencies
npm install fastmcp @radiustechsystems/mcp-sdk zod

# 2. Create server.ts
cat > server.ts << 'EOF'
import { FastMCP } from 'fastmcp';
import { RadiusMcpSdk } from '@radiustechsystems/mcp-sdk';
import { z } from 'zod';

const radius = new RadiusMcpSdk({
  contractAddress: '0x5448Dc20ad9e0cDb5Dd0db25e814545d1aa08D96'
});

const server = new FastMCP({ name: 'My Token Server' });

server.addTool({
  name: 'premium_tool',
  description: 'Premium feature (token required)',
  parameters: z.object({ input: z.string() }),
  handler: radius.protect(1, async (args) => {
    return `Premium result for: ${args.input}`;
  })
});

server.start({ 
  transportType: 'httpStream',
  httpStream: { port: 3000 }
});
EOF

# 3. Run the server
npx tsx server.ts

# 4. Test with ngrok
ngrok http 3000

# 5. Connect in claude.ai with the ngrok URL + /mcp
```

## 🎯 What Makes This Configuration Special

### Complete Development Environment

- **5 Expert Agents** - Specialized AI assistants for every aspect of token-gating
- **6 Power Commands** - From setup to deployment, all automated
- **4 Smart Hooks** - Automatic validation, formatting, and safety checks
- **Comprehensive Settings** - Pre-configured for Radius Testnet with security best practices

### Key Capabilities

1. **3-Line Integration** - Token-gate any tool with minimal code
2. **AI-Friendly Errors** - Guide Claude through authentication automatically
3. **Production Ready** - Built-in safety checks and deployment tools
4. **Security First** - Automatic detection of private keys and unsafe patterns
5. **Developer Experience** - Auto-formatting, logging, and debugging tools

### Perfect For

- Building token-gated MCP servers with FastMCP
- Implementing ERC-1155 token access control
- Creating tiered access models for AI tools
- Deploying to Radius Network (testnet and mainnet)
- Learning Web3 authentication patterns

---

**Built for the decentralized AI tool marketplace** 🚀

*Enable token-gated access to your MCP tools with minimal code and maximum security.*

**Configuration Version:** 1.0.0 | **Compatible with:** FastMCP 3.0+, Radius MCP SDK 1.0+