fix(mistralai): handle both Zod and JSON schemas in tool conversion #8269
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
Fixes a bug where using MCP (Model Context Protocol) tools with
ChatMistralAI
would throw aTypeError: Cannot read properties of undefined (reading 'typeName')
error.fixes #8171
Problem
The
_convertToolToMistralTool
function was assuming all tool schemas were Zod schemas and unconditionally callingzodToJsonSchema()
on them. However, MCP tools from@langchain/mcp-adapters
provide tools with JSON schemas already, not Zod schemas. This mismatch caused thezodToJsonSchema
function to fail when trying to access thetypeName
property on a plain JSON schema object.Error Stack Trace:
Solution
Modified the
_convertToolToMistralTool
function to:isZodSchema
helper functionzodToJsonSchema()
if it's actually a Zod schemaCode Changes:
Testing
DynamicStructuredTool
with JSON schema (simulating MCP tools)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 thecreateReactAgent
function.