Skip to content

Commit 744ec42

Browse files
committed
Revert "Merge pull request #81 from jonathanhefner/examples-use-content-for-now"
This reverts commit 437b918, reversing changes made to d5b13b7.
1 parent f6ebefe commit 744ec42

File tree

14 files changed

+150
-65
lines changed

14 files changed

+150
-65
lines changed

examples/basic-server-react/server.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import cors from "cors";
55
import express, { type Request, type Response } from "express";
66
import fs from "node:fs/promises";
77
import path from "node:path";
8+
import { z } from "zod";
89
import { RESOURCE_URI_META_KEY } from "../../dist/src/app";
910

1011
const PORT = process.env.PORT ? parseInt(process.env.PORT, 10) : 3001;
@@ -29,12 +30,14 @@ const server = new McpServer({
2930
title: "Get Time",
3031
description: "Returns the current server time as an ISO 8601 string.",
3132
inputSchema: {},
33+
outputSchema: { time: z.string() },
3234
_meta: { [RESOURCE_URI_META_KEY]: resourceUri },
3335
},
3436
async (): Promise<CallToolResult> => {
3537
const time = new Date().toISOString();
3638
return {
37-
content: [{ type: "text", text: JSON.stringify({ time }) }],
39+
content: [{ type: "text", text: time }],
40+
structuredContent: { time },
3841
};
3942
},
4043
);

