1616from concurrent import futures
1717from datetime import timedelta
1818from types import TracebackType
19- from typing import Any , Callable , Coroutine , Dict , Optional , TypeVar , Union
19+ from typing import Any , Callable , Coroutine , Dict , Optional , TypeVar , Union , cast
2020
2121from mcp import ClientSession , ListToolsResult
2222from mcp .types import CallToolResult as MCPCallToolResult
@@ -157,7 +157,7 @@ def stop(
157157 async def _set_close_event () -> None :
158158 self ._close_event .set ()
159159
160- asyncio . run_coroutine_threadsafe (_set_close_event (), self . _background_thread_event_loop )
160+ self . _invoke_on_background_thread (_set_close_event ()). result ( )
161161
162162 self ._log_debug_with_thread ("waiting for background thread to join" )
163163 self ._background_thread .join ()
@@ -183,8 +183,7 @@ def list_tools_sync(self, pagination_token: Optional[str] = None) -> PaginatedLi
183183 raise MCPClientInitializationError (CLIENT_SESSION_NOT_RUNNING_ERROR_MESSAGE )
184184
185185 async def _list_tools_async () -> ListToolsResult :
186- assert self ._background_thread_session is not None
187- return await self ._background_thread_session .list_tools (cursor = pagination_token )
186+ return await cast (ClientSession , self ._background_thread_session ).list_tools (cursor = pagination_token )
188187
189188 list_tools_response : ListToolsResult = self ._invoke_on_background_thread (_list_tools_async ()).result ()
190189 self ._log_debug_with_thread ("received %d tools from MCP server" , len (list_tools_response .tools ))
@@ -210,8 +209,7 @@ def list_prompts_sync(self, pagination_token: Optional[str] = None) -> ListPromp
210209 raise MCPClientInitializationError (CLIENT_SESSION_NOT_RUNNING_ERROR_MESSAGE )
211210
212211 async def _list_prompts_async () -> ListPromptsResult :
213- assert self ._background_thread_session is not None
214- return await self ._background_thread_session .list_prompts (cursor = pagination_token )
212+ return await cast (ClientSession , self ._background_thread_session ).list_prompts (cursor = pagination_token )
215213
216214 list_prompts_result : ListPromptsResult = self ._invoke_on_background_thread (_list_prompts_async ()).result ()
217215 self ._log_debug_with_thread ("received %d prompts from MCP server" , len (list_prompts_result .prompts ))
@@ -235,8 +233,7 @@ def get_prompt_sync(self, prompt_id: str, args: dict[str, Any]) -> GetPromptResu
235233 raise MCPClientInitializationError (CLIENT_SESSION_NOT_RUNNING_ERROR_MESSAGE )
236234
237235 async def _get_prompt_async () -> GetPromptResult :
238- assert self ._background_thread_session is not None
239- return await self ._background_thread_session .get_prompt (prompt_id , arguments = args )
236+ return await cast (ClientSession , self ._background_thread_session ).get_prompt (prompt_id , arguments = args )
240237
241238 get_prompt_result : GetPromptResult = self ._invoke_on_background_thread (_get_prompt_async ()).result ()
242239 self ._log_debug_with_thread ("received prompt from MCP server" )
@@ -271,8 +268,9 @@ def call_tool_sync(
271268 raise MCPClientInitializationError (CLIENT_SESSION_NOT_RUNNING_ERROR_MESSAGE )
272269
273270 async def _call_tool_async () -> MCPCallToolResult :
274- assert self ._background_thread_session is not None
275- return await self ._background_thread_session .call_tool (name , arguments , read_timeout_seconds )
271+ return await cast (ClientSession , self ._background_thread_session ).call_tool (
272+ name , arguments , read_timeout_seconds
273+ )
276274
277275 try :
278276 call_tool_result : MCPCallToolResult = self ._invoke_on_background_thread (_call_tool_async ()).result ()
@@ -307,8 +305,9 @@ async def call_tool_async(
307305 raise MCPClientInitializationError (CLIENT_SESSION_NOT_RUNNING_ERROR_MESSAGE )
308306
309307 async def _call_tool_async () -> MCPCallToolResult :
310- assert self ._background_thread_session is not None
311- return await self ._background_thread_session .call_tool (name , arguments , read_timeout_seconds )
308+ return await cast (ClientSession , self ._background_thread_session ).call_tool (
309+ name , arguments , read_timeout_seconds
310+ )
312311
313312 try :
314313 future = self ._invoke_on_background_thread (_call_tool_async ())
0 commit comments