Skip to content

Commit d13a396

Browse files
olaservoclaude
andcommitted
feat(everything): demonstrate an array-rooted outputSchema
Before 2026-07-28 a tool's `outputSchema` had to have "type": "object". SEP-2106 lifted that -- an output schema may now be any JSON Schema and `structuredContent` any JSON value -- but the reference server that exists to exercise every protocol feature did not demonstrate it: the existing `get-structured-content` tool is object-rooted. Adds `get-structured-content-list`, a sibling that passes `z.array(...)` straight to `outputSchema` and returns a bare array as structured content. TypeScript is the SDK where the natural spelling is also the correct one; Python needs RootModel, Go needs the root narrowed, Rust needs schema_for_output. Input is `location` (the same three cities as its sibling) plus `days` (1-5, default 3). The forecast is derived deterministically from a per-city baseline -- no clock and no randomness -- so the same arguments always produce the same payload, which keeps it usable for tests and for comparing wire captures across SDKs. The handler carries no `ctx.era` branch. It returns the natural array once and the SDK's wire codec adapts both halves per era, verified off the raw wire: modern outputSchema {"type":"array","items":{...}} structuredContent [{"day":1,...},{"day":2,...}] legacy outputSchema {"type":"object","properties":{"result":{...}}, "required":["result"]} structuredContent {"result":[{"day":1,...},{"day":2,...}]} Because projectCallToolResult keys the payload projection off the tool's own advertised schema, the schema and the result cannot drift apart -- the failure mode a hand-written era branch would eventually hit. Note this automatic legacy projection is TypeScript-specific; the Go, Python and Rust SDKs do not currently perform it, so an array-rooted schema is a silent breaking change for older clients there. The tool has no text-only failure path: a tool advertising an `outputSchema` MUST return conforming structured content, so failures have to be raised as errors rather than answered with a bare text result. Tests cover the advertised array root, the bare-array payload, the `days` bound, the text-block mirror, and the payload validating against the tool's own schema. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
1 parent 80596fc commit d13a396

6 files changed

Lines changed: 232 additions & 3 deletions

File tree

