From bc44e93d2d4d42f6dcba03f9b4b256240f2e9bd0 Mon Sep 17 00:00:00 2001 From: Joao Ferro Date: Thu, 2 Oct 2025 15:37:31 +0100 Subject: [PATCH] Fix(tools): Ensure tool schema enum compatibility for Google API Patches the input schema of MCP tools during conversion to LangChain tools. This change explicitly adds type: "string" to any schema property that defines an enum but lacks a type. This resolves a google.api_core.exceptions.InvalidArgument error raised by the Google GenAI API, which requires enums to have a declared type. --- langchain_mcp_adapters/tools.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/langchain_mcp_adapters/tools.py b/langchain_mcp_adapters/tools.py index a8bf0eb..a0bcea7 100644 --- a/langchain_mcp_adapters/tools.py +++ b/langchain_mcp_adapters/tools.py @@ -3,7 +3,7 @@ This module provides functionality to convert MCP tools into LangChain-compatible tools, handle tool execution, and manage tool conversion between the two formats. """ - +from copy import deepcopy from typing import Any, cast, get_args from langchain_core.tools import ( @@ -150,6 +150,17 @@ def convert_mcp_tool_to_langchain_tool( msg = "Either a session or a connection config must be provided" raise ValueError(msg) + input_schema = None + if isinstance(tool.inputSchema, dict): + input_schema = deepcopy(tool.inputSchema) + if "properties" in input_schema: + for _, prop_schema in input_schema.get("properties", {}).items(): + if "enum" in prop_schema and "type" not in prop_schema: + prop_schema["type"] = "string" + else: + input_schema = tool.inputSchema + + async def call_tool( **arguments: dict[str, Any], ) -> tuple[str | list[str], list[NonTextContent] | None]: @@ -255,7 +266,7 @@ async def call_tool( return StructuredTool( name=tool.name, description=tool.description or "", - args_schema=tool.inputSchema, + args_schema=input_schema, coroutine=call_tool, response_format="content_and_artifact", metadata=metadata,