Skip to content

refactor(mcp): address async warnings #422

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
31 changes: 18 additions & 13 deletions src/strands/tools/mcp/mcp_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def stop(
async def _set_close_event() -> None:
self._close_event.set()

self._invoke_on_background_thread(_set_close_event()).result()
self._invoke_on_background_thread_lazy(lambda: _set_close_event()).result()
self._log_debug_with_thread("waiting for background thread to join")
if self._background_thread is not None:
self._background_thread.join()
Expand All @@ -153,10 +153,9 @@ def list_tools_sync(self) -> List[MCPAgentTool]:
if not self._is_session_active():
raise MCPClientInitializationError(CLIENT_SESSION_NOT_RUNNING_ERROR_MESSAGE)

async def _list_tools_async() -> ListToolsResult:
return await self._background_thread_session.list_tools()

list_tools_response: ListToolsResult = self._invoke_on_background_thread(_list_tools_async()).result()
list_tools_response: ListToolsResult = self._invoke_on_background_thread_lazy(
lambda: self._background_thread_session.list_tools()
).result()
self._log_debug_with_thread("received %d tools from MCP server", len(list_tools_response.tools))

mcp_tools = [MCPAgentTool(tool, self) for tool in list_tools_response.tools]
Expand Down Expand Up @@ -188,11 +187,10 @@ def call_tool_sync(
if not self._is_session_active():
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)

try:
call_tool_result: MCPCallToolResult = self._invoke_on_background_thread(_call_tool_async()).result()
call_tool_result: MCPCallToolResult = self._invoke_on_background_thread_lazy(
lambda: self._background_thread_session.call_tool(name, arguments, read_timeout_seconds)
).result()
return self._handle_tool_result(tool_use_id, call_tool_result)
except Exception as e:
logger.exception("tool execution failed")
Expand Down Expand Up @@ -223,11 +221,10 @@ async def call_tool_async(
if not self._is_session_active():
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)

try:
future = self._invoke_on_background_thread(_call_tool_async())
future = self._invoke_on_background_thread_lazy(
lambda: self._background_thread_session.call_tool(name, arguments, read_timeout_seconds)
)
call_tool_result: MCPCallToolResult = await asyncio.wrap_future(future)
return self._handle_tool_result(tool_use_id, call_tool_result)
except Exception as e:
Expand Down Expand Up @@ -344,5 +341,13 @@ def _invoke_on_background_thread(self, coro: Coroutine[Any, Any, T]) -> futures.
raise MCPClientInitializationError("the client session was not initialized")
return asyncio.run_coroutine_threadsafe(coro=coro, loop=self._background_thread_event_loop)

def _invoke_on_background_thread_lazy(
self, coro_factory: Callable[[], Coroutine[Any, Any, T]]
) -> futures.Future[T]:
if self._background_thread_session is None or self._background_thread_event_loop is None:
raise MCPClientInitializationError("the client session was not initialized")
coro = coro_factory()
return asyncio.run_coroutine_threadsafe(coro=coro, loop=self._background_thread_event_loop)

def _is_session_active(self) -> bool:
return self._background_thread is not None and self._background_thread.is_alive()
Loading