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
@@ -143,19 +150,27 @@ async def call_tool(
143150 coroutine = call_tool ,
144151 response_format = "content_and_artifact" ,
145152 metadata = tool .annotations .model_dump () if tool .annotations else None ,
153+ handle_tool_error = handle_tool_error ,
154+ handle_validation_error = handle_validation_error ,
146155 )
147156
148157
149158async def load_mcp_tools (
150159 session : ClientSession | None ,
151160 * ,
152161 connection : Connection | None = None ,
162+ handle_tool_error : bool | str | Callable [[ToolException ], str ] | None = False ,
163+ handle_validation_error : (
164+ bool | str | Callable [[ValidationError ], str ] | None
165+ ) = False ,
153166) -> list [BaseTool ]:
154167 """Load all available MCP tools and convert them to LangChain tools.
155168
156169 Args:
157170 session: The MCP client session. If None, connection must be provided.
158171 connection: Connection config to create a new session if session is None.
172+ handle_tool_error: Optional error handler for tool execution errors.
173+ handle_validation_error: Optional error handler for validation errors.
159174
160175 Returns:
161176 List of LangChain tools. Tool annotations are returned as part
@@ -177,7 +192,13 @@ async def load_mcp_tools(
177192 tools = await _list_all_tools (session )
178193
179194 return [
180- convert_mcp_tool_to_langchain_tool (session , tool , connection = connection )
195+ convert_mcp_tool_to_langchain_tool (
196+ session ,
197+ tool ,
198+ connection = connection ,
199+ handle_tool_error = handle_tool_error ,
200+ handle_validation_error = handle_validation_error ,
201+ )
181202 for tool in tools
182203 ]
183204
0 commit comments