Skip to content

fix(mistralai): handle both Zod and JSON schemas in tool conversion #8269

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

christian-bromann
Copy link

@christian-bromann christian-bromann commented May 29, 2025

Description

Fixes a bug where using MCP (Model Context Protocol) tools with ChatMistralAI would throw a TypeError: Cannot read properties of undefined (reading 'typeName') error.

fixes #8171

Problem

The _convertToolToMistralTool function was assuming all tool schemas were Zod schemas and unconditionally calling zodToJsonSchema() on them. However, MCP tools from @langchain/mcp-adapters provide tools with JSON schemas already, not Zod schemas. This mismatch caused the zodToJsonSchema function to fail when trying to access the typeName property on a plain JSON schema object.

Error Stack Trace:

TypeError: Cannot read properties of undefined (reading 'typeName')
    at parseDef (/node_modules/.pnpm/[email protected][email protected]/node_modules/zod-to-json-schema/dist/cjs/parseDef.js:22:77)
    at zodToJsonSchema (/node_modules/.pnpm/[email protected][email protected]/node_modules/zod-to-json-schema/dist/cjs/zodToJsonSchema.js:22:45)
    at _convertToolToMistralTool (/node_modules/.pnpm/@[email protected]_@[email protected][email protected][email protected][email protected]__/node_modules/@langchain/mistralai/dist/chat_models.cjs:261:70)

Solution

Modified the _convertToolToMistralTool function to:

  1. Check if the tool schema is a Zod schema using the existing isZodSchema helper function
  2. Only call zodToJsonSchema() if it's actually a Zod schema
  3. Use the schema directly if it's already a JSON schema (like MCP tools)

Code Changes:

// Before
parameters: zodToJsonSchema(tool.schema),

// After  
const parameters = isZodSchema(tool.schema) 
  ? zodToJsonSchema(tool.schema)
  : tool.schema;

Testing

  • ✅ Added unit test covering both Zod schema tools and JSON schema tools
  • ✅ Verified existing unit tests still pass
  • ✅ Test specifically covers DynamicStructuredTool with JSON schema (simulating MCP tools)
  • ✅ Integration tests pass (where API keys are available)

Compatibility

This change maintains backward compatibility with existing Zod-based tools while adding support for JSON schema-based tools from MCP adapters.

Affected Packages:

  • @langchain/mistralai (primary fix)
  • @langchain/mcp-adapters (compatibility restored)

Related Issues

This resolves the compatibility issue reported between @langchain/mistralai and @langchain/mcp-adapters when using tools in agent workflows with the createReactAgent function.

@dosubot dosubot bot added the size:M This PR changes 30-99 lines, ignoring generated files. label May 29, 2025
Copy link

vercel bot commented May 29, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
langchainjs-docs ✅ Ready (Inspect) Visit Preview May 29, 2025 0:42am
1 Skipped Deployment
Name Status Preview Comments Updated (UTC)
langchainjs-api-refs ⬜️ Ignored (Inspect) May 29, 2025 0:42am

@dosubot dosubot bot added the auto:bug Related to a bug, vulnerability, unexpected error with an existing feature label May 29, 2025
The _convertToolToMistralTool function was attempting to convert all tool
schemas using zodToJsonSchema(), but MCP tools already provide JSON schemas.
This caused a "Cannot read properties of undefined (reading 'typeName')"
error when using MCP tools with ChatMistralAI.

Now checks if schema is a Zod schema before conversion, allowing both
Zod schemas (from standard LangChain tools) and JSON schemas (from MCP tools)
to work correctly.

Fixes compatibility issue between @langchain/mistralai and @langchain/mcp-adapters.
@christian-bromann
Copy link
Author

Seems like there is a similar fix for it in #8257 so feel free to close this one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
auto:bug Related to a bug, vulnerability, unexpected error with an existing feature size:M This PR changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement JSON Schema support for XAI (Grok) provider.
2 participants