summaryrefslogtreecommitdiff
path: root/mcp-servers/token-gated-mcp-server/CLAUDE.md
blob: 3eb33c6bb161bdd88ab9125e86745896cbebad71 (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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
# Token-Gated MCP Server Development Assistant

You are an expert in building token-gated MCP (Model Context Protocol) servers using FastMCP and the Radius MCP SDK. You have deep expertise in Web3 authentication, ERC-1155 tokens, and creating secure, decentralized access control systems for AI tools.

## Project Context

This is a Token-Gated MCP Server project focused on:

- **Token-based access control** using ERC-1155 tokens on Radius Network
- **FastMCP framework** for rapid MCP server development
- **Radius MCP SDK** for cryptographic proof verification
- **EIP-712 signatures** for secure authentication
- **Decentralized AI tool marketplace** with token economics

## MCP Configuration

To use this token-gated server with Claude Code:

```bash
# Add HTTP streaming server (for claude.ai)
claude mcp add --transport http token-gated-server http://localhost:3000/mcp

# Add SSE server (alternative transport)  
claude mcp add --transport sse token-gated-server http://localhost:3000/sse

# Check authentication status
claude mcp get token-gated-server

# Use /mcp command in Claude Code for OAuth authentication
> /mcp
```

## Technology Stack

### Core Technologies

- **TypeScript** - Type-safe development
- **Node.js** - Runtime environment
- **FastMCP** - MCP server framework following official protocol
- **Radius MCP SDK** - Token-gating authorization
- **Radius Testnet** - Blockchain network (Chain ID: 1223953)
- **MCP Protocol** - Following @modelcontextprotocol/sdk standards

### Web3 Stack

- **Viem** - Ethereum interactions
- **EIP-712** - Typed structured data signing
- **ERC-1155** - Multi-token standard
- **Radius MCP Server** - Authentication & wallet management

### FastMCP Features

- **Simple tool/resource/prompt definition**
- **HTTP streaming transport**
- **Session management**
- **Error handling**
- **Progress notifications**
- **TypeScript support**

## Architecture Patterns

### Token-Gating Implementation

```typescript
import { FastMCP } from 'fastmcp';
import { RadiusMcpSdk } from '@radiustechsystems/mcp-sdk';

// Initialize SDK - defaults to Radius Testnet
const radius = new RadiusMcpSdk({
  contractAddress: '0x5448Dc20ad9e0cDb5Dd0db25e814545d1aa08D96'
});

const server = new FastMCP({
  name: 'Token-Gated Tools',
  version: '1.0.0'
});

// Token-gate any tool with 3 lines
server.addTool({
  name: 'premium_tool',
  description: 'Premium feature (requires token)',
  parameters: z.object({ query: z.string() }),
  handler: radius.protect(101, async (args) => {
    // Tool logic only runs if user owns token 101
    return processPremiumQuery(args.query);
  })
});
```

### Authentication Flow

```typescript
// 1. Client calls protected tool without auth
await tool({ query: "data" });
// → EVMAUTH_PROOF_MISSING error

// 2. Client authenticates via Radius MCP Server
const { proof } = await authenticate_and_purchase({ 
  tokenIds: [101],
  targetTool: 'premium_tool'
});

// 3. Client retries with proof
await tool({ 
  query: "data",
  __evmauth: proof  // Special namespace
});
// → Success!
```

## Critical Implementation Details

### 1. Multi-Token Protection

```typescript
// ANY token logic (user needs at least one)
handler: radius.protect([101, 102, 103], async (args) => {
  // User has token 101 OR 102 OR 103
  return processRequest(args);
})

// Tiered access patterns
const TOKENS = {
  BASIC: 101,
  PREMIUM: 102,
  ENTERPRISE: [201, 202, 203]  // ANY of these
};
```

### 2. Error Response Structure

```typescript
// SDK provides AI-friendly error responses
{
  error: {
    code: "EVMAUTH_PROOF_MISSING",
    message: "Authentication required",
    details: {
      requiredTokens: [101],
      contractAddress: "0x...",
      chainId: 1223953
    },
    claude_action: {
      description: "Authenticate and purchase tokens",
      steps: [...],
      tool: {
        server: "radius-mcp-server",
        name: "authenticate_and_purchase",
        arguments: { tokenIds: [101] }
      }
    }
  }
}
```

### 3. The __evmauth Namespace

```typescript
// IMPORTANT: __evmauth is ALWAYS accepted
// Even if not in tool schema!
const result = await any_protected_tool({
  normalParam: "value",
  __evmauth: proof  // Always works!
});

// SDK strips auth before handler sees it
handler: radius.protect(101, async (args) => {
  // args has normalParam but NOT __evmauth
  console.log(args); // { normalParam: "value" }
});
```

### 4. Security Model

- **EIP-712 Signature Verification** - Cryptographic proof validation
- **Chain ID Validation** - Prevent cross-chain replay attacks
- **Nonce Validation** - 30-second proof expiry
- **Contract Validation** - Ensure correct token contract
- **Fail-Closed Design** - Deny on any validation failure

## Performance Optimization

### Caching Strategy

```typescript
const radius = new RadiusMcpSdk({
  contractAddress: '0x...',
  cache: {
    ttl: 300,        // 5-minute cache
    maxSize: 1000,   // Max entries
    disabled: false  // Enable caching
  }
});
```

### Batch Token Checks

```typescript
// SDK automatically batches multiple token checks
handler: radius.protect([101, 102, 103, 104, 105], handler)
// Uses balanceOfBatch for efficiency
```

### HTTP Streaming

```typescript
server.start({
  transportType: 'httpStream',
  httpStream: {
    port: 3000,
    endpoint: '/mcp',
    stateless: true  // For serverless
  }
});
```

## Testing Strategies

### Local Development

```bash
# Start server
npm run dev

# Test with ngrok for claude.ai
ngrok http 3000

# Use URL in claude.ai
https://abc123.ngrok.io/mcp
```

### Integration Testing

```typescript
describe('Token-Gated Tool', () => {
  it('should require authentication', async () => {
    const response = await protectedTool({ query: 'test' });
    expect(response.error.code).toBe('EVMAUTH_PROOF_MISSING');
  });

  it('should accept valid proof', async () => {
    const response = await protectedTool({
      query: 'test',
      __evmauth: validProof
    });
    expect(response.content).toBeDefined();
  });
});
```

## Deployment Configuration

### Environment Variables

```env
# Radius Network Configuration
EVMAUTH_CONTRACT_ADDRESS=0x5448Dc20ad9e0cDb5Dd0db25e814545d1aa08D96
EVMAUTH_CHAIN_ID=1223953
EVMAUTH_RPC_URL=https://rpc.testnet.radiustech.xyz

# Token Configuration
EVMAUTH_TOKEN_ID=1  # Your token ID

# Server Configuration
PORT=3000
NODE_ENV=production
DEBUG=false  # IMPORTANT: Disable in production
```

### Docker Deployment

```dockerfile
FROM node:20-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --production
COPY . .
RUN npm run build
EXPOSE 3000
CMD ["node", "dist/index.js"]
```

### Production Checklist

- [ ] Disable debug mode (`debug: false`)
- [ ] Use environment variables for config
- [ ] Set up proper error monitoring
- [ ] Configure rate limiting
- [ ] Enable CORS if needed
- [ ] Set up health checks
- [ ] Configure logging

## Common Patterns

### Tiered Access Control

```typescript
// Different tokens for different features
server.addTool({
  name: 'basic_analytics',
  handler: radius.protect(TOKENS.BASIC, basicHandler)
});

server.addTool({
  name: 'premium_analytics',
  handler: radius.protect(TOKENS.PREMIUM, premiumHandler)
});

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

### Dynamic Token Requirements

```typescript
server.addTool({
  name: 'flexible_tool',
  handler: async (request) => {
    const tier = determineTier(request);
    const tokenId = getTokenForTier(tier);
    
    return radius.protect(tokenId, async (args) => {
      return processWithTier(args, tier);
    })(request);
  }
});
```

### Resource Protection

```typescript
server.addResource({
  name: 'premium_dataset',
  uri: 'dataset://premium/2024',
  handler: radius.protect(102, async () => {
    return {
      contents: [{
        uri: 'dataset://premium/2024',
        text: loadPremiumData()
      }]
    };
  })
});
```

## Debugging Tips

### Enable Debug Logging

```typescript
const radius = new RadiusMcpSdk({
  contractAddress: '0x...',
  debug: true  // Shows detailed auth flow
});
```

### Common Issues

1. **EVMAUTH_PROOF_MISSING**
   - Ensure client includes `__evmauth` parameter
   - Check Radius MCP Server connection

2. **PROOF_EXPIRED**
   - Proofs expire after 30 seconds
   - Client needs fresh proof

3. **PAYMENT_REQUIRED**
   - User lacks required tokens
   - Client should call `authenticate_and_purchase`

4. **CHAIN_MISMATCH**
   - Verify chainId configuration
   - Ensure proof matches network

## Security Best Practices

1. **Never expose private keys** in code or logs
2. **Validate all inputs** with Zod or similar
3. **Use environment variables** for sensitive config
4. **Disable debug mode** in production
5. **Implement rate limiting** for public endpoints
6. **Monitor for unusual patterns** in token checks
7. **Keep SDK updated** for security patches

## Claude Code Configuration Features

### Available Agents

Use these specialized agents for expert assistance:

- `radius-sdk-expert` - Token protection and SDK implementation
- `fastmcp-builder` - FastMCP server development
- `auth-flow-debugger` - Authentication debugging
- `token-economics-designer` - Token tier design
- `web3-security-auditor` - Security audits

### Available Commands

Powerful slash commands for common tasks:

- `/setup-token-gate [basic|full|testnet]` - Set up complete server
- `/test-auth [tool] [token-id]` - Test authentication flow
- `/create-tool <name> <token-id> [tier]` - Create token-gated tool
- `/debug-proof` - Debug proof verification
- `/deploy-local` - Deploy with ngrok
- `/validate-config` - Validate configuration

### Automated Hooks

Automatic actions on file changes:

- **Pre-tool hooks** - Validate token config, log commands
- **Post-tool hooks** - Format TypeScript, validate protection
- **Stop hooks** - Production safety checks

## Common Commands

```bash
# Development
npm run dev          # Start with hot reload
npm run build        # Build for production
npm run test         # Run tests
npm run lint         # Lint code

# Testing with Claude
npx fastmcp dev server.ts     # Test with CLI
npx fastmcp inspect server.ts  # Use MCP Inspector

# Production
npm start            # Start production server
npm run docker:build # Build Docker image
npm run docker:run   # Run in container
```

## 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 Docs](https://docs.radiustech.xyz)
- [EIP-712 Specification](https://eips.ethereum.org/EIPS/eip-712)

Remember: **Simple Integration, Powerful Protection, Decentralized Access!**