Add powerful analytics tracking AND Stripe payments to your Cloudflare MCP servers with just 2 simple changes.
Track tool usage, user behavior, performance metrics, results, errors, AND process payments automatically while maintaining full compatibility with existing MCP tools built on Cloudflare's platform.
🚀 Quick Start
Add powerful analytics tracking AND Stripe payments to your Cloudflare MCP servers with just 2 simple changes.
Installation
npm install mcp-analytics
Choose Your Agent Type
Free Tools Only (Analytics Only)
import { AnalyticsMcpAgent } from 'mcp-analytics';
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
import { z } from 'zod';
export class MyMCP extends AnalyticsMcpAgent<Env, Record<string, never>, Props> {
server = new McpServer({
name: 'My Analytics-Only MCP',
version: '1.0.0',
});
async init() {
// Free tool with analytics tracking
this.analyticsTool(
'add',
'Add two numbers',
{ a: z.number(), b: z.number() },
async ({ a, b }) => ({
content: [{ type: 'text', text: `Result: ${a + b}` }],
})
);
}
}
Free + Paid Tools (Analytics + Payments)
import { AnalyticsPaidMcpAgent } from 'mcp-analytics';
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
import { z } from 'zod';
export class MyMCP extends AnalyticsPaidMcpAgent<Env, PaymentState, PaymentProps> {
server = new McpServer({
name: 'My Analytics + Payments MCP',
version: '1.0.0',
});
async init() {
// Free tool with analytics
this.analyticsTool(
'add',
'Add two numbers',
{ a: z.number(), b: z.number() },
async ({ a, b }) => ({
content: [{ type: 'text', text: `Result: ${a + b}` }],
})
);
// Paid tool with analytics + payments
this.analyticsPaidTool(
'generate_image',
'Generate AI image with premium quality',
{ prompt: z.string() },
async ({ prompt }) => ({
content: [{ type: 'text', text: `Generated image for: ${prompt}` }],
}),
{
checkout: {
success_url: 'https://yoursite.com/success',
line_items: [{ price: 'price_123', quantity: 1 }],
mode: 'payment',
},
paymentReason: 'High-quality AI image generation',
}
);
}
}
Environment Variables
# Required for analytics
MCP_ANALYTICS_API_KEY=your_analytics_api_key
# Required for paid tools
STRIPE_SECRET_KEY=your_stripe_secret_key
# Optional
MCP_ANALYTICS_ENABLED=true
ENVIRONMENT=production
🏗️ Platform Requirements
This SDK is specifically designed for Cloudflare MCP Agents:
✅ Cloudflare Workers - Deploys on Cloudflare's edge platform
✅ Cloudflare MCP Agent - Extends the McpAgent class from agents/mcp
✅ Durable Objects - Automatic session management and state persistence
✅ OAuth Provider Library - Built-in authentication with @cloudflare/workers-oauth-provider
Not compatible with:
❌ Local MCP servers (stdio-based)
❌ Other cloud platforms (AWS, GCP, Azure)
❌ Standard MCP SDK without Cloudflare extensions
🎯 What Gets Tracked Automatically
For All Tools (Free + Paid)
✅ Tool execution time - How long each tool takes to run
✅ Success/failure status - Which tools succeed or fail
✅ Input parameters - What data users provide (sensitive data auto-redacted)
✅ Tool results - Output data from tool executions (automatically sanitized)
✅ Error details - Full error information when tools fail
✅ User information - Automatic user identification from OAuth props
✅ Session tracking - Group tool calls by user session
✅ Server metadata - Server name and version automatically detected
# Analytics (Required for tracking)
MCP_ANALYTICS_API_KEY=your_analytics_api_key
# Payments (Required for AnalyticsPaidMcpAgent)
STRIPE_SECRET_KEY=your_stripe_secret_key
# Optional Settings
MCP_ANALYTICS_ENABLED=true # Enable/disable analytics
ENVIRONMENT=production # Environment tag
MCP_ANALYTICS_API_URL=https://custom.api.com # Custom analytics endpoint
Cloudflare Deployment
# Local development (.dev.vars file)
MCP_ANALYTICS_API_KEY=your_key_here
STRIPE_SECRET_KEY=your_stripe_key_here
# Production deployment
npx wrangler secret put MCP_ANALYTICS_API_KEY
npx wrangler secret put STRIPE_SECRET_KEY
📊 Benefits
For Developers
Minimal code changes - Only 2 changes needed: import and method
Full type safety - Complete TypeScript support with generics
Automatic server detection - No duplicate configuration needed
Automatic user tracking - Works with any OAuth provider
Payment integration - Stripe payments with zero additional code
Performance insights - See which tools are slow
Error monitoring - Get notified when tools fail
Revenue tracking - Track payments and revenue automatically
Flexible tracking - Disable result tracking for sensitive tools
For Business
User behavior analysis - Which tools are most popular?
Performance optimization - Identify bottlenecks
Revenue analytics - Track income by tool, user, time period