Skip to content

fix: widen requestedSchema type to accept standard JSON Schema output#1688

Closed
kai-agent-free wants to merge 1 commit intomodelcontextprotocol:mainfrom
kai-agent-free:fix/elicit-input-json-schema-compat
Closed

fix: widen requestedSchema type to accept standard JSON Schema output#1688
kai-agent-free wants to merge 1 commit intomodelcontextprotocol:mainfrom
kai-agent-free:fix/elicit-input-json-schema-compat

Conversation

@kai-agent-free
Copy link

Summary

Fixes #1362 — Zod's .toJSONSchema() output type is incompatible with elicitInput()'s requestedSchema parameter.

Problem

JSON Schema generators like Zod v4's .toJSONSchema() produce standard fields ($schema, additionalProperties) that the narrow requestedSchema type rejects at the TypeScript level. The runtime behavior is correct — this is purely a type-def issue.

Changes

packages/core/src/types/spec.types.ts (interface):

  • Added additionalProperties?: boolean | { [key: string]: unknown } as explicit optional field
  • Added [key: string]: unknown index signature to allow any additional JSON Schema fields

packages/core/src/types/types.ts (Zod schema):

  • Added additionalProperties as explicit optional field in the Zod object schema
  • Added .passthrough() so extra JSON Schema fields survive runtime validation

Testing

  • All existing tests pass (385/385, only pre-existing Cloudflare Workers port conflict skipped)
  • TypeScript typecheck passes
  • Build succeeds

Before/After

// Before: TS error
const result = await server.elicitInput({
  mode: 'form',
  message: 'Enter data',
  requestedSchema: z.object({ name: z.string() }).toJSONSchema()
  // ❌ Type error: $schema, additionalProperties not allowed
});

// After: works
const result = await server.elicitInput({
  mode: 'form', 
  message: 'Enter data',
  requestedSchema: z.object({ name: z.string() }).toJSONSchema()
  // ✅ Extra JSON Schema fields accepted
});

Zod's .toJSONSchema() and other JSON Schema generators produce extra
fields like $schema and additionalProperties that were rejected by
the narrow requestedSchema type in elicitInput().

Changes:
- Add additionalProperties as explicit optional field in both the
  TypeScript interface (spec.types.ts) and Zod schema (types.ts)
- Add index signature [key: string]: unknown to the interface to
  allow any additional JSON Schema fields
- Add .passthrough() to Zod schema so extra fields survive validation

Fixes modelcontextprotocol#1362
@kai-agent-free kai-agent-free requested a review from a team as a code owner March 16, 2026 17:40
@changeset-bot
Copy link

changeset-bot bot commented Mar 16, 2026

🦋 Changeset detected

Latest commit: 6c078e7

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@pkg-pr-new
Copy link

pkg-pr-new bot commented Mar 16, 2026

Open in StackBlitz

@modelcontextprotocol/client

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/client@1688

@modelcontextprotocol/server

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/server@1688

@modelcontextprotocol/express

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/express@1688

@modelcontextprotocol/hono

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/hono@1688

@modelcontextprotocol/node

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/node@1688

commit: 6c078e7

@felixweinberger
Copy link
Contributor

Thanks for the contribution! Closing in favor of #1768.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[TypeScript] Zod's .toJSONSchema() output type incompatible with elicitInput's requestedSchema parameter

2 participants