2525 TextContent ,
2626)
2727from mcp .types import Tool as MCPTool
28- from pydantic import BaseModel , create_model
28+ from pydantic import BaseModel , ValidationError , create_model
2929
3030from langchain_mcp_adapters .callbacks import CallbackContext , Callbacks , _MCPCallbacks
3131from langchain_mcp_adapters .interceptors import (
@@ -161,6 +161,10 @@ def convert_mcp_tool_to_langchain_tool(
161161 callbacks : Callbacks | None = None ,
162162 tool_interceptors : list [ToolCallInterceptor ] | None = None ,
163163 server_name : str | None = None ,
164+ handle_tool_error : bool | str | Callable [[ToolException ], str ] | None = False ,
165+ handle_validation_error : (
166+ bool | str | Callable [[ValidationError ], str ] | None
167+ ) = False ,
164168) -> BaseTool :
165169 """Convert an MCP tool to a LangChain tool.
166170
@@ -174,6 +178,8 @@ def convert_mcp_tool_to_langchain_tool(
174178 callbacks: Optional callbacks for handling notifications and events
175179 tool_interceptors: Optional list of interceptors for tool call processing
176180 server_name: Name of the server this tool belongs to
181+ handle_tool_error: Optional error handler for tool execution errors.
182+ handle_validation_error: Optional error handler for validation errors.
177183
178184 Returns:
179185 a LangChain tool
@@ -303,6 +309,8 @@ async def execute_tool(request: MCPToolCallRequest) -> MCPToolCallResult:
303309 coroutine = call_tool ,
304310 response_format = "content_and_artifact" ,
305311 metadata = metadata ,
312+ handle_tool_error = handle_tool_error ,
313+ handle_validation_error = handle_validation_error ,
306314 )
307315
308316
@@ -313,6 +321,10 @@ async def load_mcp_tools(
313321 callbacks : Callbacks | None = None ,
314322 tool_interceptors : list [ToolCallInterceptor ] | None = None ,
315323 server_name : str | None = None ,
324+ handle_tool_error : bool | str | Callable [[ToolException ], str ] | None = False ,
325+ handle_validation_error : (
326+ bool | str | Callable [[ValidationError ], str ] | None
327+ ) = False ,
316328) -> list [BaseTool ]:
317329 """Load all available MCP tools and convert them to LangChain [tools](https://docs.langchain.com/oss/python/langchain/tools).
318330
@@ -322,6 +334,8 @@ async def load_mcp_tools(
322334 callbacks: Optional `Callbacks` for handling notifications and events.
323335 tool_interceptors: Optional list of interceptors for tool call processing.
324336 server_name: Name of the server these tools belong to.
337+ handle_tool_error: Optional error handler for tool execution errors.
338+ handle_validation_error: Optional error handler for validation errors.
325339
326340 Returns:
327341 List of LangChain [tools](https://docs.langchain.com/oss/python/langchain/tools).
@@ -361,6 +375,8 @@ async def load_mcp_tools(
361375 callbacks = callbacks ,
362376 tool_interceptors = tool_interceptors ,
363377 server_name = server_name ,
378+ handle_tool_error = handle_tool_error ,
379+ handle_validation_error = handle_validation_error ,
364380 )
365381 for tool in tools
366382 ]
0 commit comments