Skip to content

fix(agent-kit): pass MCP tool schemas through adapters as JSON Schema#319

Open
ATOM00blue wants to merge 1 commit into
inngest:mainfrom
ATOM00blue:fix/mcp-tools-zod-v4-compat
Open

fix(agent-kit): pass MCP tool schemas through adapters as JSON Schema#319
ATOM00blue wants to merge 1 commit into
inngest:mainfrom
ATOM00blue:fix/mcp-tools-zod-v4-compat

Conversation

@ATOM00blue
Copy link
Copy Markdown

Summary

Fixes #312.

MCP tools crash with TypeError: Cannot read properties of undefined (reading 'def')
when network.run() builds an LLM request. listMCPTools converts each MCP tool's
JSON Schema into a Zod schema via @dmitryrechkin/json-schema-to-zod, which bundles
Zod v3, but the model adapters call Zod v4's toJSONSchema() on those schemas. v4's
toJSONSchema() reads .def, which doesn't exist on a v3 schema, so it throws.

MCP tools already expose a JSON Schema on mcp.tool.inputSchema, so the
JSON Schema -> Zod -> JSON Schema round-trip is unnecessary. This PR:

  • stops producing the incompatible Zod schema in listMCPTools and keeps the raw
    mcp.tool.inputSchema on the tool;
  • has the Anthropic, OpenAI, and Gemini adapters fall back to that raw schema when
    parameters is absent (the Gemini path still strips fields Gemini rejects);
  • removes the now-unused @dmitryrechkin/json-schema-to-zod dependency, which was the
    source of the Zod v3/v4 mismatch.

Test plan

  • Added a test that starts an MCP server, runs initMCP, and verifies the tool
    flows through all three adapters' requestParser without throwing (fails before
    this change, passes after).
  • pnpm --filter @inngest/agent-kit test — all green.
  • Build, lint, and type-check pass.
  • Existing createTool/Zod-parameter tests still pass (non-MCP path unaffected).

MCP tools failed with "TypeError: Cannot read properties of undefined
(reading 'def')" when building an LLM request. listMCPTools converted the
MCP JSON Schema into a Zod schema via @dmitryrechkin/json-schema-to-zod,
which bundles Zod v3, while the model adapters call Zod v4's toJSONSchema()
on those schemas. The v3/v4 mismatch made toJSONSchema() read .def on an
incompatible schema and crash.

MCP tools already expose a JSON Schema on mcp.tool.inputSchema, so the
round-trip is unnecessary. Stop producing the incompatible Zod schema in
listMCPTools and have the Anthropic, OpenAI and Gemini adapters fall back
to the raw inputSchema. The unused json-schema-to-zod dependency is removed.
Copilot AI review requested due to automatic review settings May 21, 2026 04:02
@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented May 21, 2026

🦋 Changeset detected

Latest commit: 5e7c6d7

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

This PR includes changesets to release 1 package
Name Type
@inngest/agent-kit Patch

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

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes MCP tool crashes caused by a Zod v3/v4 mismatch by avoiding JSON Schema → Zod → JSON Schema conversion for MCP-provided tool schemas and passing the original MCP JSON Schema through the request adapters.

Changes:

  • Stop converting MCP tool inputSchema into Zod in Agent.listMCPTools; keep raw MCP JSON Schema on mcp.tool.inputSchema.
  • Update Anthropic/OpenAI/Gemini adapters to use t.mcp.tool.inputSchema when t.parameters is absent (Gemini still strips unsupported fields).
  • Remove @dmitryrechkin/json-schema-to-zod dependency and add a regression test covering all three adapters’ request parsers.

Reviewed changes

Copilot reviewed 7 out of 8 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
packages/agent-kit/src/agent.ts Removes MCP JSONSchema→Zod conversion and stores MCP tools with raw inputSchema on mcp.tool.
packages/agent-kit/src/agent.mcp.test.ts Adds regression test ensuring MCP tool schemas flow through all adapters without throwing.
packages/agent-kit/src/adapters/anthropic.ts Falls back to MCP inputSchema when Zod parameters are absent.
packages/agent-kit/src/adapters/openai.ts Falls back to MCP inputSchema when Zod parameters are absent.
packages/agent-kit/src/adapters/gemini.ts Falls back to MCP inputSchema and recursively strips unsupported fields (e.g., additionalProperties).
packages/agent-kit/package.json Drops @dmitryrechkin/json-schema-to-zod dependency.
packages/agent-kit/pnpm-lock.yaml Removes @dmitryrechkin/json-schema-to-zod and updates lock entries.
.changeset/quiet-tigers-flow.md Adds changeset entry for patch release describing the MCP crash fix.
Files not reviewed (1)
  • packages/agent-kit/pnpm-lock.yaml: Language not supported

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +86 to 91
parameters: t.parameters
? z.toJSONSchema(t.parameters, { target: "draft-7" })
: t.mcp?.tool?.inputSchema,
strict:
typeof t.strict !== "undefined" ? t.strict : Boolean(t.parameters), // strict mode is only supported with parameters
},
Comment on lines 28 to +37
description: t.description,
parameters: t.parameters
? geminiZodToJsonSchema(t.parameters)
: // eslint-disable-next-line @typescript-eslint/no-explicit-any
(geminiZodToJsonSchema(z.object({})) as any),
: t.mcp?.tool?.inputSchema
? // MCP tools already expose a JSON Schema; strip the fields Gemini
// does not accept and pass it through directly.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(recursiveGeminiZodToJsonSchema(t.mcp.tool.inputSchema) as any)
: // eslint-disable-next-line @typescript-eslint/no-explicit-any
(geminiZodToJsonSchema(z.object({})) as any),
inngest@3.43.1:
resolution: {integrity: sha512-zG+BuENQFPUFNVM2sZRRhDXDTh3PAH6KPuus9a/ZTu7LGnAvE+HvHZFmRcBND2LFN6phQ6sIyqB34LV90wg3IA==}
engines: {node: '>=20'}
deprecated: 'CRITICAL SECURITY: upgrade to >=3.54.0'
Comment on lines +81 to +96
test("MCP tool schema flows through model adapters without crashing", async () => {
await newMCPServer(3002, createStreamableHTTPTransport);

const agent = new Agent({
name: "test",
system: "noop",
mcpServers: [
{
name: "test",
transport: {
type: "streamable-http",
url: "http://localhost:3002/mcp",
},
},
],
});
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.

MCP tools fail with Cannot read properties of undefined (reading 'def') — Zod v3/v4 mismatch in listMCPTools

2 participants