* Add Playwright E2E tests with screenshot golden testing - Add E2E tests for all 8 MCP server examples - Screenshot golden images for visual regression testing - CI workflow for running E2E tests - npm scripts: test:e2e, test:e2e:update, test:e2e:ui 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com> * Add default demo code for Three.js example When no code is provided, show a rotating green cube demo. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com> * Add E2E testing documentation to CONTRIBUTING.md 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com> * Update Three.js golden screenshot with 3D cube 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com> * Add explicit permissions to CI workflow Set minimal read-only permissions for security best practices. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com> * Fix test.setTimeout() to be inside describe blocks Playwright requires setTimeout to be called within a test or describe block. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com> * Simplify E2E tests - remove excessive timeouts - Remove explicit setTimeout calls (default 30s is sufficient) - Replace waitForTimeout(5000/6000) with proper waitForAppLoad() - Wait for inner iframe visibility instead of fixed delays - Keep only 500ms stabilization for screenshot animations 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com> * Auto-generate missing snapshots in CI Use updateSnapshots: 'missing' in CI to handle cross-platform screenshot differences (macOS vs Linux). 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com> * Use platform-agnostic golden screenshots - Remove -chromium-darwin suffix from snapshot filenames - Configure snapshotPathTemplate for cross-platform compatibility - Increase tolerance to 5% for rendering differences between platforms 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com> * Refactor tests to use forEach instead of for-of loop May help with Playwright test discovery issues in CI. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com> * Use npm ci and list reporter in CI - Use npm ci for exact package versions - Use --reporter=list instead of html to avoid potential re-evaluation issues - Only upload test-results on failure 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com> * Exclude e2e tests from bun test Run bun test only on src/ directory to avoid running Playwright tests with Bun's test runner. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com> * Fix prettier formatting 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com> * Add interaction tests for basic server apps Test that clicking buttons in the MCP App triggers the corresponding host callbacks: - Send Message → host logs "Message from MCP App" - Send Log → host logs "Log message from MCP App" - Open Link → host logs "Open link request" Tests both React and Vanilla JS implementations. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com> * Mask dynamic content in E2E screenshot tests Add Playwright mask option to handle servers with dynamic/random content: - basic-react/vanillajs: mask server time display - system-monitor: mask CPU chart, memory stats, uptime - cohort-heatmap: mask heatmap grid (random data) - customer-segmentation: mask scatter chart (random data) This addresses PR feedback about handling examples with non-deterministic output. Masking replaces the previous 10% tolerance with proper exclusion of dynamic elements, allowing tighter 1% tolerance for the rest of the UI. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com> * Add wiki-explorer to E2E tests with default URL param - Add default URL "https://en.wikipedia.org/wiki/Model_Context_Protocol" to wiki-explorer-server's get-first-degree-links tool inputSchema - Update basic-host to automatically populate input field with tool defaults extracted from inputSchema.properties - Add wiki-explorer-server to E2E test suite with dynamic masking for the force-directed graph 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com> * Use .default() for threejs tool schema instead of .optional() This exposes the default code and height values in the JSON Schema, allowing basic-host to auto-populate the input field with defaults. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com> * Enable parallel E2E tests with timeouts and canvas masking - Enable fullyParallel with 4 workers locally (2 in CI) - Add 30s timeout per test - Mask threejs canvas for stable screenshots - Update snapshots to reflect default values in input fields 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com> * Add pre-commit check for private registry URLs in package-lock.json Same check as in CI to catch issues before push. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com> * Increase screenshot diff tolerance to 6% for cross-platform rendering Font rendering differs between macOS and Linux, causing ~5% pixel differences. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com> * Revert pre-commit artifactory check (moved to separate PR #133) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com> * Format threejs server.ts 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com> * Update CONTRIBUTING.md to reflect platform-agnostic screenshots 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com> * fix(e2e): use factory pattern for MCP servers to support parallel connections McpServer only supports one transport at a time. When multiple browser contexts connected in parallel, calling server.connect(transport) for each session overwrote the previous transport's callbacks, causing connection corruption and test timeouts. Changes: - server-utils.ts: accept factory function instead of single instance - All 9 example servers: wrap server creation in createServer() function - playwright.config.ts: re-enable parallel execution (4 workers) - servers.spec.ts: add toBeEnabled wait for server connection - Update screenshot baselines for basic-vanillajs and cohort-heatmap 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> --------- Co-authored-by: Claude <noreply@anthropic.com>
219 lines
6 KiB
TypeScript
219 lines
6 KiB
TypeScript
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 { RESOURCE_MIME_TYPE, RESOURCE_URI_META_KEY } from "../../dist/src/app";
|
|
import { 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";
|
|
|
|
server.registerTool(
|
|
"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 {
|
|
content: [{ type: "text", text: JSON.stringify(data) }],
|
|
};
|
|
},
|
|
);
|
|
|
|
server.registerResource(
|
|
resourceUri,
|
|
resourceUri,
|
|
{},
|
|
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);
|
|
});
|