-
Notifications
You must be signed in to change notification settings - Fork 268
Expand file tree
/
Copy pathmcp-app.ts
More file actions
486 lines (441 loc) · 14.4 KB
/
mcp-app.ts
File metadata and controls
486 lines (441 loc) · 14.4 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
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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
/**
* @file Customer Segmentation Explorer - interactive scatter/bubble visualization
*/
import {
App,
PostMessageTransport,
applyHostStyleVariables,
applyDocumentTheme,
} 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,
};
});
}
// Hidden element for resolving CSS color values (reused to avoid DOM thrashing)
let colorResolver: HTMLDivElement | null = null;
// Resolve a CSS color value (handles light-dark() function)
function resolveColor(cssValue: string, fallback: string): string {
if (!cssValue) return fallback;
// If it's a simple color value, return it directly
if (!cssValue.includes("light-dark(")) return cssValue;
// Create resolver element once and keep it hidden
if (!colorResolver) {
colorResolver = document.createElement("div");
colorResolver.style.position = "absolute";
colorResolver.style.visibility = "hidden";
colorResolver.style.pointerEvents = "none";
document.body.appendChild(colorResolver);
}
colorResolver.style.color = cssValue;
return getComputedStyle(colorResolver).color || fallback;
}
// Get colors from CSS variables
function getChartColors(): { textColor: string; gridColor: string } {
const style = getComputedStyle(document.documentElement);
const rawTextColor = style.getPropertyValue("--color-text-secondary").trim();
const rawGridColor = style.getPropertyValue("--color-border-primary").trim();
return {
textColor: resolveColor(rawTextColor, "#6b7280"),
gridColor: resolveColor(rawGridColor, "#e5e7eb"),
};
}
// Initialize Chart.js
function initChart(): Chart {
const { textColor, gridColor } = getChartColors();
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 { textColor, gridColor } = getChartColors();
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: { color: string; callback: (value: number) => string };
grid: { color: string };
};
y: {
title: { text: string; color: string };
ticks: { color: string; callback: (value: number) => string };
grid: { color: 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.color = textColor;
scales.y.ticks.color = textColor;
scales.x.ticks.callback = (value: number) => formatValue(value, state.xAxis);
scales.y.ticks.callback = (value: number) => formatValue(value, state.yAxis);
scales.x.grid.color = gridColor;
scales.y.grid.color = gridColor;
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: {},
});
// Prefer structuredContent (STRUCTURED_CONTENT_ONLY mode), fall back to parsing text
let data: { customers: Customer[]; segments: SegmentSummary[] };
if (result.structuredContent) {
data = result.structuredContent as typeof data;
} else {
const text = result
.content!.filter(
(c): c is { type: "text"; text: string } => c.type === "text",
)
.map((c) => c.text)
.join("");
try {
data = JSON.parse(text);
} catch (e) {
log.error("Failed to parse tool result:", text, e);
return;
}
}
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 system theme changes (fallback when host doesn't provide styles)
window
.matchMedia("(prefers-color-scheme: dark)")
.addEventListener("change", (e) => {
// Only apply if we haven't received host theme
if (!app.getHostContext()?.theme) {
applyDocumentTheme(e.matches ? "dark" : "light");
if (state.chart) {
state.chart.destroy();
state.chart = initChart();
}
}
});
// Apply initial theme based on system preference (before host context is available)
const systemDark = window.matchMedia("(prefers-color-scheme: dark)").matches;
applyDocumentTheme(systemDark ? "dark" : "light");
// Register handlers and connect
app.onerror = log.error;
// Handle host context changes (theme and styles from host)
app.onhostcontextchanged = (params) => {
if (params.theme) {
applyDocumentTheme(params.theme);
}
if (params.styles?.variables) {
applyHostStyleVariables(params.styles.variables);
}
// Recreate chart to pick up new colors
if (state.chart && (params.theme || params.styles?.variables)) {
state.chart.destroy();
state.chart = initChart();
}
};
app.connect().then(() => {
// Apply initial host context after connection
const ctx = app.getHostContext();
if (ctx?.theme) {
applyDocumentTheme(ctx.theme);
}
if (ctx?.styles?.variables) {
applyHostStyleVariables(ctx.styles.variables);
}
});
// Fetch data after connection
setTimeout(fetchData, 100);