4
4
tools, handle tool execution, and manage tool conversion between the two formats.
5
5
"""
6
6
7
+ from collections .abc import Callable
7
8
from typing import Any , cast , get_args
8
9
9
10
from langchain_core .tools import (
18
19
from mcp .server .fastmcp .utilities .func_metadata import ArgModelBase , FuncMetadata
19
20
from mcp .types import CallToolResult , EmbeddedResource , ImageContent , TextContent
20
21
from mcp .types import Tool as MCPTool
21
- from pydantic import BaseModel , create_model
22
+ from pydantic import BaseModel , ValidationError , create_model
22
23
23
24
from langchain_mcp_adapters .sessions import Connection , create_session
24
25
@@ -102,6 +103,10 @@ def convert_mcp_tool_to_langchain_tool(
102
103
tool : MCPTool ,
103
104
* ,
104
105
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 ,
105
110
) -> BaseTool :
106
111
"""Convert an MCP tool to a LangChain tool.
107
112
@@ -112,6 +117,8 @@ def convert_mcp_tool_to_langchain_tool(
112
117
tool: MCP tool to convert
113
118
connection: Optional connection config to use to create a new session
114
119
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.
115
122
116
123
Returns:
117
124
a LangChain tool
@@ -148,19 +155,27 @@ async def call_tool(
148
155
coroutine = call_tool ,
149
156
response_format = "content_and_artifact" ,
150
157
metadata = metadata ,
158
+ handle_tool_error = handle_tool_error ,
159
+ handle_validation_error = handle_validation_error ,
151
160
)
152
161
153
162
154
163
async def load_mcp_tools (
155
164
session : ClientSession | None ,
156
165
* ,
157
166
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 ,
158
171
) -> list [BaseTool ]:
159
172
"""Load all available MCP tools and convert them to LangChain tools.
160
173
161
174
Args:
162
175
session: The MCP client session. If None, connection must be provided.
163
176
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.
164
179
165
180
Returns:
166
181
List of LangChain tools. Tool annotations are returned as part
@@ -182,7 +197,13 @@ async def load_mcp_tools(
182
197
tools = await _list_all_tools (session )
183
198
184
199
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
+ )
186
207
for tool in tools
187
208
]
188
209
0 commit comments