fix: optional mcp tools#171
Merged
Merged
Conversation
98ca4d5 to
d212207
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 in
mcp_tool_to_langchain(agentgateway/converters.py) where all MCP tool parameters were unconditionally marked as required in the generated Pydanticargs_schema, regardless of what the tool'sinput_schemadeclared as optional.Root cause: The field mapping
{k: (str, ...) for k in properties}used the Pydantic sentinel...(required) for every field. Fields not listed in the JSON Schema"required"array were never treated as optional.Fix: The converter now reads the
"required"array frominput_schemaand maps fields accordingly — fields in"required"remain(str, ...), while all other fields become(str | None, None)(optional, defaulting toNone).This means LangChain agents and any Pydantic-aware consumer using
mcp_tool_to_langchainwill no longer raise validation errors when optional MCP tool parameters are omitted.Related Issue
Closes #
Type of Change
How to Test
MCPToolwhoseinput_schemahas both required and optional properties:langchain_tool = mcp_tool_to_langchain(tool, call_tool, get_user_token)langchain_tool.args_schema.model_fields:eventidmust be required (is_required() == True)datafetchmodemust be optional (is_required() == False)pytest tests/agentgateway/unit/test_converters.py -vChecklist
Additional Notes
The fix is confined to the
mcp_tool_to_langchainreference converter. The coreAgentGatewayClient.call_mcp_tool()is unaffected — it still passes**kwargsthrough to the MCP server without schema validation.