forked from modelcontextprotocol/ext-apps
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmcp-app.ts
More file actions
407 lines (367 loc) · 11.7 KB
/
mcp-app.ts
File metadata and controls
407 lines (367 loc) · 11.7 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
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
/**
* @file Customer Segmentation Explorer - interactive scatter/bubble visualization
*/
import { App, PostMessageTransport } from "@modelcontextprotocol/ext-apps";
import { Chart, registerables } from "chart.js";
import "./global.css";
import "./mcp-app.css";
import type { Customer, SegmentSummary, MetricName } from "./types.ts";
import { SEGMENT_COLORS, METRIC_LABELS } from "./types.ts";
// Register Chart.js components
Chart.register(...registerables);
const log = {
info: console.log.bind(console, "[APP]"),
error: console.error.bind(console, "[APP]"),
};
// DOM element references
const xAxisSelect = document.getElementById("x-axis") as HTMLSelectElement;
const yAxisSelect = document.getElementById("y-axis") as HTMLSelectElement;
const sizeMetricSelect = document.getElementById(
"size-metric",
) as HTMLSelectElement;
const chartCanvas = document.getElementById(
"scatter-chart",
) as HTMLCanvasElement;
const legendContainer = document.getElementById("legend")!;
const detailPanel = document.getElementById("detail-panel")!;
// App state
interface AppState {
customers: Customer[];
segments: SegmentSummary[];
chart: Chart | null;
xAxis: MetricName;
yAxis: MetricName;
sizeMetric: string;
hiddenSegments: Set<string>;
selectedCustomer: Customer | null;
}
const state: AppState = {
customers: [],
segments: [],
chart: null,
xAxis: "annualRevenue",
yAxis: "engagementScore",
sizeMetric: "off",
hiddenSegments: new Set(),
selectedCustomer: null,
};
// Format numbers for display
function formatValue(value: number, metric: MetricName): string {
switch (metric) {
case "annualRevenue":
if (value >= 1_000_000) {
return `$${(value / 1_000_000).toFixed(1)}M`;
}
return `$${(value / 1_000).toFixed(0)}K`;
case "employeeCount":
return value.toLocaleString();
case "accountAge":
return `${value}mo`;
case "engagementScore":
return `${value}`;
case "supportTickets":
return `${value}`;
case "nps":
return value >= 0 ? `+${value}` : `${value}`;
default:
return `${value}`;
}
}
// Get min/max for a metric across all customers
function getMetricRange(
customers: Customer[],
metric: MetricName,
): { min: number; max: number } {
const values = customers.map((c) => c[metric] as number);
return {
min: Math.min(...values),
max: Math.max(...values),
};
}
// Normalize value to bubble radius (6-30px range)
function normalizeToRadius(value: number, min: number, max: number): number {
if (max === min) return 12;
const normalized = (value - min) / (max - min);
return 6 + normalized * 24;
}
// Get filtered customers based on current state
function getFilteredCustomers(): Customer[] {
return state.customers.filter((c) => !state.hiddenSegments.has(c.segment));
}
// Build chart datasets from customers
function buildDatasets(): Chart["data"]["datasets"] {
const customers = getFilteredCustomers();
const segments = [...new Set(customers.map((c) => c.segment))];
// Calculate size range if size metric is enabled
let sizeRange: { min: number; max: number } | null = null;
if (state.sizeMetric !== "off") {
sizeRange = getMetricRange(state.customers, state.sizeMetric as MetricName);
}
return segments.map((segment) => {
const segmentCustomers = customers.filter((c) => c.segment === segment);
const color = SEGMENT_COLORS[segment] || "#888888";
return {
label: segment,
data: segmentCustomers.map((c) => ({
x: c[state.xAxis] as number,
y: c[state.yAxis] as number,
r:
sizeRange && state.sizeMetric !== "off"
? normalizeToRadius(
c[state.sizeMetric as MetricName] as number,
sizeRange.min,
sizeRange.max,
)
: 8,
customer: c,
})),
backgroundColor: color + "aa",
borderColor: color,
borderWidth: 1,
hoverBackgroundColor: color,
hoverBorderColor: "#ffffff",
hoverBorderWidth: 2,
};
});
}
// Initialize Chart.js
function initChart(): Chart {
const isDarkMode = window.matchMedia("(prefers-color-scheme: dark)").matches;
const textColor = isDarkMode ? "#9ca3af" : "#6b7280";
const gridColor = isDarkMode ? "#374151" : "#e5e7eb";
return new Chart(chartCanvas, {
type: "bubble",
data: {
datasets: buildDatasets(),
},
options: {
responsive: true,
maintainAspectRatio: false,
animation: {
duration: 300,
},
interaction: {
intersect: true,
mode: "nearest",
},
plugins: {
legend: {
display: false, // Using custom legend
},
tooltip: {
enabled: true,
callbacks: {
label: (context) => {
const point = context.raw as { customer: Customer };
const c = point.customer;
return [
c.name,
`${METRIC_LABELS[state.xAxis]}: ${formatValue(c[state.xAxis] as number, state.xAxis)}`,
`${METRIC_LABELS[state.yAxis]}: ${formatValue(c[state.yAxis] as number, state.yAxis)}`,
];
},
},
},
},
scales: {
x: {
title: {
display: true,
text: METRIC_LABELS[state.xAxis],
color: textColor,
font: { size: 11, weight: "bold" },
},
ticks: {
color: textColor,
font: { size: 10 },
callback: (value) => formatValue(value as number, state.xAxis),
},
grid: {
color: gridColor,
},
},
y: {
title: {
display: true,
text: METRIC_LABELS[state.yAxis],
color: textColor,
font: { size: 11, weight: "bold" },
},
ticks: {
color: textColor,
font: { size: 10 },
callback: (value) => formatValue(value as number, state.yAxis),
},
grid: {
color: gridColor,
},
},
},
onClick: (_event, elements) => {
if (elements.length > 0) {
const element = elements[0];
const dataset = state.chart!.data.datasets[element.datasetIndex];
const point = dataset.data[element.index] as unknown as {
customer: Customer;
};
state.selectedCustomer = point.customer;
updateDetailPanel(point.customer);
}
},
onHover: (_event, elements) => {
if (elements.length > 0 && !state.selectedCustomer) {
const element = elements[0];
const dataset = state.chart!.data.datasets[element.datasetIndex];
const point = dataset.data[element.index] as unknown as {
customer: Customer;
};
updateDetailPanel(point.customer);
} else if (elements.length === 0 && !state.selectedCustomer) {
resetDetailPanel();
}
},
},
});
}
// Update chart with new data
function updateChart(): void {
if (!state.chart) return;
const isDarkMode = window.matchMedia("(prefers-color-scheme: dark)").matches;
const textColor = isDarkMode ? "#9ca3af" : "#6b7280";
state.chart.data.datasets = buildDatasets();
// Update axis titles and formatters (using type assertions for Chart.js scale options)
const scales = state.chart.options.scales as {
x: {
title: { text: string; color: string };
ticks: { callback: (value: number) => string };
};
y: {
title: { text: string; color: string };
ticks: { callback: (value: number) => string };
};
};
scales.x.title.text = METRIC_LABELS[state.xAxis];
scales.y.title.text = METRIC_LABELS[state.yAxis];
scales.x.title.color = textColor;
scales.y.title.color = textColor;
scales.x.ticks.callback = (value: number) => formatValue(value, state.xAxis);
scales.y.ticks.callback = (value: number) => formatValue(value, state.yAxis);
state.chart.update();
}
// Render custom legend
function renderLegend(): void {
// Count customers per segment
const counts = new Map<string, number>();
for (const c of state.customers) {
counts.set(c.segment, (counts.get(c.segment) || 0) + 1);
}
legendContainer.innerHTML = state.segments
.map((seg) => {
const count = counts.get(seg.name) || 0;
const isHidden = state.hiddenSegments.has(seg.name);
return `
<div class="legend-item ${isHidden ? "hidden" : ""}" data-segment="${seg.name}">
<span class="legend-dot" style="background: ${seg.color}"></span>
<span class="legend-label">${seg.name}</span>
<span class="legend-count">(${count})</span>
</div>
`;
})
.join("");
// Add click handlers
legendContainer.querySelectorAll(".legend-item").forEach((item) => {
item.addEventListener("click", () => {
const segment = item.getAttribute("data-segment")!;
if (state.hiddenSegments.has(segment)) {
state.hiddenSegments.delete(segment);
} else {
state.hiddenSegments.add(segment);
}
renderLegend();
updateChart();
});
});
}
// Update detail panel with customer info
function updateDetailPanel(customer: Customer): void {
const segmentClass = customer.segment.toLowerCase().replace("-", "-");
detailPanel.innerHTML = `
<span class="detail-name">${customer.name}</span>
<span class="detail-segment ${segmentClass}">${customer.segment}</span>
<span class="detail-metric"><strong>${formatValue(customer.annualRevenue, "annualRevenue")}</strong> rev</span>
<span class="detail-metric"><strong>${customer.engagementScore}</strong> engagement</span>
<span class="detail-metric"><strong>${customer.nps >= 0 ? "+" : ""}${customer.nps}</strong> NPS</span>
`;
}
// Reset detail panel to placeholder
function resetDetailPanel(): void {
detailPanel.innerHTML =
'<span class="detail-placeholder">Hover over a point to see details</span>';
}
// Create app instance
const app = new App({ name: "Customer Segmentation", version: "1.0.0" });
// Fetch data from server
async function fetchData(): Promise<void> {
try {
const result = await app.callServerTool({
name: "get-customer-data",
arguments: {},
});
const text = result
.content!.filter(
(c): c is { type: "text"; text: string } => c.type === "text",
)
.map((c) => c.text)
.join("");
const data = JSON.parse(text) as {
customers: Customer[];
segments: SegmentSummary[];
};
state.customers = data.customers;
state.segments = data.segments;
// Initialize or update chart
if (!state.chart) {
state.chart = initChart();
} else {
updateChart();
}
renderLegend();
log.info(`Loaded ${data.customers.length} customers`);
} catch (error) {
log.error("Failed to fetch data:", error);
}
}
// Event handlers
xAxisSelect.addEventListener("change", () => {
state.xAxis = xAxisSelect.value as MetricName;
updateChart();
});
yAxisSelect.addEventListener("change", () => {
state.yAxis = yAxisSelect.value as MetricName;
updateChart();
});
sizeMetricSelect.addEventListener("change", () => {
state.sizeMetric = sizeMetricSelect.value;
updateChart();
});
// Clear selection when clicking outside chart
document.addEventListener("click", (e) => {
if (!(e.target as HTMLElement).closest(".chart-section")) {
state.selectedCustomer = null;
resetDetailPanel();
}
});
// Handle theme changes
window
.matchMedia("(prefers-color-scheme: dark)")
.addEventListener("change", () => {
if (state.chart) {
state.chart.destroy();
state.chart = initChart();
}
});
// Register handlers and connect
app.onerror = log.error;
app.connect(new PostMessageTransport(window.parent));
// Fetch data after connection
setTimeout(fetchData, 100);