Skip to content

Commit e60975b

Browse files
authored
mcp[patch]: raise a runtime error if the tool call is not available (#318)
mcp-sdk context manager is swallowing some classes of exceptions with no way to recover the actual error the occurred. This patch makes a slight improvement to the exception an end user sees (so it's not an unbound variable). Unfortunately, we don't have any information about the root cause (we don't even know whether a timeout can cause this.) I'll try to recreate this issue: #314 in unit tests to see if we can trigger it with timeouts.
1 parent bb26197 commit e60975b

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

langchain_mcp_adapters/tools.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ def convert_mcp_tool_to_langchain_tool(
124124
async def call_tool(
125125
**arguments: dict[str, Any],
126126
) -> tuple[str | list[str], list[NonTextContent] | None]:
127+
call_tool_result = None
127128
if session is None:
128129
# If a session is not provided, we will create one on the fly
129130
async with create_session(connection) as tool_session:
@@ -134,6 +135,15 @@ async def call_tool(
134135
)
135136
else:
136137
call_tool_result = await session.call_tool(tool.name, arguments)
138+
139+
if call_tool_result is None:
140+
raise RuntimeError(
141+
"Tool call failed: no result returned from the underlying MCP SDK. "
142+
"This may indicate that an exception was handled or suppressed "
143+
"by the MCP SDK (e.g., client disconnection, network issue, "
144+
"or other execution error)."
145+
)
146+
137147
return _convert_call_tool_result(call_tool_result)
138148

139149
meta = tool.meta if hasattr(tool, "meta") else None

0 commit comments

Comments
 (0)