‎src/everything/__tests__/registrations.test.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ describe("Registration Index Files", () => {
3030
// `-32021` at dispatch when the caller lacks the capability -- on both
3131
// eras. Note the mock advertises NO client capabilities, and the
3232
// capability-gated tools are still expected below.
33-
expect(mockServer.registerTool).toHaveBeenCalledTimes(17);
33+
expect(mockServer.registerTool).toHaveBeenCalledTimes(18);
3434

3535
// Verify specific tools are registered
3636
const registeredTools = (mockServer.registerTool as any).mock.calls.map(
@@ -41,6 +41,7 @@ describe("Registration Index Files", () => {
4141
expect(registeredTools).toContain("get-env");
4242
expect(registeredTools).toContain("get-tiny-image");
4343
expect(registeredTools).toContain("get-structured-content");
44+
expect(registeredTools).toContain("get-structured-content-list");
4445
expect(registeredTools).toContain("get-annotated-message");
4546
expect(registeredTools).toContain("trigger-long-running-operation");
4647
expect(registeredTools).toContain("get-resource-links");

‎src/everything/__tests__/tools.test.ts‎

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { describe, it, expect, vi, beforeEach } from "vitest";
2+
import { z } from "zod";
23
import { McpServer } from "@modelcontextprotocol/server";
34
import { registerEchoTool, EchoSchema } from "../tools/echo.js";
45
import { registerGetSumTool } from "../tools/get-sum.js";
@@ -8,6 +9,7 @@ import {
89
MCP_TINY_IMAGE,
910
} from "../tools/get-tiny-image.js";
1011
import { registerGetStructuredContentTool } from "../tools/get-structured-content.js";
12+
import { registerGetStructuredContentListTool } from "../tools/get-structured-content-list.js";
1113
import { registerGetAnnotatedMessageTool } from "../tools/get-annotated-message.js";
1214
import { registerTriggerLongRunningOperationTool } from "../tools/trigger-long-running-operation.js";
1315
import { registerGetResourceLinksTool } from "../tools/get-resource-links.js";
@@ -271,6 +273,108 @@ describe("Tools", () => {
271273
});
272274
});
273275

276+
describe("get-structured-content-list", () => {
277+
it("should advertise an array-rooted output schema", async () => {
278+
const { mockServer, configs } = createMockServer();
279+
registerGetStructuredContentListTool(mockServer);
280+
281+
// The point of this tool: 2026-07-28 lifted the object-root restriction,
282+
// so the advertised schema is `"type": "array"` rather than an object
283+
// with a wrapper property. The SDK projects this down to the legacy
284+
// `{result: ...}` shape for pre-2026 peers; it is NOT authored that way.
285+
const outputSchema = configs.get(
286+
"get-structured-content-list"
287+
)!.outputSchema;
288+
const asJsonSchema = z.toJSONSchema(outputSchema) as {
289+
type?: string;
290+
items?: unknown;
291+
};
292+
293+
expect(asJsonSchema.type).toBe("array");
294+
expect(asJsonSchema.items).toBeDefined();
295+
});
296+
297+
it("should return a bare array as structured content", async () => {
298+
const { mockServer, handlers } = createMockServer();
299+
registerGetStructuredContentListTool(mockServer);
300+
301+
const handler = handlers.get("get-structured-content-list")!;
302+
const result = await handler({ location: "Chicago", days: 3 });
303+
304+
// Bare array at the root -- not `{ result: [...] }`, and not an object
305+
// with a single array-valued property.
306+
expect(Array.isArray(result.structuredContent)).toBe(true);
307+
expect(result.structuredContent).toEqual([
308+
{
309+
day: 1,
310+
temperature: 36,
311+
conditions: "Light rain / drizzle",
312+
humidity: 82,
313+
},
314+
{
315+
day: 2,
316+
temperature: 37,
317+
conditions: "Light rain / drizzle",
318+
humidity: 80,
319+
},
320+
{
321+
day: 3,
322+
temperature: 38,
323+
conditions: "Light rain / drizzle",
324+
humidity: 78,
325+
},
326+
]);
327+
});
328+
329+
it("should honor the requested number of days", async () => {
330+
const { mockServer, handlers } = createMockServer();
331+
registerGetStructuredContentListTool(mockServer);
332+
333+
const handler = handlers.get("get-structured-content-list")!;
334+
335+
const one = await handler({ location: "New York", days: 1 });
336+
expect(one.structuredContent).toHaveLength(1);
337+
expect(one.structuredContent[0]).toEqual({
338+
day: 1,
339+
temperature: 33,
340+
conditions: "Cloudy",
341+
humidity: 82,
342+
});
343+
344+
const five = await handler({ location: "Los Angeles", days: 5 });
345+
expect(five.structuredContent).toHaveLength(5);
346+
expect(five.structuredContent[4].day).toBe(5);
347+
});
348+
349+
it("should mirror the structured content in a text block", async () => {
350+
const { mockServer, handlers } = createMockServer();
351+
registerGetStructuredContentListTool(mockServer);
352+
353+
const handler = handlers.get("get-structured-content-list")!;
354+
const result = await handler({ location: "New York", days: 2 });
355+
356+
expect(result.content[0].type).toBe("text");
357+
expect(JSON.parse(result.content[0].text)).toEqual(
358+
result.structuredContent
359+
);
360+
});
361+
362+
it("should conform to its own advertised output schema", async () => {
363+
const { mockServer, handlers, configs } = createMockServer();
364+
registerGetStructuredContentListTool(mockServer);
365+
366+
const handler = handlers.get("get-structured-content-list")!;
367+
const result = await handler({ location: "Chicago", days: 4 });
368+
369+
// A tool advertising an `outputSchema` MUST return conforming structured
370+
// content, so validate the real payload against the real schema.
371+
const outputSchema = configs.get(
372+
"get-structured-content-list"
373+
)!.outputSchema;
374+
expect(() => outputSchema.parse(result.structuredContent)).not.toThrow();
375+
});
376+
});
377+
274378
describe("get-annotated-message", () => {
275379
it("should return error message with high priority", async () => {
276380
const { mockServer, handlers } = createMockServer();

‎src/everything/docs/features.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
- `get-resource-reference` (tools/get-resource-reference.ts): Accepts `resourceType` (`text` or `blob`) and `resourceId` (positive integer). Returns a concrete `resource` content block (with its `uri`, `mimeType`, and data) with surrounding explanatory `text`.
1717
- `get-roots-list` (tools/get-roots-list.ts): Reports the client's workspace roots. On the legacy era the server has usually already pulled them after the handshake and answers from cache; on 2026-07-28 there is nothing cached, so it asks for them via `inputRequired` and the client retries with the listing attached.
1818
- `gzip-file-as-resource` (tools/gzip-file-as-resource.ts): Accepts a `name` and `data` (URL or data URI), fetches the data subject to size/time/domain constraints, compresses it, registers it as a session resource at `demo://resource/session/<name>` with `mimeType: application/gzip`, and returns either a `resource_link` (default) or an inline `resource` depending on `outputType`.
19-
- `get-structured-content` (tools/get-structured-content.ts): Demonstrates structured responses. Accepts `location` input and returns both backward‑compatible `content` (a `text` block containing JSON) and `structuredContent` validated by an `outputSchema` (temperature, conditions, humidity).
19+
- `get-structured-content` (tools/get-structured-content.ts): Demonstrates structured responses. Accepts `location` input and returns both backward‑compatible `content` (a `text` block containing JSON) and `structuredContent` validated by an `outputSchema` (temperature, conditions, humidity). The output schema has an **object** root — the only shape any revision before 2026-07-28 allowed.
20+
- `get-structured-content-list` (tools/get-structured-content-list.ts): The same idea with an **array** root, which 2026-07-28 newly permits (SEP-2106 lifted the object-only restriction on `outputSchema`). Accepts `location` and `days` (1–5, default 3) and returns a bare array of daily forecast entries. The handler is era-agnostic and returns the natural array; the SDK adapts the wire shape per era — identity on 2026-07-28, and for a legacy peer it projects *both* the advertised schema (to `{"type":"object","properties":{"result":…}}`) and the payload (to `{"result": [...]}`) so the two cannot drift apart. Note this automatic legacy projection is TypeScript-specific; the Go, Python and Rust SDKs do not currently perform it.
2021
- `get-sum` (tools/get-sum.ts): For two numbers `a` and `b` calculates and returns their sum. Uses Zod to validate inputs.
2122
- `get-tiny-image` (tools/get-tiny-image.ts): Returns a tiny PNG MCP logo as an `image` content item with brief descriptive text before and after.
2223
- `trigger-long-running-operation` (tools/trigger-long-running-operation.ts): Simulates a multi-step operation over a given `duration` and number of `steps`; reports progress via `notifications/progress` when a `progressToken` is provided by the client.

‎src/everything/docs/structure.md‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ src/everything
4747
│ ├── get-resource-reference.ts
4848
│ ├── get-roots-list.ts
4949
│ ├── get-structured-content.ts
50+
│ ├── get-structured-content-list.ts
5051
│ ├── get-sum.ts
5152
│ ├── get-tiny-image.ts
5253
│ ├── gzip-file-as-resource.ts
@@ -165,7 +166,9 @@ src/everything
165166
- `trigger-sampling-request.ts`
166167
- Registers a `trigger-sampling-request` tool that asks the client/LLM for a completion and returns the sampling result.
167168
- `get-structured-content.ts`
168-
- Registers a `get-structured-content` tool that demonstrates structuredContent block responses.
169+
- Registers a `get-structured-content` tool that demonstrates structuredContent block responses, with an object-rooted `outputSchema`.
170+
- `get-structured-content-list.ts`
171+
- Registers a `get-structured-content-list` tool whose `outputSchema` is an **array** at the root — the shape SEP-2106 newly permits at 2026-07-28. The handler returns the bare array on both eras; the SDK's wire codec projects the schema and the payload together into the legacy `{result: …}` shape for pre-2026 peers, so no `ctx.era` branch is needed in the tool.
169172
- `get-sum.ts`
170173
- Registers a `get-sum` tool with a Zod input schema that sums two numbers `a` and `b` and returns the result.
171174
- `get-tiny-image.ts`
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
import { z } from "zod";
2+
import { McpServer } from "@modelcontextprotocol/server";
3+
import { CallToolResult, ContentBlock } from "@modelcontextprotocol/server";
4+
5+
// Tool input schema
6+
const GetStructuredContentListInputSchema = {
7+
location: z
8+
.enum(["New York", "Chicago", "Los Angeles"])
9+
.describe("Choose city"),
10+
days: z
11+
.number()
12+
.int()
13+
.min(1)
14+
.max(5)
15+
.default(3)
16+
.describe("How many forecast days to return (1-5)"),
17+
};
18+
19+
// Tool output schema -- an ARRAY at the root, not an object.
20+
//
21+
// Before protocol revision 2026-07-28 an `outputSchema` had to have
22+
// `"type": "object"`. SEP-2106 lifted that: an output schema may now be any
23+
// JSON Schema, and `structuredContent` any JSON value. TypeScript is the SDK
24+
// where the natural spelling is also the correct one -- `z.array(...)` is
25+
// passed straight through, with no `RootModel`/wrapper dance needed.
26+
const GetStructuredContentListOutputSchema = z.array(
27+
z.object({
28+
day: z.number().int().describe("Forecast day, 1-based"),
29+
temperature: z.number().describe("Temperature in celsius"),
30+
conditions: z.string().describe("Weather conditions description"),
31+
humidity: z.number().describe("Humidity percentage"),
32+
})
33+
);
34+
35+
// Deterministic per-city baselines, so the forecast is reproducible.
36+
const BASELINES = {
37+
"New York": { temperature: 33, conditions: "Cloudy", humidity: 82 },
38+
Chicago: {
39+
temperature: 36,
40+
conditions: "Light rain / drizzle",
41+
humidity: 82,
42+
},
43+
"Los Angeles": { temperature: 73, conditions: "Sunny / Clear", humidity: 48 },
44+
} as const;
45+
46+
// Tool configuration
47+
const name = "get-structured-content-list";
48+
const config = {
49+
title: "Get Structured Content List Tool",
50+
description:
51+
"Returns an array-rooted structured content payload, demonstrating the 2026-07-28 removal of the object-root restriction on output schemas",
52+
inputSchema: GetStructuredContentListInputSchema,
53+
outputSchema: GetStructuredContentListOutputSchema,
54+
annotations: {
55+
readOnlyHint: true,
56+
destructiveHint: false,
57+
idempotentHint: true,
58+
openWorldHint: false,
59+
},
60+
};
61+
62+
/**
63+
* Registers the 'get-structured-content-list' tool.
64+
*
65+
* The sibling `get-structured-content` tool demonstrates an object-rooted
66+
* output schema, which is all any revision before 2026-07-28 permitted. This
67+
* tool demonstrates the case that revision newly allows: a **bare array at the
68+
* root** of both `outputSchema` and `structuredContent` (SEP-2106).
69+
*
70+
* ## What each era sees
71+
*
72+
* The handler below is written once and returns the natural value -- a bare
73+
* array. The SDK's wire codec adapts it per era, so neither the schema nor the
74+
* handler needs an `ctx.era` branch:
75+
*
76+
* - **modern** (`2026-07-28`) -- identity. `outputSchema` goes out with
77+
* `"type": "array"` and `structuredContent` is the bare array.
78+
* - **legacy** (through `2025-11-25`) -- the codec projects both halves so the
79+
* response stays legal on a wire that requires an object. `tools/list`
80+
* advertises `{"type":"object","properties":{"result":{"type":"array",...}}}`
81+
* and `tools/call` answers `{"result": [...]}`. The projection is applied by
82+
* `projectCallToolResult`, keyed on the tool's advertised schema, so the two
83+
* halves cannot drift apart.
84+
*
85+
* This automatic legacy projection is specific to the TypeScript SDK; as of
86+
* this writing the Go, Python and Rust SDKs do not perform it, so an
87+
* array-rooted schema is a breaking change for older clients there.
88+
*
89+
* Note there is no failure path returning bare text. A tool that advertises an
90+
* `outputSchema` MUST return conforming structured content, so an error has to
91+
* be raised as an error rather than answered with a text-only result.
92+
*
93+
* @param {McpServer} server - The McpServer instance where the tool will be registered.
94+
*/
95+
export const registerGetStructuredContentListTool = (server: McpServer) => {
96+
server.registerTool(name, config, async (args): Promise<CallToolResult> => {
97+
const baseline = BASELINES[args.location];
98+
99+
// A simple deterministic drift off the city's baseline -- no clock and no
100+
// randomness, so the same arguments always produce the same forecast.
101+
const forecast = Array.from({ length: args.days }, (_, index) => ({
102+
day: index + 1,
103+
temperature: baseline.temperature + index,
104+
conditions: baseline.conditions,
105+
humidity: Math.max(0, baseline.humidity - index * 2),
106+
}));
107+
108+
const backwardCompatibleContentBlock: ContentBlock = {
109+
type: "text",
110+
text: JSON.stringify(forecast),
111+
};
112+
113+
return {
114+
content: [backwardCompatibleContentBlock],
115+
structuredContent: forecast,
116+
};
117+
});
118+
};

‎src/everything/tools/index.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { registerGetResourceLinksTool } from "./get-resource-links.js";
66
import { registerGetResourceReferenceTool } from "./get-resource-reference.js";
77
import { registerGetRootsListTool } from "./get-roots-list.js";
88
import { registerGetStructuredContentTool } from "./get-structured-content.js";
9+
import { registerGetStructuredContentListTool } from "./get-structured-content-list.js";
910
import { registerGetSumTool } from "./get-sum.js";
1011
import { registerGetTinyImageTool } from "./get-tiny-image.js";
1112
import { registerGZipFileAsResourceTool } from "./gzip-file-as-resource.js";
@@ -42,6 +43,7 @@ export const registerTools = (server: McpServer) => {
4243
registerGetResourceReferenceTool(server);
4344
registerGetRootsListTool(server);
4445
registerGetStructuredContentTool(server);
46+
registerGetStructuredContentListTool(server);
4547
registerGetSumTool(server);
4648
registerGetTinyImageTool(server);
4749
registerGZipFileAsResourceTool(server);

0 commit comments

Comments
 (0)