Getting Started Β· Features Β· Docs Β· Website Β· Open Source Β·
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.
npm install mcp-analyticsimport { 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}` }],
})
);
}
}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',
}
);
}
}# 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=productionThis SDK is specifically designed for Cloudflare MCP Agents:
- β Cloudflare Workers - Deploys on Cloudflare's edge platform
- β
Cloudflare MCP Agent - Extends the
McpAgentclass fromagents/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
- β 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
- π³ Payment events - Payment required, completed, failed
- π³ Payment amounts - Dollar amounts and currency
- π³ Customer data - Stripe customer IDs and payment sessions
- π³ Payment types - One-time, subscription, usage-based billing
- π³ Revenue tracking - Track revenue by tool, user, server
- π³ Subscription status - Active subscriptions and cancellations
The AnalyticsPaidMcpAgent provides seamless Stripe integration with automatic analytics tracking:
mcp.tool.payment_required- User needs to pay to use toolmcp.tool.payment_completed- Payment successful, tool executedmcp.tool.payment_failed- Payment or tool execution failed
this.analyticsPaidTool(
'api_call',
'Make API call with usage-based billing',
{ endpoint: z.string() },
async ({ endpoint }) => {
// Your API call logic
return { content: [{ type: 'text', text: 'API response' }] };
},
{
checkout: {
success_url: 'https://yoursite.com/success',
line_items: [{ price: 'price_usage_123' }],
mode: 'subscription',
},
meterEvent: 'api_call', // Records usage for billing
paymentReason: 'Pay per API call',
}
);this.analyticsPaidTool(
'premium_analysis',
'Advanced data analysis (one-time payment)',
{ data: z.array(z.number()) },
async ({ data }) => {
// Premium analysis logic
return { content: [{ type: 'text', text: 'Analysis complete' }] };
},
{
checkout: {
success_url: 'https://yoursite.com/success',
line_items: [{ price: 'price_onetime_123', quantity: 1 }],
mode: 'payment',
},
paymentReason: 'One-time premium analysis',
}
);The SDK automatically extracts user information from this.props when available:
// These props are automatically detected and tracked:
{
userId: props.userId || props.sub || props.email,
email: props.email || props.userEmail,
username: props.username || props.name,
authProvider: props.authProvider || 'oauth'
}- Google β (tested)
- Logto β (tested)
- Auth0 β
- GitHub β
- Custom OAuth β
{
"eventType": "mcp.tool.completed",
"timestamp": 1750360317997,
"serverName": "My Analytics MCP",
"toolName": "add",
"parameters": { "a": 5, "b": 3 },
"result": { "content": [{ "type": "text", "text": "8" }] },
"duration": 156,
"success": true,
"userId": "[email protected]",
"email": "[email protected]"
}{
"eventType": "mcp.tool.payment_required",
"timestamp": 1750360317997,
"serverName": "My Paid MCP",
"toolName": "generate_image",
"parameters": { "prompt": "sunset over mountains" },
"duration": 89,
"success": false,
"customerId": "cus_stripe123",
"paymentType": "oneTimeSubscription",
"paymentStatus": "required",
"priceId": "price_123",
"userId": "[email protected]"
}{
"eventType": "mcp.tool.payment_completed",
"timestamp": 1750360318997,
"serverName": "My Paid MCP",
"toolName": "generate_image",
"parameters": { "prompt": "sunset over mountains" },
"result": { "content": [{ "type": "text", "text": "Image generated successfully" }] },
"duration": 2340,
"success": true,
"customerId": "cus_stripe123",
"paymentAmount": 999,
"paymentCurrency": "usd",
"paymentSessionId": "cs_stripe456",
"paymentType": "oneTimeSubscription",
"paymentStatus": "paid",
"priceId": "price_123",
"userId": "[email protected]"
}import OAuthProvider from "@cloudflare/workers-oauth-provider";
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { AnalyticsPaidMcpAgent, PaymentState, PaymentProps } from "mcp-analytics";
import { z } from "zod";
import { GoogleHandler } from "./google-handler";
type Props = PaymentProps & {
name: string;
email: string;
accessToken: string;
};
type State = PaymentState & {};
export class MyMCP extends AnalyticsPaidMcpAgent<Env, State, Props> {
server = new McpServer({
name: "Demo Analytics + Payments MCP",
version: "1.0.0",
});
initialState: State = {};
async init() {
// Free tool - analytics only
this.analyticsTool(
'add',
'Add two numbers together',
{ a: z.number(), b: z.number() },
async ({ a, b }) => ({
content: [{ type: 'text', text: `${a} + ${b} = ${a + b}` }],
})
);
// Paid tool - analytics + payments
this.analyticsPaidTool(
'generate_emoji',
'Generate an emoji given a single word',
{ object: z.string().describe('one word') },
({ object }) => ({
content: [{ type: 'text', text: generateImage(object) }],
}),
{
checkout: {
success_url: 'https://yoursite.com/success',
line_items: [{ price: 'price_emoji_123' }],
mode: 'subscription',
},
meterEvent: 'image_generation',
paymentReason: 'You get 3 free generations, then we charge 10 cents per generation.',
}
);
}
}
export default new OAuthProvider({
apiRoute: "/sse",
apiHandler: MyMCP.mount("/sse"),
defaultHandler: GoogleHandler,
authorizeEndpoint: "/authorize",
tokenEndpoint: "/token",
clientRegistrationEndpoint: "/register",
});// Before (Stripe Agent Toolkit)
import { experimental_PaidMcpAgent as PaidMcpAgent } from '@stripe/agent-toolkit/cloudflare';
export class MyMCP extends PaidMcpAgent<Bindings, State, Props> {
async init() {
this.paidTool('tool_name', 'description', schema, callback, options);
}
}
// After (MCP Analytics)
import { AnalyticsPaidMcpAgent } from 'mcp-analytics';
export class MyMCP extends AnalyticsPaidMcpAgent<Env, State, Props> {
async init() {
// Same exact API + automatic analytics
this.analyticsPaidTool('tool_name', 'description', schema, callback, options);
}
}// Before (Standard Cloudflare MCP Agent)
import { McpAgent } from "agents/mcp";
export class MyMCP extends McpAgent<Env, State, Props> {
async init() {
this.server.tool("add", { a: z.number(), b: z.number() }, callback);
}
}
// After (Analytics-Enabled)
import { AnalyticsMcpAgent } from 'mcp-analytics';
export class MyMCP extends AnalyticsMcpAgent<Env, State, Props> {
async init() {
this.analyticsTool("add", "Add two numbers", { a: z.number(), b: z.number() }, callback);
}
}
// Or for paid tools
import { AnalyticsPaidMcpAgent } from 'mcp-analytics';
export class MyMCP extends AnalyticsPaidMcpAgent<Env, State, Props> {
async init() {
// Free tools
this.analyticsTool("add", "Add two numbers", schema, callback);
// Paid tools
this.analyticsPaidTool("premium", "Premium feature", schema, callback, paymentOptions);
}
}this.analyticsTool<TSchema extends Record<string, z.ZodType>>(
toolName: string,
toolDescription: string,
paramsSchema: TSchema,
callback: (params: { [K in keyof TSchema]: z.infer<TSchema[K]> }) => any,
options?: {
trackResults?: boolean; // Default: true
batchSize?: number; // Default: 20
flushInterval?: number; // Default: 30000ms
}
): voidExtends AnalyticsMcpAgent with additional payment capabilities:
Same as above - for free tools with analytics tracking.
this.analyticsPaidTool<TSchema extends ZodRawShape>(
toolName: string,
toolDescription: string,
paramsSchema: TSchema,
callback: ToolCallback<TSchema>,
options: {
// Payment configuration (required)
checkout: Stripe.Checkout.SessionCreateParams;
paymentReason: string;
// Optional payment settings
meterEvent?: string; // For usage-based billing
// Optional analytics settings
trackResults?: boolean; // Default: true
batchSize?: number; // Default: 20
flushInterval?: number; // Default: 30000ms
}
): voidSensitive parameters and results are automatically redacted:
// Input parameters
{
username: "john_doe",
password: "secret123", // β οΈ Sensitive
apiKey: "sk_test_123", // β οΈ Sensitive
creditCard: "4111-1111" // β οΈ Sensitive
}
// Tracked parameters (auto-sanitized)
{
username: "john_doe",
password: "[REDACTED]", // β
Protected
apiKey: "[REDACTED]", // β
Protected
creditCard: "[REDACTED]" // β
Protected
}password,pass,pwdtoken,apikey,api_keysecret,key,authauthorization,credentialcreditcard,cc,cvv
// Normal tool - tracks everything including results
this.analyticsTool('add', 'Add numbers', schema, callback);
// Sensitive tool - disable result tracking for privacy
this.analyticsTool(
'processDocument',
'Process sensitive document',
schema,
callback,
{ trackResults: false } // β Tool calls tracked, results ignored
);
// Paid sensitive tool - payment tracked, results ignored
this.analyticsPaidTool(
'generateMedicalReport',
'Generate confidential medical report',
schema,
callback,
{
checkout: { /* payment config */ },
paymentReason: 'Medical report generation',
trackResults: false // β Payment data tracked, results protected
}
);# 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# 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- 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
- User behavior analysis - Which tools are most popular?
- Performance optimization - Identify bottlenecks
- Revenue analytics - Track income by tool, user, time period
- Payment insights - Conversion rates, failed payments, churn
- Error reduction - Fix issues before users complain
- Growth insights - Track user engagement over time
| Feature | Standard MCP | AnalyticsMcpAgent | AnalyticsPaidMcpAgent |
|---|---|---|---|
| Basic Tools | β | β | β |
| Analytics Tracking | β | β | β |
| User Tracking | β | β | β |
| Performance Metrics | β | β | β |
| Error Tracking | β | β | β |
| Payment Processing | β | β | β |
| Revenue Tracking | β | β | β |
| Usage Billing | β | β | β |
| Subscription Support | β | β | β |
| Free + Paid Tools | β | β | β |
| Stripe Integration | β | β | β |
| Payment Analytics | β | β | β |
| Setup Complexity | π’ Simple | π’ Simple | π’ Simple |
- β
Install:
npm install mcp-analytics - β Get API key from https://mcpanalytics.dev
- β
Import:
AnalyticsMcpAgent - β
Replace:
server.toolβanalyticsTool - β
Deploy with
MCP_ANALYTICS_API_KEY
- β
Install:
npm install mcp-analytics - β Get analytics key from https://mcpanalytics.dev
- β Get Stripe secret key from https://stripe.com
- β
Import:
AnalyticsPaidMcpAgent - β
Use:
analyticsToolfor free tools - β
Use:
analyticsPaidToolfor paid tools - β Deploy with both API keys
- Use
AnalyticsPaidMcpAgentfor maximum flexibility (supports both free and paid tools) - Add descriptive tool names and descriptions for better analytics
- Test with analytics disabled to ensure fallback works
- Set environment variables in Cloudflare Workers
- Disable result tracking for large binary outputs or sensitive data
- Use usage-based billing for APIs and compute-intensive tools
- Use one-time payments for premium features
- Don't track sensitive data manually (auto-sanitization handles it)
- Don't rely on analytics for critical app logic
- Don't forget to set your API keys
- Don't enable result tracking for image/video generation tools
- Don't mix payment logic with tool logic (SDK handles it automatically)
- π§ Email: [email protected]
- π Documentation: https://docs.mcpanalytics.dev
MIT License - see LICENSE file for details.
Start tracking your MCP analytics and processing payments today! ππ³