examples/basic-server-react/src/mcp-app.tsx

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,8 @@ const log = {
2020

2121

2222
function extractTime(callToolResult: CallToolResult): string {
23-
const text = callToolResult.content!
24-
.filter((c): c is { type: "text"; text: string } => c.type === "text")
25-
.map((c) => c.text)
26-
.join("");
27-
const { time } = JSON.parse(text) as { time: string };
28-
return time;
23+
const { time } = (callToolResult.structuredContent as { time?: string }) ?? {};
24+
return time ?? "[ERROR]";
2925
}
3026

3127

examples/basic-server-vanillajs/server.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import cors from "cors";
55
import express, { type Request, type Response } from "express";
66
import fs from "node:fs/promises";
77
import path from "node:path";
8+
import { z } from "zod";
89
import { RESOURCE_URI_META_KEY } from "../../dist/src/app";
910

1011
const PORT = process.env.PORT ? parseInt(process.env.PORT, 10) : 3001;
@@ -29,12 +30,14 @@ const server = new McpServer({
2930
title: "Get Time",
3031
description: "Returns the current server time as an ISO 8601 string.",
3132
inputSchema: {},
33+
outputSchema: { time: z.string() },
3234
_meta: { [RESOURCE_URI_META_KEY]: resourceUri },
3335
},
3436
async (): Promise<CallToolResult> => {
3537
const time = new Date().toISOString();
3638
return {
37-
content: [{ type: "text", text: JSON.stringify({ time }) }],
39+
content: [{ type: "text", text: time }],
40+
structuredContent: { time },
3841
};
3942
},
4043
);

examples/basic-server-vanillajs/src/mcp-app.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,8 @@ const log = {
1515

1616

1717
function extractTime(result: CallToolResult): string {
18-
const text = result.content!
19-
.filter((c): c is { type: "text"; text: string } => c.type === "text")
20-
.map((c) => c.text)
21-
.join("");
22-
const { time } = JSON.parse(text) as { time: string };
23-
return time;
18+
const { time } = (result.structuredContent as { time?: string }) ?? {};
19+
return time ?? "[ERROR]";
2420
}
2521

2622

examples/budget-allocator-server/server.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,30 @@ function generateHistory(
222222
return months;
223223
}
224224

225+
// ---------------------------------------------------------------------------
226+
// Response Formatting
227+
// ---------------------------------------------------------------------------
228+
229+
function formatBudgetSummary(data: BudgetDataResponse): string {
230+
const lines: string[] = [
231+
"Budget Allocator Configuration",
232+
"==============================",
233+
"",
234+
`Default Budget: ${data.config.currencySymbol}${data.config.defaultBudget.toLocaleString()}`,
235+
`Available Presets: ${data.config.presetBudgets.map((b) => `${data.config.currencySymbol}${b.toLocaleString()}`).join(", ")}`,
236+
"",
237+
"Categories:",
238+
...data.config.categories.map(
239+
(c) => ` - ${c.name}: ${c.defaultPercent}% default`,
240+
),
241+
"",
242+
`Historical Data: ${data.analytics.history.length} months`,
243+
`Benchmark Stages: ${data.analytics.stages.join(", ")}`,
244+
`Default Stage: ${data.analytics.defaultStage}`,
245+
];
246+
return lines.join("\n");
247+
}
248+
225249
// ---------------------------------------------------------------------------
226250
// MCP Server Setup
227251
// ---------------------------------------------------------------------------
@@ -240,6 +264,7 @@ server.registerTool(
240264
description:
241265
"Returns budget configuration with 24 months of historical allocations and industry benchmarks by company stage",
242266
inputSchema: {},
267+
outputSchema: BudgetDataResponseSchema.shape,
243268
_meta: { [RESOURCE_URI_META_KEY]: resourceUri },
244269
},
245270
async (): Promise<CallToolResult> => {
@@ -268,9 +293,10 @@ server.registerTool(
268293
content: [
269294
{
270295
type: "text",
271-
text: JSON.stringify(response),
296+
text: formatBudgetSummary(response),
272297
},
273298
],
299+
structuredContent: response,
274300
};
275301
},
276302
);

examples/budget-allocator-server/src/mcp-app.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -606,13 +606,7 @@ const app = new App({ name: "Budget Allocator", version: "1.0.0" });
606606

607607
app.ontoolresult = (result) => {
608608
log.info("Received tool result:", result);
609-
const text = result
610-
.content!.filter(
611-
(c): c is { type: "text"; text: string } => c.type === "text",
612-
)
613-
.map((c) => c.text)
614-
.join("");
615-
const data = JSON.parse(text) as BudgetDataResponse;
609+
const data = result.structuredContent as unknown as BudgetDataResponse;
616610
if (data?.config && data?.analytics) {
617611
initializeUI(data.config, data.analytics);
618612
}

examples/cohort-heatmap-server/server.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,17 @@ function generateCohortData(
150150
};
151151
}
152152

153+
function formatCohortSummary(data: CohortData): string {
154+
const avgRetention = data.cohorts
155+
.flatMap((c) => c.cells)
156+
.filter((cell) => cell.periodIndex > 0)
157+
.reduce((sum, cell, _, arr) => sum + cell.retention / arr.length, 0);
158+
159+
return `Cohort Analysis: ${data.cohorts.length} cohorts, ${data.periods.length} periods
160+
Average retention: ${(avgRetention * 100).toFixed(1)}%
161+
Metric: ${data.metric}, Period: ${data.periodType}`;
162+
}
163+
153164
const server = new McpServer({
154165
name: "Cohort Heatmap Server",
155166
version: "1.0.0",
@@ -166,6 +177,7 @@ const server = new McpServer({
166177
description:
167178
"Returns cohort retention heatmap data showing customer retention over time by signup month",
168179
inputSchema: GetCohortDataInputSchema.shape,
180+
outputSchema: CohortDataSchema.shape,
169181
_meta: { [RESOURCE_URI_META_KEY]: resourceUri },
170182
},
171183
async ({ metric, periodType, cohortCount, maxPeriods }) => {
@@ -177,7 +189,8 @@ const server = new McpServer({
177189
);
178190

179191
return {
180-
content: [{ type: "text", text: JSON.stringify(data) }],
192+
content: [{ type: "text", text: formatCohortSummary(data) }],
193+
structuredContent: data,
181194
};
182195
},
183196
);

examples/cohort-heatmap-server/src/mcp-app.tsx

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,7 @@ function CohortHeatmapInner({ app }: { app: App }) {
103103
maxPeriods: 12,
104104
},
105105
});
106-
const text = result
107-
.content!.filter(
108-
(c): c is { type: "text"; text: string } => c.type === "text",
109-
)
110-
.map((c) => c.text)
111-
.join("");
112-
setData(JSON.parse(text) as CohortData);
106+
setData(result.structuredContent as unknown as CohortData);
113107
} catch (e) {
114108
console.error("Failed to fetch cohort data:", e);
115109
} finally {

examples/customer-segmentation-server/server.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,29 @@ const GetCustomerDataInputSchema = z.object({
2828
.describe("Filter by segment (default: All)"),
2929
});
3030

31+
const CustomerSchema = z.object({
32+
id: z.string(),
33+
name: z.string(),
34+
segment: z.string(),
35+
annualRevenue: z.number(),
36+
employeeCount: z.number(),
37+
accountAge: z.number(),
38+
engagementScore: z.number(),
39+
supportTickets: z.number(),
40+
nps: z.number(),
41+
});
42+
43+
const SegmentSummarySchema = z.object({
44+
name: z.string(),
45+
count: z.number(),
46+
color: z.string(),
47+
});
48+
49+
const GetCustomerDataOutputSchema = z.object({
50+
customers: z.array(CustomerSchema),
51+
segments: z.array(SegmentSummarySchema),
52+
});
53+
3154
// Cache generated data for session consistency
3255
let cachedCustomers: Customer[] | null = null;
3356
let cachedSegments: SegmentSummary[] | null = null;
@@ -70,13 +93,15 @@ const server = new McpServer({
7093
description:
7194
"Returns customer data with segment information for visualization. Optionally filter by segment.",
7295
inputSchema: GetCustomerDataInputSchema.shape,
96+
outputSchema: GetCustomerDataOutputSchema.shape,
7397
_meta: { [RESOURCE_URI_META_KEY]: resourceUri },
7498
},
7599
async ({ segment }): Promise<CallToolResult> => {
76100
const data = getCustomerData(segment);
77101

78102
return {
79-
content: [{ type: "text", text: JSON.stringify(data) }],
103+
content: [{ type: "text", text: JSON.stringify(data, null, 2) }],
104+
structuredContent: data,
80105
};
81106
},
82107
);

examples/customer-segmentation-server/src/mcp-app.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -336,13 +336,7 @@ async function fetchData(): Promise<void> {
336336
arguments: {},
337337
});
338338

339-
const text = result
340-
.content!.filter(
341-
(c): c is { type: "text"; text: string } => c.type === "text",
342-
)
343-
.map((c) => c.text)
344-
.join("");
345-
const data = JSON.parse(text) as {
339+
const data = result.structuredContent as unknown as {
346340
customers: Customer[];
347341
segments: SegmentSummary[];
348342
};

0 commit comments

Comments
 (0)