diff --git a/.changeset/calltoolresult-content-default.md b/.changeset/calltoolresult-content-default.md new file mode 100644 index 0000000000..9d0daeebbf --- /dev/null +++ b/.changeset/calltoolresult-content-default.md @@ -0,0 +1,14 @@ +--- +'@modelcontextprotocol/core-internal': patch +'@modelcontextprotocol/client': patch +'@modelcontextprotocol/core': patch +'@modelcontextprotocol/server': patch +--- + +Restore the v1 parse tolerance for `CallToolResult.content`: an inbound legacy-era `tools/call` result without `content` defaults to `[]` instead of failing validation. Deployed servers — accepted by SDK v1 for years — return `structuredContent`-only (or otherwise content-less) results, and the strict parse turned every such call into an `INVALID_RESULT` error before application code could run. + +The silent-empty-success hazard the strictness guarded is preserved where it matters: the 2025 era's wire-seam schema refuses to default `content` for a body carrying another result family's vocabulary (`task`, `inputRequests`, `requestState` — the era is frozen, so the list is complete), and the 2026-era wire schemas stay strict — modern-revision servers have no legacy excuse. Task interop through an explicit result schema is untouched (including bodies that also stamp a foreign `resultType`), and the server-side authoring normalization refuses the same foreign-family vocabulary. + +Server-side authoring is era-independent: a handler result without `content` (dynamic/JS callers — the TypeScript surface requires it) is normalized to `content: []` before era validation on every leg, reaching the wire spec-valid. + +Conscious call: the nested sampling `ToolResultContentSchema` stays spec-strict — v1 had defaulted its `content` too, but it is params-side (tool results a caller authors into a sampling message), deliberately not restored. diff --git a/docs/migration/support-2026-07-28.md b/docs/migration/support-2026-07-28.md index a9d603d11a..c781aea0c2 100644 --- a/docs/migration/support-2026-07-28.md +++ b/docs/migration/support-2026-07-28.md @@ -340,9 +340,11 @@ If you were on a v2 alpha and consumed wire schemas directly: The `resultType` / `EmptyResultSchema` / `specTypeSchemas` rules above have **no v1.x impact** — these members did not exist before 2026-07-28. The neutral-model wire -tightening that **does** affect v1 code (`content` required, custom-handler `_meta` -passthrough, `specTypeSchemas` narrowing) is in -[upgrade-to-v2.md › Wire tightening](./upgrade-to-v2.md#wire-tightening-every-era). +tightening that **does** affect v1 code (custom-handler `_meta` passthrough, +`specTypeSchemas` narrowing) is in +[upgrade-to-v2.md › Wire tightening](./upgrade-to-v2.md#wire-tightening-every-era); +`CallToolResult.content` keeps its v1 default on the legacy era (2026-07-28 +connections require it explicitly). > **If you were on a v2 alpha:** the 2026-07-28 draft error codes were renumbered: > `HeaderMismatch` `-32001`→`-32020`, `MissingRequiredClientCapability` `-32003`→`-32021`, diff --git a/docs/migration/upgrade-to-v2.md b/docs/migration/upgrade-to-v2.md index 89188aa893..a5d5e99a5f 100644 --- a/docs/migration/upgrade-to-v2.md +++ b/docs/migration/upgrade-to-v2.md @@ -1673,10 +1673,16 @@ requests, the per-request `_meta.logLevel` envelope key is the filter — see #### Wire tightening (every era) -- **`CallToolResult.content` is required at the wire boundary.** The `content.default([])` - affordance was removed. Tool handlers MUST include `content` (the TypeScript surface - always required it; `content: []` is fine). A handler result without it is rejected - with `-32602`. +- **`CallToolResult.content` keeps the v1 parse tolerance on the legacy era.** An + inbound result without `content` defaults to `[]` (deployed servers omit it + alongside `structuredContent`); 2026-07-28 connections stay strict. Authoring is + unchanged and era-independent: the TypeScript surface requires `content` on handler + results, and a content-less handler result is normalized to `content: []` before it + reaches the wire. One sharpening remains: a content-less body carrying another + result family's vocabulary (a task handle or an `input_required` round) is still + rejected loudly — tolerance never turns a different result kind into a silent empty + success. A body whose only foreign key was `resultType` strips to an empty object + and defaults, exactly as v1 parsed a payload-free body. - **`ElicitResult.content` values are typed and validated as `string | number | boolean | string[]`.** v1's TypeScript surface accepted `Record` content values; an elicitation handler returning arbitrary diff --git a/packages/core-internal/eslint.config.mjs b/packages/core-internal/eslint.config.mjs index 64a6c212c0..ae8ff4ce39 100644 --- a/packages/core-internal/eslint.config.mjs +++ b/packages/core-internal/eslint.config.mjs @@ -7,7 +7,9 @@ export default [ { // Wire-layer isolation, outbound direction: nothing outside src/wire/ may // reach into a wire revision module. The wire layer's only public surface - // is src/wire/codec.ts (the WireCodec interface) and src/wire/bootstrap.ts. + // is src/wire/codec.ts (the WireCodec interface), src/wire/bootstrap.ts, + // and the leaf result-family module src/wire/resultFamilies.ts (the shared + // tools/call-result ruling, re-exported on the barrel). // test/wire/layeringInvariants.test.ts re-derives the same invariant with // zero exceptions. Type-only imports are exempted at the lint layer (a // type-only crossing is erased at runtime), but the test allows none. diff --git a/packages/core-internal/src/index.ts b/packages/core-internal/src/index.ts index 058de16174..3e2f961fbe 100644 --- a/packages/core-internal/src/index.ts +++ b/packages/core-internal/src/index.ts @@ -28,11 +28,15 @@ export * from './util/inMemory'; // 2026-only seam runs in. NOTHING per-revision (registries, codec objects, // per-revision schemas) is ever exported on this barrel — sibling packages // reach the wire layer ONLY through `codecForVersion`'s function-only -// `WireCodec` surface. +// `WireCodec` surface. Sole exemption: the shared result-family ruling +// (`wire/resultFamilies.ts`), era-independent by design — the server's +// authoring normalization and the e2e wire sniffer apply the same ruling +// (and name the same vocabulary) as the 2025 wire seam. export * from './util/schema'; export * from './util/standardSchema'; export * from './util/zodCompat'; export { codecForVersion, MODERN_WIRE_REVISION } from './wire/codec'; +export { normalizeContentlessToolResult, TOOL_RESULT_FOREIGN_FAMILY_KEYS } from './wire/resultFamilies'; // Validator provider classes stay subpath-only. Re-exporting them here, even as // `type`, can make generated client/server root declarations advertise diff --git a/packages/core-internal/src/types/guards.ts b/packages/core-internal/src/types/guards.ts index a0d575054e..eb1ebdf6af 100644 --- a/packages/core-internal/src/types/guards.ts +++ b/packages/core-internal/src/types/guards.ts @@ -84,7 +84,9 @@ export const isJSONRPCResponse = (value: unknown): value is JSONRPCResponse => J * @returns True if the value is a valid {@linkcode CallToolResult}, false otherwise. */ export const isCallToolResult = (value: unknown): value is CallToolResult => { - if (typeof value !== 'object' || value === null || !('content' in value)) return false; + // content === undefined covers an explicit undefined key too: the schema's + // .default([]) would parse it, but the narrowed type requires the array. + if (typeof value !== 'object' || value === null || (value as { content?: unknown }).content === undefined) return false; return CallToolResultSchema.safeParse(value).success; }; diff --git a/packages/core-internal/src/types/schemas.ts b/packages/core-internal/src/types/schemas.ts index 21960a6176..bc64597b98 100644 --- a/packages/core-internal/src/types/schemas.ts +++ b/packages/core-internal/src/types/schemas.ts @@ -1381,8 +1381,11 @@ export const CallToolResultSchema = ResultSchema.extend({ * * If the `Tool` does not define an outputSchema, this field MUST be present in the result. * Required on the wire per the specification (it may be an empty array). + * + * Parse-tolerant: absent `content` defaults to `[]` (v1 parity — deployed + * servers omit it alongside `structuredContent`). */ - content: z.array(ContentBlockSchema), + content: z.array(ContentBlockSchema).default([]), /** * Structured tool output. diff --git a/packages/core-internal/src/wire/resultFamilies.ts b/packages/core-internal/src/wire/resultFamilies.ts new file mode 100644 index 0000000000..10b0b9ee5e --- /dev/null +++ b/packages/core-internal/src/wire/resultFamilies.ts @@ -0,0 +1,24 @@ +/** + * Result-family keys that must never default into a `{content: []}` tools/call + * success. Shared by the 2025 wire-seam schema and server normalization. + * Leaf module (like `textFallback.ts`): imported by registry/server paths, so + * it must NOT import from `./codec.js` — that would close a runtime cycle. + */ +export const TOOL_RESULT_FOREIGN_FAMILY_KEYS = ['task', 'inputRequests', 'requestState'] as const; + +/** + * Single owner of the v1-parity ruling: a plain-object tool result without `content` (and + * without foreign-family keys) gains `content: []`. Shared by the 2025 wire seam and server-side handler normalization. + */ +export function normalizeContentlessToolResult(value: unknown): unknown { + if ( + value === null || + typeof value !== 'object' || + Array.isArray(value) || + (value as { content?: unknown }).content !== undefined || + TOOL_RESULT_FOREIGN_FAMILY_KEYS.some(key => key in value) + ) { + return value; + } + return { ...value, content: [] }; +} diff --git a/packages/core-internal/src/wire/rev2025-11-25/codec.ts b/packages/core-internal/src/wire/rev2025-11-25/codec.ts index ed30e9c67b..de39f8b21a 100644 --- a/packages/core-internal/src/wire/rev2025-11-25/codec.ts +++ b/packages/core-internal/src/wire/rev2025-11-25/codec.ts @@ -105,8 +105,9 @@ export const rev2025Codec: WireCodec = { decodeResult(_method: string, raw: unknown): DecodedResult { // Strip-on-lift (Q1-SD3 ii): a foreign `resultType` on the 2025 leg is - // dropped before validation, whatever its value. There is no - // discrimination on this era — `resultType` carries no meaning here. + // dropped before validation, whatever its value. Validation judges the + // husk — the registry wire-seam schema on the plain path, the caller's + // schema on the explicit path (task interop). if (isPlainObject(raw) && 'resultType' in raw) { const stripped = { ...raw }; delete stripped['resultType']; diff --git a/packages/core-internal/src/wire/rev2025-11-25/registry.ts b/packages/core-internal/src/wire/rev2025-11-25/registry.ts index f2878d429e..fb582559f9 100644 --- a/packages/core-internal/src/wire/rev2025-11-25/registry.ts +++ b/packages/core-internal/src/wire/rev2025-11-25/registry.ts @@ -21,9 +21,10 @@ * shells, `resultType`, the `_meta` envelope) has NO entry and NO code path * here — the inverse-leak guarantee is physical absence, not discipline. */ -import type * as z from 'zod/v4'; +import * as z from 'zod/v4'; import type { NotificationMethod, NotificationTypeMap, RequestMethod, RequestTypeMap, ResultTypeMap } from '../../types/types'; +import { normalizeContentlessToolResult, TOOL_RESULT_FOREIGN_FAMILY_KEYS } from '../resultFamilies'; import type { ClientNotificationSchema, ClientRequestSchema, ServerNotificationSchema, ServerRequestSchema } from './schemas'; import { CallToolRequestSchema, @@ -106,6 +107,31 @@ type Rev2025TypedRequestMethod = Extract; // no key may fall outside it (no `tasks/*` entries — the task methods are // 2025-11-25 wire vocabulary with no SDK runtime; callers needing task // interop pass an explicit schema). +/** + * Wire seam: owns both halves of the v1-parity ruling — the guard (a content-less body + * carrying another result family's keys fails loudly; the era is frozen so the key list is + * complete) and the tolerance (`content` defaults to `[]`). The era file stays twin-conformant. + */ +export const CallToolResultWireSchema = z + .unknown() + .superRefine((value, ctx) => { + // content === undefined covers both an absent key and an explicit + // undefined from server-side authoring objects. + if (typeof value !== 'object' || value === null || Array.isArray(value) || (value as Record).content !== undefined) + return; + for (const key of TOOL_RESULT_FOREIGN_FAMILY_KEYS) { + if (key in value) { + ctx.addIssue({ + code: 'custom', + message: `content is required when the body carries '${key}' — another result family cannot default into an empty tools/call success` + }); + return; + } + } + }) + .transform(normalizeContentlessToolResult) + .pipe(CallToolResultSchema); + const resultSchemas: { readonly [M in Rev2025TypedRequestMethod]: z.ZodType } = { ping: EmptyResultSchema, initialize: InitializeResultSchema, @@ -118,7 +144,7 @@ const resultSchemas: { readonly [M in Rev2025TypedRequestMethod]: z.ZodType { + protected assertCapabilityForMethod(): void {} + protected assertNotificationCapability(): void {} + protected assertRequestHandlerCapability(): void {} + protected buildContext(ctx: BaseContext): BaseContext { + return ctx; + } +} + +/** + * v1 parse-parity for `CallToolResult.content` on the legacy era: absent + * content defaults to []; another result family's content-less body still + * fails loudly via the registry wire-seam schema. + */ +describe('CallToolResult content default (v1 parity)', () => { + async function respondWith(body: Record, resultSchema?: Parameters['request']>[1]) { + const protocol = new TestProtocolImpl(); + const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair(); + serverTransport.onmessage = message => { + if (isJSONRPCRequest(message)) { + void serverTransport.send({ + jsonrpc: '2.0', + id: (message as JSONRPCRequest).id, + result: body + }); + } + }; + await serverTransport.start(); + await protocol.connect(clientTransport); + try { + return resultSchema === undefined + ? await protocol.request({ method: 'tools/call', params: { name: 'echo', arguments: {} } }) + : await protocol.request({ method: 'tools/call', params: { name: 'echo', arguments: {} } }, resultSchema); + } finally { + await protocol.close().catch(() => {}); + } + } + + it('a structured-only result resolves with content: []', async () => { + const result = (await respondWith({ structuredContent: { ok: true } })) as { + content: unknown; + structuredContent: unknown; + }; + expect(result.content).toEqual([]); + expect(result.structuredContent).toEqual({ ok: true }); + }); + + it('an entirely empty result resolves with content: []', async () => { + const result = (await respondWith({})) as { content: unknown }; + expect(result.content).toEqual([]); + }); + + it('a task-shaped body without content still fails loudly (wire-seam guard)', async () => { + await expect(respondWith({ task: { taskId: 't-1', status: 'working' } })).rejects.toBeInstanceOf(SdkError); + }); + + it('task interop via an explicit result schema still works — the guard never touches that overload', async () => { + const { CreateTaskResultSchema } = await import('../../src/wire/rev2025-11-25/schemas'); + const body = { + task: { + taskId: '786af6b0-2779-48ed-9cc1-b8a8a25b8a86', + status: 'working', + createdAt: '2025-11-25T10:30:00Z', + lastUpdatedAt: '2025-11-25T10:30:05Z', + ttl: 60000, + pollInterval: 5000 + } + }; + const result = (await respondWith(body, CreateTaskResultSchema)) as { task: { taskId: string } }; + expect(result.task.taskId).toBe('786af6b0-2779-48ed-9cc1-b8a8a25b8a86'); + }); + + it('explicit-schema task interop resolves even when the body also stamps a foreign resultType', async () => { + const { CreateTaskResultSchema } = await import('../../src/wire/rev2025-11-25/schemas'); + const body = { + resultType: 'complete', + task: { + taskId: '786af6b0-2779-48ed-9cc1-b8a8a25b8a86', + status: 'working', + createdAt: '2025-11-25T10:30:00Z', + lastUpdatedAt: '2025-11-25T10:30:05Z', + ttl: 60000, + pollInterval: 5000 + } + }; + const result = (await respondWith(body, CreateTaskResultSchema)) as { task: { taskId: string } }; + expect(result.task.taskId).toBe('786af6b0-2779-48ed-9cc1-b8a8a25b8a86'); + }); + + it('the wire-seam guard treats an explicit content: undefined like an absent key', async () => { + const { getResultSchema } = await import('../../src/wire/rev2025-11-25/registry'); + const wireSeam = getResultSchema('tools/call')!; + expect(wireSeam.safeParse({ task: { taskId: 't-1', status: 'working' }, content: undefined }).success).toBe(false); + }); + + it('an input_required-shaped body without content still fails loudly (wire-seam guard)', async () => { + await expect(respondWith({ inputRequests: { r1: { method: 'elicitation/create' } } })).rejects.toBeInstanceOf(SdkError); + await expect(respondWith({ requestState: 'opaque-token' })).rejects.toBeInstanceOf(SdkError); + }); +}); diff --git a/packages/core-internal/test/shared/rawResultTypeFirst.test.ts b/packages/core-internal/test/shared/rawResultTypeFirst.test.ts index 3e44c48ed7..87519c1562 100644 --- a/packages/core-internal/test/shared/rawResultTypeFirst.test.ts +++ b/packages/core-internal/test/shared/rawResultTypeFirst.test.ts @@ -137,12 +137,9 @@ describe('raw-first resultType discrimination — 2026 era (codec decode step 1) describe('raw-first resultType handling — 2025 era (strip-on-lift, Q1-SD3 ii)', () => { test('a foreign input_required body is stripped, then validation judges the content — never a silent success', async () => { - // BEHAVIOR MIGRATION (ledgered): pre-split, the era-blind funnel arm - // rejected this with UnsupportedResultType on every leg. On the 2025 - // era resultType carries no meaning — the ruled posture strips the - // foreign key and lets validation decide. The body has no content, - // so it fails the (default-free) tools/call result schema LOUDLY — - // the V-1 invariant (never a hollow success) holds. + // BEHAVIOR MIGRATION (ledgered): the strip drops the foreign key and + // the wire-seam schema refuses to default a husk carrying + // input_required keys — V-1 (never a hollow success) holds at the seam. const protocol = await wireWithRawResult(INPUT_REQUIRED_BODY); const outcome = await settle(protocol); diff --git a/packages/core-internal/test/shared/typedMapAlignment.test.ts b/packages/core-internal/test/shared/typedMapAlignment.test.ts index eacf493e4d..a8ad903651 100644 --- a/packages/core-internal/test/shared/typedMapAlignment.test.ts +++ b/packages/core-internal/test/shared/typedMapAlignment.test.ts @@ -93,18 +93,11 @@ describe('task-shaped result bodies against the narrowed runtime map', () => { await protocol.close(); }); - test('tools/call: a CreateTaskResult body is now a typed invalid-result error too (content-default removal flip)', async () => { - // FLIPPED PIN (Q1 increment 2, ledgered with the content-default - // removal — changeset: codec-split-wire-break). The previous "Honest - // pin, not an endorsement" recorded that CallToolResultSchema's - // content.default([]) swallowed ANY object — including a task body — - // as a content-empty success, which made the old union member - // unreachable and the map narrowing observationally invisible for - // tools/call. With `content` now REQUIRED at the wire boundary the - // masking surface is gone: a task body has no `content`, fails the - // plain schema, and surfaces as the same typed invalid-result error - // as sampling/elicit. The result-schema-strictness question the old - // pin deferred is hereby resolved: loud rejection. + test('tools/call: a CreateTaskResult body on the plain path is a typed invalid-result error (wire-seam guard)', async () => { + // FLIPPED PIN, twice-ledgered (changesets: codec-split-wire-break, + // calltoolresult-content-default). The wire-seam schema restores the + // v1 default for plain results but refuses to default a body carrying + // another result family's keys — a task body fails it loudly. const protocol = await wireWithRawResult(CREATE_TASK_RESULT_BODY); const rejection = await protocol diff --git a/packages/core-internal/test/types.test.ts b/packages/core-internal/test/types.test.ts index ba7adca0fb..e836086bfc 100644 --- a/packages/core-internal/test/types.test.ts +++ b/packages/core-internal/test/types.test.ts @@ -296,12 +296,14 @@ describe('Types', () => { } }); - test('requires content: the empty-object result no longer parses (deliberate flip)', () => { - // BEHAVIOR MIGRATION (Q1 increment 2, ledgered): content.default([]) - // was removed from the wire schema (the T6 silent-empty-success - // masking root). Content is spec-required in every revision. - // Changeset: codec-split-wire-break. - expect(CallToolResultSchema.safeParse({}).success).toBe(false); + test('tolerates absent content: the empty-object result parses with content [] (v1 parity restored)', () => { + // BEHAVIOR MIGRATION (reversal, ledgered): content.default([]) is + // back on the neutral layer + 2025 era; T6 closed at the wire seam. + const empty = CallToolResultSchema.safeParse({}); + expect(empty.success).toBe(true); + if (empty.success) { + expect(empty.data.content).toEqual([]); + } const result = CallToolResultSchema.safeParse({ content: [] }); expect(result.success).toBe(true); if (result.success) { diff --git a/packages/core-internal/test/types/guards.test.ts b/packages/core-internal/test/types/guards.test.ts index fe96b64dd3..5db5f3bdb0 100644 --- a/packages/core-internal/test/types/guards.test.ts +++ b/packages/core-internal/test/types/guards.test.ts @@ -81,6 +81,10 @@ describe('isCallToolResult', () => { expect(isCallToolResult({})).toBe(false); }); + it('returns false for an explicit content: undefined (the schema default parses it, but the narrowed type requires the array)', () => { + expect(isCallToolResult({ content: undefined, structuredContent: { ok: true } })).toBe(false); + }); + it('returns true for a result with content', () => { expect( isCallToolResult({ diff --git a/packages/core-internal/test/types/registryPins.test.ts b/packages/core-internal/test/types/registryPins.test.ts index 9af70927f2..6e8a9d5c18 100644 --- a/packages/core-internal/test/types/registryPins.test.ts +++ b/packages/core-internal/test/types/registryPins.test.ts @@ -22,7 +22,7 @@ import { describe, expect, it } from 'vitest'; // Post-relocation home (Q1 increment-2 step 1): the pinned contents are // unchanged — only the module housing the registries moved. -import { getNotificationSchema, getRequestSchema, getResultSchema } from '../../src/wire/rev2025-11-25/registry'; +import { getNotificationSchema, getRequestSchema, CallToolResultWireSchema, getResultSchema } from '../../src/wire/rev2025-11-25/registry'; // The 2025 wire schemas are fully self-contained in the era's schema module: // every per-method schema the registry serves is a FROZEN 2025-11-25 copy so // the public/neutral layer can evolve (e.g. SEP-2106 widening) without @@ -130,7 +130,9 @@ const EXPECTED_RESULT_SCHEMAS = { 'resources/read': ReadResourceResultSchema, 'resources/subscribe': EmptyResultSchema, 'resources/unsubscribe': EmptyResultSchema, - 'tools/call': CallToolResultSchema, + // The wire-seam wrapper (content-default guard) IS the pinned entry — + // identity to the wrapper, which pipes into the plain schema. + 'tools/call': CallToolResultWireSchema, 'tools/list': ListToolsResultSchema, 'sampling/createMessage': CreateMessageResultWithToolsSchema, 'elicitation/create': ElicitResultSchema, diff --git a/packages/core-internal/test/types/schemaBoundaryPins.test.ts b/packages/core-internal/test/types/schemaBoundaryPins.test.ts index 5dd6a11727..d28a7963e0 100644 --- a/packages/core-internal/test/types/schemaBoundaryPins.test.ts +++ b/packages/core-internal/test/types/schemaBoundaryPins.test.ts @@ -34,6 +34,8 @@ import { ListToolsResultSchema as Wire2026ListToolsResultSchema, RequestMetaEnvelopeSchema } from '../../src/wire/rev2026-07-28/schemas'; +import { getResultSchema as getRev2025ResultSchema } from '../../src/wire/rev2025-11-25/registry'; +import { CallToolResultSchema as Wire2025CallToolResultSchema } from '../../src/wire/rev2025-11-25/schemas'; import type { CallToolResult, CompleteResult, @@ -139,18 +141,17 @@ describe('typed result schemas are loose', () => { expect((parsed as Record).ttlMs).toBe(5); }); - test('CallToolResult requires content on the wire (the silent-empty-success default is gone)', () => { - // BEHAVIOR MIGRATION (Q1 increment 2, ledgered): `content.default([])` - // was removed from the wire schema. The default was the T6 width-leak - // root: a task-shaped (or otherwise content-less) body parsed as a - // silent `{content: []}` success. Content is required by the spec in - // every revision; a content-less body now fails the parse LOUDLY. - // Changeset: codec-split-wire-break; docs/migration/upgrade-to-v2.md - // "Wire tightening (every era)". - expect(CallToolResultSchema.safeParse({ structuredContent: { ok: true } }).success).toBe(false); - const parsed = CallToolResultSchema.parse({ content: [], structuredContent: { ok: true } }); + test('CallToolResult tolerates absent content on the wire (defaults to [], v1 parity)', () => { + // BEHAVIOR MIGRATION (reversal, ledgered): `content.default([])` was + // removed in the codec split (T6 width-leak root: a task-shaped body + // parsed as a silent success), then RESTORED for ecosystem parity — + // real deployments omit `content` alongside `structuredContent`. The + // T6 leak stays closed at the 2025 wire-seam schema; 2026 stays strict. + const parsed = CallToolResultSchema.parse({ structuredContent: { ok: true } }); expect(parsed.content).toEqual([]); expect(parsed.structuredContent).toEqual({ ok: true }); + const explicit = CallToolResultSchema.parse({ content: [], structuredContent: { ok: true } }); + expect(explicit.content).toEqual([]); }); test('CallToolResult preserves isError and sibling members through the parse', () => { @@ -167,6 +168,15 @@ describe('typed result schemas are loose', () => { }); }); +describe('2025 wire layering: era file spec-strict, era seam tolerant', () => { + test('the era-schema file rejects a content-less CallToolResult body; the registry seam defaults it', () => { + // Layering rule: era-schema files stay spec-verbatim (the CallToolResult twin requires content); the seam's v1-parity tolerance defaults CallToolResult-family bodies only — foreign-family results (task/inputRequests/requestState) are never defaulted. + expect(Wire2025CallToolResultSchema.safeParse({ structuredContent: {} }).success).toBe(false); + const seamParsed = getRev2025ResultSchema('tools/call')!.parse({ structuredContent: {} }) as { content: unknown }; + expect(seamParsed.content).toEqual([]); + }); +}); + describe('completion result boundary', () => { test('the completion object is loose: unknown sibling fields are preserved', () => { const parsed = CompleteResultSchema.parse({ completion: { values: ['alpha'], extraField: 'kept' } }); diff --git a/packages/core-internal/test/types/specTypeSchema.test.ts b/packages/core-internal/test/types/specTypeSchema.test.ts index 3266b09521..d6c775b4e3 100644 --- a/packages/core-internal/test/types/specTypeSchema.test.ts +++ b/packages/core-internal/test/types/specTypeSchema.test.ts @@ -90,20 +90,20 @@ describe('isSpecType', () => { } }); - it('CallToolResult requires content at the boundary (the wire default was removed)', () => { - // BEHAVIOR MIGRATION (Q1 increment 2, ledgered): CallToolResultSchema - // lost `content.default([])` — the silent-empty-success masking root. - // The guard's input shape now requires content, matching the spec in - // every revision. Changeset: codec-split-wire-break. + it('CallToolResult tolerates absent content at the boundary (default restored, v1 parity)', () => { + // BEHAVIOR MIGRATION (reversal, ledgered): the guard accepts a + // content-less body as v1's did; the task-husk leak is closed at the + // 2025 wire-seam schema instead. const empty: unknown = {}; - expect(isSpecType.CallToolResult(empty)).toBe(false); + expect(isSpecType.CallToolResult(empty)).toBe(true); const v: unknown = { content: [] }; expect(isSpecType.CallToolResult(v)).toBe(true); if (isSpecType.CallToolResult(v)) { - expectTypeOf(v.content).toEqualTypeOf(); - expectTypeOf(v.content).not.toEqualTypeOf(); + // The guard narrows to the INPUT type: content optional pre-parse. + expectTypeOf(v.content).toEqualTypeOf(); } - void (0 as unknown as CallToolResult); + // The parsed/public type keeps content required (z.output). + expectTypeOf().toEqualTypeOf(); }); it('JSONValue / JSONObject — narrows to the JSON type, not unknown', () => { diff --git a/packages/core-internal/test/wire/eraGates.test.ts b/packages/core-internal/test/wire/eraGates.test.ts index 8ecd642a1e..b03202e011 100644 --- a/packages/core-internal/test/wire/eraGates.test.ts +++ b/packages/core-internal/test/wire/eraGates.test.ts @@ -574,27 +574,19 @@ describe('T6 width-leak killed at both roots', () => { expect(decoded.kind).toBe('invalid'); }); - test('2025 era: with the content default gone, a bare task-shaped body fails the plain schema loudly', async () => { + test('2025 era: a bare task-shaped body fails the wire-seam schema — the content default cannot mask it', async () => { const { rev2025Codec } = await import('../../src/wire/rev2025-11-25/codec'); - const { CallToolResultSchema } = await import('../../src/wire/rev2025-11-25/schemas'); const decoded = rev2025Codec.decodeResult('tools/call', { task: { taskId: 't-1', status: 'working' } }); + // Decode passes bodies without resultType through (explicit-schema + // task interop must work); the wire-seam schema does the refusing. expect(decoded.kind).toBe('complete'); - if (decoded.kind === 'complete') { - // The plain schema (which IS the registry entry — the result map - // is aligned to the typed map, no task-widened union): no - // default([]) means no silent {content: []} masking. - expect(CallToolResultSchema.safeParse(decoded.result).success).toBe(false); - } - // The GENERIC path agrees: the registry serves the same plain schema, - // so even a fully conforming CreateTaskResult body is a loud schema - // failure (surfaced as a typed INVALID_RESULT — see - // test/shared/typedMapAlignment.test.ts). Task interop is the - // explicit-schema overload, never a silent union member. + // The wire-seam schema rejects even a fully conforming + // CreateTaskResult on the plain path (typed INVALID_RESULT); task + // interop is the explicit-schema overload only. const { getResultSchema } = await import('../../src/wire/rev2025-11-25/registry'); - const plain = getResultSchema('tools/call'); - expect(plain).toBe(CallToolResultSchema); + const wireSeam = getResultSchema('tools/call'); expect( - plain!.safeParse({ + wireSeam!.safeParse({ task: { taskId: '786af6b0-2779-48ed-9cc1-b8a8a25b8a86', status: 'working', @@ -605,5 +597,7 @@ describe('T6 width-leak killed at both roots', () => { } }).success ).toBe(false); + // While a plain content-less tool result still defaults: + expect(wireSeam!.safeParse({ structuredContent: { ok: true } }).success).toBe(true); }); }); diff --git a/packages/server/src/server/server.ts b/packages/server/src/server/server.ts index 4151849494..d69e01c1b2 100644 --- a/packages/server/src/server/server.ts +++ b/packages/server/src/server/server.ts @@ -52,6 +52,7 @@ import { missingClientCapabilities, MissingRequiredClientCapabilityError, modernProtocolVersions, + normalizeContentlessToolResult, parseSchema, Protocol, ProtocolError, @@ -524,7 +525,12 @@ export class Server extends Protocol { return result; } - const validationResult = codec.validateResult('tools/call', result); + // v1-parity authoring affordance, era-independent: a content-less + // handler result normalizes to content: [] before era validation. + // Other result families' bodies stay un-normalized and fail loudly. + const normalizedResult = normalizeContentlessToolResult(result); + + const validationResult = codec.validateResult('tools/call', normalizedResult); if (!validationResult.ok) { throw new ProtocolError( validationResult.reason === 'not-in-era' ? ProtocolErrorCode.InternalError : ProtocolErrorCode.InvalidParams, diff --git a/packages/server/test/server/server.test.ts b/packages/server/test/server/server.test.ts index f6c40602ae..0a96a0bb66 100644 --- a/packages/server/test/server/server.test.ts +++ b/packages/server/test/server/server.test.ts @@ -154,13 +154,10 @@ describe('Server', () => { }); }); - describe('tools/call handler-result validation (required content)', () => { - // Server-side pin for the documented wire break (docs/migration/upgrade-to-v2.md, - // "Wire tightening (every era)"): with the - // content.default([]) affordance removed, a handler result without - // `content` is rejected with -32602 `Invalid tools/call result` — - // never silently defaulted onto the wire — while an authored-content - // result passes through the wrapped handler untouched. + describe('tools/call handler-result validation (content default)', () => { + // Pin for the v1-parity authoring affordance: content-less handler + // results normalize to content: [] before era validation, on every + // leg; other result families are not normalized. async function callToolOnServer(result: CallToolResult): Promise { const server = new Server({ name: 'test', version: '1.0.0' }, { capabilities: { tools: {} } }); server.setRequestHandler('tools/call', () => result); @@ -193,13 +190,42 @@ describe('Server', () => { return response; } - it('rejects a structured-only handler result (no content) with -32602 Invalid tools/call result', async () => { + it('defaults a structured-only handler result (no content) to content: [] on the wire (v1 parity)', async () => { + // Runtime defaults structured-only results (v1 parity); the wire + // stays spec-valid with content: []. const response = await callToolOnServer({ structuredContent: { ok: true } } as unknown as CallToolResult); + const result = (response as { result?: { content?: unknown; structuredContent?: unknown } }).result; + expect(result).toBeDefined(); + expect(result!.content).toEqual([]); + expect(result!.structuredContent).toEqual({ ok: true }); + }); + + it('does not normalize an array handler result — rejected loudly', async () => { + const response = await callToolOnServer([{ type: 'text', text: 'hi' }] as unknown as CallToolResult); + const error = (response as { error?: { code: number } }).error; + expect(error).toBeDefined(); + expect(error!.code).toBe(-32602); + }); + + it('does not normalize a foreign-family body with an explicit content: undefined', async () => { + const response = await callToolOnServer({ + task: { taskId: 't-1', status: 'working' }, + content: undefined + } as unknown as CallToolResult); + const error = (response as { error?: { code: number } }).error; + expect(error).toBeDefined(); + expect(error!.code).toBe(-32602); + }); + + it('does not normalize a body carrying another result family — rejected loudly', async () => { + const response = await callToolOnServer({ + inputRequests: { r1: { method: 'elicitation/create' } } + } as unknown as CallToolResult); + const error = (response as { error?: { code: number; message: string } }).error; expect(error).toBeDefined(); expect(error!.code).toBe(-32602); - expect(error!.message).toContain('Invalid tools/call result'); }); it('passes an authored-content result through to the wire', async () => { diff --git a/test/e2e/helpers/wire-sniffer.ts b/test/e2e/helpers/wire-sniffer.ts index 4e2c26bea5..912e2e047e 100644 --- a/test/e2e/helpers/wire-sniffer.ts +++ b/test/e2e/helpers/wire-sniffer.ts @@ -4,7 +4,8 @@ import { ClientResultSchema, ServerNotificationSchema, ServerRequestSchema, - ServerResultSchema + ServerResultSchema, + TOOL_RESULT_FOREIGN_FAMILY_KEYS } from '@modelcontextprotocol/core-internal'; import type { Transport } from '@modelcontextprotocol/server'; import { @@ -16,6 +17,10 @@ import { isSpecType } from '@modelcontextprotocol/server'; +function isPlainRecord(v: unknown): v is Record { + return typeof v === 'object' && v !== null && !Array.isArray(v); +} + export type WireParty = 'client' | 'server'; export interface SnifferOptions { @@ -100,7 +105,28 @@ export function assertWireMessage(msg: unknown, party: WireParty, opts: SnifferO // (InputRequiredResultSchema lives alongside, never widening it). // Era-gated: only cells wired for the modern era opt in, so an // input_required on a 2025-era cell's wire is still flagged. - if (party === 'server' && opts.allowInputRequiredResults === true && isInputRequiredResult(result)) return; + if (party === 'server' && isInputRequiredResult(result)) { + if (opts.allowInputRequiredResults === true) return; + fail(party, `input_required result on a cell not opted into multi-round-trip`, msg); + } + // With content.default([]) restored, the neutral union accepts any + // object via the CallToolResult member — so union conformance below + // is a weak check, and the other result families are asserted here + // explicitly, by vocabulary. Cells that opt into vendor-extension + // methods skip this check (the sniffer is method-blind for results; + // before the default was restored these bodies failed the union parse + // and were excused by the same flag). + if (party === 'server' && !opts.allowCustomMethods && isPlainRecord(result) && result.content === undefined) { + for (const key of TOOL_RESULT_FOREIGN_FAMILY_KEYS) { + if (key in result) { + fail( + party, + `content-less result carrying '${key}' — another result family must not read as an empty tools/call success`, + msg + ); + } + } + } const r = schemas.result.safeParse(result); if (!r.success) { // A result for a vendor-extension request legitimately won't match the spec union. diff --git a/test/e2e/scenarios/raw-result-type.test.ts b/test/e2e/scenarios/raw-result-type.test.ts index eb42e73f30..38db4c9a25 100644 --- a/test/e2e/scenarios/raw-result-type.test.ts +++ b/test/e2e/scenarios/raw-result-type.test.ts @@ -14,10 +14,10 @@ * ABSENT `resultType` is a spec violation surfaced as a typed error * naming it. * - Negotiated legacy (2025 era): `resultType` is FOREIGN vocabulary — - * strip-on-lift (Q1-SD3 ii; a deliberate, ledgered change from the - * pre-split era-blind rejection — changeset: codec-split-wire-break). The - * stripped body then fails the (default-free) result schema loudly - * because it has no content. + * strip-on-lift (Q1-SD3 ii). The stripped husk still carries the + * input_required family keys, so the wire-seam schema rejects it loudly + * (a content-less body without family keys would default to content: [] + * — changeset: calltoolresult-content-default). * * Either way the V-1 invariant holds: never an empty-content success. */ @@ -115,8 +115,8 @@ verifies('typescript:client:raw-result-type-first', async ({ transport }: TestAr try { const outcome = await callToolOutcome(client); - // Strip-on-lift (Q1-SD3 ii, ledgered): the foreign resultType is - // dropped; the body has no content, so validation fails LOUDLY. + // Strip-on-lift: the foreign resultType is dropped; the husk's + // input_required family keys fail the wire-seam schema LOUDLY. // Never an empty-content success. expect('resolved' in outcome, `must not resolve: ${JSON.stringify(outcome)}`).toBe(false); const rejection = (outcome as { rejected: unknown }).rejected; diff --git a/test/integration/test/client/client.test.ts b/test/integration/test/client/client.test.ts index 5cfa15abf2..92140b531f 100644 --- a/test/integration/test/client/client.test.ts +++ b/test/integration/test/client/client.test.ts @@ -1828,8 +1828,6 @@ describe('outputSchema validation', () => { server.setRequestHandler('tools/call', async request => { if (request.params.name === 'test-tool') { return { - // content is spec-required (the wire default([]) was removed - // - ledgered; changeset codec-split-wire-break) content: [], structuredContent: { result: 'success', count: 42 } };