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 (
2526 TextContent ,
2627)
2728from mcp .types import Tool as MCPTool
28- from pydantic import BaseModel , create_model
29+ from pydantic import BaseModel , ValidationError , create_model
2930
3031from langchain_mcp_adapters .callbacks import CallbackContext , Callbacks , _MCPCallbacks
3132from langchain_mcp_adapters .hooks import CallToolRequestSpec , Hooks , ToolHookContext
@@ -128,6 +129,10 @@ def convert_mcp_tool_to_langchain_tool(
128129 callbacks : Callbacks | None = None ,
129130 hooks : Hooks | None = None ,
130131 server_name : str | None = None ,
132+ handle_tool_error : bool | str | Callable [[ToolException ], str ] | None = False ,
133+ handle_validation_error : (
134+ bool | str | Callable [[ValidationError ], str ] | None
135+ ) = False ,
131136) -> BaseTool :
132137 """Convert an MCP tool to a LangChain tool.
133138
@@ -141,6 +146,8 @@ def convert_mcp_tool_to_langchain_tool(
141146 callbacks: Optional callbacks for handling notifications and events
142147 hooks: Optional hooks for before/after tool call processing
143148 server_name: Name of the server this tool belongs to
149+ handle_tool_error: Optional error handler for tool execution errors.
150+ handle_validation_error: Optional error handler for validation errors.
144151
145152 Returns:
146153 a LangChain tool
@@ -259,6 +266,8 @@ async def call_tool(
259266 coroutine = call_tool ,
260267 response_format = "content_and_artifact" ,
261268 metadata = metadata ,
269+ handle_tool_error = handle_tool_error ,
270+ handle_validation_error = handle_validation_error ,
262271 )
263272
264273
@@ -269,6 +278,10 @@ async def load_mcp_tools(
269278 callbacks : Callbacks | None = None ,
270279 hooks : Hooks | None = None ,
271280 server_name : str | None = None ,
281+ handle_tool_error : bool | str | Callable [[ToolException ], str ] | None = False ,
282+ handle_validation_error : (
283+ bool | str | Callable [[ValidationError ], str ] | None
284+ ) = False ,
272285) -> list [BaseTool ]:
273286 """Load all available MCP tools and convert them to LangChain tools.
274287
@@ -278,6 +291,8 @@ async def load_mcp_tools(
278291 callbacks: Optional callbacks for handling notifications and events.
279292 hooks: Optional hooks for before/after tool call processing.
280293 server_name: Name of the server these tools belong to.
294+ handle_tool_error: Optional error handler for tool execution errors.
295+ handle_validation_error: Optional error handler for validation errors.
281296
282297 Returns:
283298 List of LangChain tools. Tool annotations are returned as part
@@ -317,6 +332,8 @@ async def load_mcp_tools(
317332 callbacks = callbacks ,
318333 hooks = hooks ,
319334 server_name = server_name ,
335+ handle_tool_error = handle_tool_error ,
336+ handle_validation_error = handle_validation_error ,
320337 )
321338 for tool in tools
322339 ]
0 commit comments