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
@@ -148,19 +155,27 @@ async def call_tool(
148155 coroutine = call_tool ,
149156 response_format = "content_and_artifact" ,
150157 metadata = metadata ,
158+ handle_tool_error = handle_tool_error ,
159+ handle_validation_error = handle_validation_error ,
151160 )
152161
153162
154163async def load_mcp_tools (
155164 session : ClientSession | None ,
156165 * ,
157166 connection : Connection | None = None ,
167+ handle_tool_error : bool | str | Callable [[ToolException ], str ] | None = False ,
168+ handle_validation_error : (
169+ bool | str | Callable [[ValidationError ], str ] | None
170+ ) = False ,
158171) -> list [BaseTool ]:
159172 """Load all available MCP tools and convert them to LangChain tools.
160173
161174 Args:
162175 session: The MCP client session. If None, connection must be provided.
163176 connection: Connection config to create a new session if session is None.
177+ handle_tool_error: Optional error handler for tool execution errors.
178+ handle_validation_error: Optional error handler for validation errors.
164179
165180 Returns:
166181 List of LangChain tools. Tool annotations are returned as part
@@ -182,7 +197,13 @@ async def load_mcp_tools(
182197 tools = await _list_all_tools (session )
183198
184199 return [
185- convert_mcp_tool_to_langchain_tool (session , tool , connection = connection )
200+ convert_mcp_tool_to_langchain_tool (
201+ session ,
202+ tool ,
203+ connection = connection ,
204+ handle_tool_error = handle_tool_error ,
205+ handle_validation_error = handle_validation_error ,
206+ )
186207 for tool in tools
187208 ]
188209
0 commit comments