-
Notifications
You must be signed in to change notification settings - Fork 266
Expand file tree
/
Copy pathserver.ts
More file actions
224 lines (193 loc) · 6.12 KB
/
server.ts
File metadata and controls
224 lines (193 loc) · 6.12 KB
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
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import type { ReadResourceResult } from "@modelcontextprotocol/sdk/types.js";
import fs from "node:fs/promises";
import path from "node:path";
import { z } from "zod";
import {
registerAppTool,
registerAppResource,
RESOURCE_MIME_TYPE,
RESOURCE_URI_META_KEY,
} from "@modelcontextprotocol/ext-apps/server";
import { makeToolResult, startServer } from "../shared/server-utils.js";
const DIST_DIR = path.join(import.meta.dirname, "dist");
// Schemas - types are derived from these using z.infer
const GetCohortDataInputSchema = z.object({
metric: z
.enum(["retention", "revenue", "active"])
.optional()
.default("retention"),
periodType: z.enum(["monthly", "weekly"]).optional().default("monthly"),
cohortCount: z.number().min(3).max(24).optional().default(12),
maxPeriods: z.number().min(3).max(24).optional().default(12),
});
const CohortCellSchema = z.object({
cohortIndex: z.number(),
periodIndex: z.number(),
retention: z.number(),
usersRetained: z.number(),
usersOriginal: z.number(),
});
const CohortRowSchema = z.object({
cohortId: z.string(),
cohortLabel: z.string(),
originalUsers: z.number(),
cells: z.array(CohortCellSchema),
});
const CohortDataSchema = z.object({
cohorts: z.array(CohortRowSchema),
periods: z.array(z.string()),
periodLabels: z.array(z.string()),
metric: z.string(),
periodType: z.string(),
generatedAt: z.string(),
});
// Types derived from schemas
type CohortCell = z.infer<typeof CohortCellSchema>;
type CohortRow = z.infer<typeof CohortRowSchema>;
type CohortData = z.infer<typeof CohortDataSchema>;
// Internal types (not part of API schema)
interface RetentionParams {
baseRetention: number;
decayRate: number;
floor: number;
noise: number;
}
// Retention curve generator using exponential decay
function generateRetention(period: number, params: RetentionParams): number {
if (period === 0) return 1.0;
const { baseRetention, decayRate, floor, noise } = params;
const base = baseRetention * Math.exp(-decayRate * (period - 1)) + floor;
const variation = (Math.random() - 0.5) * 2 * noise;
return Math.max(0, Math.min(1, base + variation));
}
// Generate cohort data
function generateCohortData(
metric: string,
periodType: string,
cohortCount: number,
maxPeriods: number,
): CohortData {
const now = new Date();
const cohorts: CohortRow[] = [];
const periods: string[] = [];
const periodLabels: string[] = [];
// Generate period headers
for (let i = 0; i < maxPeriods; i++) {
periods.push(`M${i}`);
periodLabels.push(i === 0 ? "Month 0" : `Month ${i}`);
}
// Retention parameters vary by metric type
const paramsMap: Record<string, RetentionParams> = {
retention: {
baseRetention: 0.75,
decayRate: 0.12,
floor: 0.08,
noise: 0.04,
},
revenue: { baseRetention: 0.7, decayRate: 0.1, floor: 0.15, noise: 0.06 },
active: { baseRetention: 0.6, decayRate: 0.18, floor: 0.05, noise: 0.05 },
};
const params = paramsMap[metric] ?? paramsMap.retention;
// Generate cohorts (oldest first)
for (let c = 0; c < cohortCount; c++) {
const cohortDate = new Date(now);
cohortDate.setMonth(cohortDate.getMonth() - (cohortCount - 1 - c));
const cohortId = `${cohortDate.getFullYear()}-${String(cohortDate.getMonth() + 1).padStart(2, "0")}`;
const cohortLabel = cohortDate.toLocaleDateString("en-US", {
month: "short",
year: "numeric",
});
// Random cohort size: 1000-5000 users
const originalUsers = Math.floor(1000 + Math.random() * 4000);
// Number of periods this cohort has data for (newer cohorts have fewer periods)
const periodsAvailable = cohortCount - c;
const cells: CohortCell[] = [];
let previousRetention = 1.0;
for (let p = 0; p < Math.min(periodsAvailable, maxPeriods); p++) {
// Retention must decrease or stay same (with small exceptions for noise)
let retention = generateRetention(p, params);
retention = Math.min(retention, previousRetention + 0.02);
previousRetention = retention;
cells.push({
cohortIndex: c,
periodIndex: p,
retention,
usersRetained: Math.round(originalUsers * retention),
usersOriginal: originalUsers,
});
}
cohorts.push({ cohortId, cohortLabel, originalUsers, cells });
}
return {
cohorts,
periods,
periodLabels,
metric,
periodType,
generatedAt: new Date().toISOString(),
};
}
function createServer(): McpServer {
const server = new McpServer({
name: "Cohort Heatmap Server",
version: "1.0.0",
});
// Register tool and resource
const resourceUri = "ui://get-cohort-data/mcp-app.html";
registerAppTool(
server,
"get-cohort-data",
{
title: "Get Cohort Retention Data",
description:
"Returns cohort retention heatmap data showing customer retention over time by signup month",
inputSchema: GetCohortDataInputSchema.shape,
_meta: { [RESOURCE_URI_META_KEY]: resourceUri },
},
async ({ metric, periodType, cohortCount, maxPeriods }) => {
const data = generateCohortData(
metric,
periodType,
cohortCount,
maxPeriods,
);
return makeToolResult(data);
},
);
registerAppResource(
server,
resourceUri,
resourceUri,
{ mimeType: RESOURCE_MIME_TYPE },
async (): Promise<ReadResourceResult> => {
const html = await fs.readFile(
path.join(DIST_DIR, "mcp-app.html"),
"utf-8",
);
return {
contents: [
{
uri: resourceUri,
mimeType: RESOURCE_MIME_TYPE,
text: html,
},
],
};
},
);
return server;
}
async function main() {
if (process.argv.includes("--stdio")) {
await createServer().connect(new StdioServerTransport());
} else {
const port = parseInt(process.env.PORT ?? "3104", 10);
await startServer(createServer, { port, name: "Cohort Heatmap Server" });
}
}
main().catch((e) => {
console.error(e);
process.exit(1);
});