|
| 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 | +}; |
0 commit comments