Skip to content

Commit 22441e7

Browse files
committed
feat: use structuredContent in all example servers
All servers now return structuredContent alongside JSON text content. All mcp-apps now read structuredContent directly (no JSON.parse fallback). Updated servers: - basic-server-vanillajs - budget-allocator-server - cohort-heatmap-server - customer-segmentation-server - integration-server - scenario-modeler-server - system-monitor-server - threejs-server - video-resource-server - wiki-explorer-server
1 parent 9911544 commit 22441e7

File tree

19 files changed

+85
-145
lines changed

19 files changed

+85
-145
lines changed

examples/basic-server-vanillajs/server.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,10 @@ export function createServer(): McpServer {
3232
},
3333
async (): Promise<CallToolResult> => {
3434
const time = new Date().toISOString();
35+
const data = { time };
3536
return {
36-
content: [{ type: "text", text: JSON.stringify({ time }) }],
37+
content: [{ type: "text", text: JSON.stringify(data) }],
38+
structuredContent: data,
3739
};
3840
},
3941
);

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

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

1616

1717
function extractTime(result: CallToolResult): string {
18-
const { text } = result.content?.find((c) => c.type === "text")!;
19-
return text;
18+
const data = result.structuredContent as { time: string };
19+
return data.time;
2020
}
2121

2222

examples/budget-allocator-server/server.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ export function createServer(): McpServer {
279279
text: JSON.stringify(response),
280280
},
281281
],
282+
structuredContent: response,
282283
};
283284
},
284285
);

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

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

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

examples/cohort-heatmap-server/server.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ export function createServer(): McpServer {
181181

182182
return {
183183
content: [{ type: "text", text: JSON.stringify(data) }],
184+
structuredContent: data,
184185
};
185186
},
186187
);

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

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -123,13 +123,7 @@ function CohortHeatmapInner({
123123
maxPeriods: 12,
124124
},
125125
});
126-
const text = result
127-
.content!.filter(
128-
(c): c is { type: "text"; text: string } => c.type === "text",
129-
)
130-
.map((c) => c.text)
131-
.join("");
132-
setData(JSON.parse(text) as CohortData);
126+
setData(result.structuredContent as CohortData);
133127
} catch (e) {
134128
console.error("Failed to fetch cohort data:", e);
135129
} finally {

examples/customer-segmentation-server/server.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ export function createServer(): McpServer {
8585

8686
return {
8787
content: [{ type: "text", text: JSON.stringify(data) }],
88+
structuredContent: data,
8889
};
8990
},
9091
);

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

Lines changed: 21 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -369,41 +369,31 @@ function resetDetailPanel(): void {
369369
// Create app instance
370370
const app = new App({ name: "Customer Segmentation", version: "1.0.0" });
371371

372-
// Fetch data from server
373-
async function fetchData(): Promise<void> {
374-
try {
375-
const result = await app.callServerTool({
376-
name: "get-customer-data",
377-
arguments: {},
378-
});
379-
380-
const text = result
381-
.content!.filter(
382-
(c): c is { type: "text"; text: string } => c.type === "text",
383-
)
384-
.map((c) => c.text)
385-
.join("");
386-
const data = JSON.parse(text) as {
387-
customers: Customer[];
388-
segments: SegmentSummary[];
389-
};
372+
// Handle tool results via structuredContent
373+
app.ontoolresult = (result) => {
374+
const data = result.structuredContent as {
375+
customers: Customer[];
376+
segments: SegmentSummary[];
377+
};
390378

391-
state.customers = data.customers;
392-
state.segments = data.segments;
379+
if (!data?.customers || !data?.segments) {
380+
log.error("Invalid data received:", result);
381+
return;
382+
}
393383

394-
// Initialize or update chart
395-
if (!state.chart) {
396-
state.chart = initChart();
397-
} else {
398-
updateChart();
399-
}
384+
state.customers = data.customers;
385+
state.segments = data.segments;
400386

401-
renderLegend();
402-
log.info(`Loaded ${data.customers.length} customers`);
403-
} catch (error) {
404-
log.error("Failed to fetch data:", error);
387+
// Initialize or update chart
388+
if (!state.chart) {
389+
state.chart = initChart();
390+
} else {
391+
updateChart();
405392
}
406-
}
393+
394+
renderLegend();
395+
log.info(`Loaded ${data.customers.length} customers`);
396+
};
407397

408398
// Event handlers
409399
xAxisSelect.addEventListener("change", () => {
@@ -482,6 +472,3 @@ app.connect().then(() => {
482472
handleHostContextChanged(ctx);
483473
}
484474
});
485-
486-
// Fetch data after connection
487-
setTimeout(fetchData, 100);

examples/integration-server/server.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,15 @@ export function createServer(): McpServer {
3636
_meta: { [RESOURCE_URI_META_KEY]: RESOURCE_URI },
3737
},
3838
async (): Promise<CallToolResult> => {
39+
const time = new Date().toISOString();
3940
return {
4041
content: [
4142
{
4243
type: "text",
43-
text: JSON.stringify({ time: new Date().toISOString() }),
44+
text: JSON.stringify({ time }),
4445
},
4546
],
47+
structuredContent: { time },
4648
};
4749
},
4850
);

examples/integration-server/src/mcp-app.tsx

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,7 @@ const log = {
1717
};
1818

1919
function extractTime(callToolResult: CallToolResult): string {
20-
const text = callToolResult
21-
.content!.filter(
22-
(c): c is { type: "text"; text: string } => c.type === "text",
23-
)
24-
.map((c) => c.text)
25-
.join("");
26-
const { time } = JSON.parse(text) as { time: string };
20+
const { time } = callToolResult.structuredContent as { time: string };
2721
return time;
2822
}
2923

0 commit comments

Comments
 (0)