Skip to content

Commit 61a1d9b

Browse files
chore: enhance error messaging when MCP tools are used without session initialization
1 parent ffc7c5e commit 61a1d9b

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

src/strands/tools/mcp/mcp_client.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@
4141
"image/webp": "webp",
4242
}
4343

44+
CLIENT_SESSION_NOT_RUNNING_ERROR_MESSAGE = (
45+
"the client session is not running. Please ensure the agent is used within "
46+
"the MCP client context manager ('with mcp_client:')"
47+
)
48+
4449

4550
class MCPClient:
4651
"""Represents a connection to a Model Context Protocol (MCP) server.
@@ -145,7 +150,7 @@ def list_tools_sync(self) -> List[MCPAgentTool]:
145150
"""
146151
self._log_debug_with_thread("listing MCP tools synchronously")
147152
if not self._is_session_active():
148-
raise MCPClientInitializationError("the client session is not running")
153+
raise MCPClientInitializationError(CLIENT_SESSION_NOT_RUNNING_ERROR_MESSAGE)
149154

150155
async def _list_tools_async() -> ListToolsResult:
151156
return await self._background_thread_session.list_tools()
@@ -180,7 +185,7 @@ def call_tool_sync(
180185
"""
181186
self._log_debug_with_thread("calling MCP tool '%s' synchronously with tool_use_id=%s", name, tool_use_id)
182187
if not self._is_session_active():
183-
raise MCPClientInitializationError("the client session is not running")
188+
raise MCPClientInitializationError(CLIENT_SESSION_NOT_RUNNING_ERROR_MESSAGE)
184189

185190
async def _call_tool_async() -> MCPCallToolResult:
186191
return await self._background_thread_session.call_tool(name, arguments, read_timeout_seconds)

0 commit comments

Comments
 (0)