Skip to content

schemaAdapter fails with Zod schemas containing .transform() - cannot convert to JSON Schema #143

@Nlea

Description

@Nlea

When using mcp-lite with Cloudflare Workers, tools that use Zod schemas with .transform() methods fail during deployment with error code 10021. The default schemaAdapter implementation uses z.toJSONSchema() which doesn't support Zod transforms.

Environment

mcp-lite: 0.8.1
zod: 4.1.12
Platform: Cloudflare Workers
Runtime: Cloudflare Workers runtime

Steps to reproduce

import { McpServer } from "mcp-lite";
import { z } from "zod";

const mcp = new McpServer({
  name: "example",
  version: "1.0.0",
  schemaAdapter: (schema) => z.toJSONSchema(schema as z.ZodType),
});

mcp.tool("example-tool", {
  description: "Example tool",
  inputSchema: z.object({
    // This will fail during deployment
    field: z.string().transform(val => val.toLowerCase()),
  }),
  handler: async (args) => {
    return { content: [{ type: "text", text: "ok" }] };
  },
});

Error Message

Uncaught Error: Transforms cannot be represented in JSON Schema
  at null.<anonymous>
  (file:///node_modules/.pnpm/[email protected]/node_modules/zod/v4/core/to-json-schema.js:445:35)
  in process
  at schemaAdapter
  [code: 10021]

Current Workaround

Remove .transform() from schemas and handle transformations in the handler function instead:

inputSchema: z.object({
  field: z.string(), // Remove .transform()
}),
handler: async (args) => {
  const normalizedField = args.field.toLowerCase(); // Transform here instead
  // ...
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions