Use content instead of structuredContent in examples

Not all MCP clients support structuredContent in MCP tool results, so
examples now return data as JSON in content and parse it client-side.

Pattern used:
- Server: content: [{ type: "text", text: JSON.stringify(data) }]
- Client: JSON.parse(result.content!.filter(...).map(...).join(""))

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Jonathan Hefner 2025-12-05 16:18:02 -06:00
commit 61cd4ed219
2 changed files with 65 additions and 150 deletions

View file

@ -149,17 +149,6 @@ function generateCohortData(
};
}
function formatCohortSummary(data: CohortData): string {
const avgRetention = data.cohorts
.flatMap((c) => c.cells)
.filter((cell) => cell.periodIndex > 0)
.reduce((sum, cell, _, arr) => sum + cell.retention / arr.length, 0);
return `Cohort Analysis: ${data.cohorts.length} cohorts, ${data.periods.length} periods
Average retention: ${(avgRetention * 100).toFixed(1)}%
Metric: ${data.metric}, Period: ${data.periodType}`;
}
const server = new McpServer({
name: "Cohort Heatmap Server",
version: "1.0.0",
@ -176,7 +165,6 @@ const server = new McpServer({
description:
"Returns cohort retention heatmap data showing customer retention over time by signup month",
inputSchema: GetCohortDataInputSchema.shape,
outputSchema: CohortDataSchema.shape,
_meta: { [RESOURCE_URI_META_KEY]: resourceUri },
},
async ({ metric, periodType, cohortCount, maxPeriods }) => {
@ -188,8 +176,7 @@ const server = new McpServer({
);
return {
content: [{ type: "text", text: formatCohortSummary(data) }],
structuredContent: data,
content: [{ type: "text", text: JSON.stringify(data) }],
};
},
);

View file

@ -103,7 +103,13 @@ function CohortHeatmapInner({ app }: { app: App }) {
maxPeriods: 12,
},
});
setData(result.structuredContent as unknown as CohortData);
const text = result
.content!.filter(
(c): c is { type: "text"; text: string } => c.type === "text",
)
.map((c) => c.text)
.join("");
setData(JSON.parse(text) as CohortData);
} catch (e) {
console.error("Failed to fetch cohort data:", e);
} finally {