44tools, handle tool execution, and manage tool conversion between the two formats.
55"""
66
7+ from collections .abc import Callable
78from typing import Any , cast , get_args
89
910from langchain_core .tools import (
1819from mcp .server .fastmcp .utilities .func_metadata import ArgModelBase , FuncMetadata
1920from mcp .types import CallToolResult , EmbeddedResource , ImageContent , TextContent
2021from mcp .types import Tool as MCPTool
21- from pydantic import BaseModel , create_model
22+ from pydantic import BaseModel , ValidationError , create_model
2223
2324from langchain_mcp_adapters .sessions import Connection , create_session
2425
@@ -102,6 +103,10 @@ def convert_mcp_tool_to_langchain_tool(
102103 tool : MCPTool ,
103104 * ,
104105 connection : Connection | None = None ,
106+ handle_tool_error : bool | str | Callable [[ToolException ], str ] | None = False ,
107+ handle_validation_error : (
108+ bool | str | Callable [[ValidationError ], str ] | None
109+ ) = False ,
105110) -> BaseTool :
106111 """Convert an MCP tool to a LangChain tool.
107112
@@ -112,6 +117,8 @@ def convert_mcp_tool_to_langchain_tool(
112117 tool: MCP tool to convert
113118 connection: Optional connection config to use to create a new session
114119 if a `session` is not provided
120+ handle_tool_error: Optional error handler for tool execution errors.
121+ handle_validation_error: Optional error handler for validation errors.
115122
116123 Returns:
117124 a LangChain tool
@@ -158,19 +165,27 @@ async def call_tool(
158165 coroutine = call_tool ,
159166 response_format = "content_and_artifact" ,
160167 metadata = metadata ,
168+ handle_tool_error = handle_tool_error ,
169+ handle_validation_error = handle_validation_error ,
161170 )
162171
163172
164173async def load_mcp_tools (
165174 session : ClientSession | None ,
166175 * ,
167176 connection : Connection | None = None ,
177+ handle_tool_error : bool | str | Callable [[ToolException ], str ] | None = False ,
178+ handle_validation_error : (
179+ bool | str | Callable [[ValidationError ], str ] | None
180+ ) = False ,
168181) -> list [BaseTool ]:
169182 """Load all available MCP tools and convert them to LangChain tools.
170183
171184 Args:
172185 session: The MCP client session. If None, connection must be provided.
173186 connection: Connection config to create a new session if session is None.
187+ handle_tool_error: Optional error handler for tool execution errors.
188+ handle_validation_error: Optional error handler for validation errors.
174189
175190 Returns:
176191 List of LangChain tools. Tool annotations are returned as part
@@ -192,7 +207,13 @@ async def load_mcp_tools(
192207 tools = await _list_all_tools (session )
193208
194209 return [
195- convert_mcp_tool_to_langchain_tool (session , tool , connection = connection )
210+ convert_mcp_tool_to_langchain_tool (
211+ session ,
212+ tool ,
213+ connection = connection ,
214+ handle_tool_error = handle_tool_error ,
215+ handle_validation_error = handle_validation_error ,
216+ )
196217 for tool in tools
197218 ]
198219
0 commit comments