3737 TextResourceContents ,
3838)
3939from mcp .types import Tool as MCPTool
40- from pydantic import BaseModel , create_model
40+ from pydantic import BaseModel , ValidationError , create_model
4141
4242from langchain_mcp_adapters .callbacks import CallbackContext , Callbacks , _MCPCallbacks
4343from langchain_mcp_adapters .interceptors import (
@@ -278,6 +278,10 @@ def convert_mcp_tool_to_langchain_tool(
278278 tool_interceptors : list [ToolCallInterceptor ] | None = None ,
279279 server_name : str | None = None ,
280280 tool_name_prefix : bool = False ,
281+ handle_tool_error : bool | str | Callable [[ToolException ], str ] | None = False ,
282+ handle_validation_error : (
283+ bool | str | Callable [[ValidationError ], str ] | None
284+ ) = False ,
281285) -> BaseTool :
282286 """Convert an MCP tool to a LangChain tool.
283287
@@ -293,6 +297,8 @@ def convert_mcp_tool_to_langchain_tool(
293297 server_name: Name of the server this tool belongs to
294298 tool_name_prefix: If `True` and `server_name` is provided, the tool name will be
295299 prefixed w/ server name (e.g., `"weather_search"` instead of `"search"`)
300+ handle_tool_error: Optional error handler for tool execution errors.
301+ handle_validation_error: Optional error handler for validation errors.
296302
297303 Returns:
298304 a LangChain tool
@@ -430,6 +436,8 @@ async def execute_tool(request: MCPToolCallRequest) -> MCPToolCallResult:
430436 coroutine = call_tool ,
431437 response_format = "content_and_artifact" ,
432438 metadata = metadata ,
439+ handle_tool_error = handle_tool_error ,
440+ handle_validation_error = handle_validation_error ,
433441 )
434442
435443
@@ -441,6 +449,10 @@ async def load_mcp_tools(
441449 tool_interceptors : list [ToolCallInterceptor ] | None = None ,
442450 server_name : str | None = None ,
443451 tool_name_prefix : bool = False ,
452+ handle_tool_error : bool | str | Callable [[ToolException ], str ] | None = False ,
453+ handle_validation_error : (
454+ bool | str | Callable [[ValidationError ], str ] | None
455+ ) = False ,
444456) -> list [BaseTool ]:
445457 """Load all available MCP tools and convert them to LangChain [tools](https://docs.langchain.com/oss/python/langchain/tools).
446458
@@ -452,6 +464,8 @@ async def load_mcp_tools(
452464 server_name: Name of the server these tools belong to.
453465 tool_name_prefix: If `True` and `server_name` is provided, tool names will be
454466 prefixed w/ server name (e.g., `"weather_search"` instead of `"search"`).
467+ handle_tool_error: Optional error handler for tool execution errors.
468+ handle_validation_error: Optional error handler for validation errors.
455469
456470 Returns:
457471 List of LangChain [tools](https://docs.langchain.com/oss/python/langchain/tools).
@@ -492,6 +506,8 @@ async def load_mcp_tools(
492506 tool_interceptors = tool_interceptors ,
493507 server_name = server_name ,
494508 tool_name_prefix = tool_name_prefix ,
509+ handle_tool_error = handle_tool_error ,
510+ handle_validation_error = handle_validation_error ,
495511 )
496512 for tool in tools
497513 ]
0 commit comments