8000 chore: enhance error messaging when MCP tools are used without sessio… by dbschmigelski · Pull Request #175 · strands-agents/sdk-python · GitHub
[go: up one dir, main page]

Skip to content

chore: enhance error messaging when MCP tools are used without sessio… #175

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/strands/tools/mcp/mcp_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@
"image/webp": "webp",
}

CLIENT_SESSION_NOT_RUNNING_ERROR_MESSAGE = (
"the client session is not running. Ensure the agent is used within "
"the MCP client context manager. For more information see: "
"https://strandsagents.com/latest/user-guide/concepts/tools/mcp-tools/#mcpclientinitializationerror"
)


class MCPClient:
"""Represents a connection to a Model Context Protocol (MCP) server.
Expand Down Expand Up @@ -145,7 +151,7 @@ def list_tools_sync(self) -> List[MCPAgentTool]:
"""
self._log_debug_with_thread("listing MCP tools synchronously")
if not self._is_session_active():
raise MCPClientInitializationError("the client session is not running")
raise MCPClientInitializationError(CLIENT_SESSION_NOT_RUNNING_ERROR_MESSAGE)

async def _list_tools_async() -> ListToolsResult:
return await self._background_thread_session.list_tools()
Expand Down Expand Up @@ -180,7 +186,7 @@ def call_tool_sync(
"""
self._log_debug_with_thread("calling MCP tool '%s' synchronously with tool_use_id=%s", name, tool_use_id)
if not self._is_session_active():
raise MCPClientInitializationError("the client session is not running")
raise MCPClientInitializationError(CLIENT_SESSION_NOT_RUNNING_ERROR_MESSAGE)

async def _call_tool_async() -> MCPCallToolResult:
return await self._background_thread_session.call_tool(name, arguments, read_timeout_seconds)
Expand Down
0