1111import logging
1212import time
1313from contextlib import AsyncExitStack
14- from typing import Any , Dict , List , Optional , Tuple
14+ from typing import Any , Dict , List , Optional , Tuple , cast
1515
1616import httpx
1717from mcp .client .session import ClientSession
@@ -276,7 +276,10 @@ async def _get_initial_state_from_mcp_resource(self, session: MCPSession) -> Any
276276 try :
277277 # List available resources - this is where initial state should come from
278278 logger .debug (f"Session { session .session_id } : Discovering MCP resources for initial state..." )
279- resources_response = await mcp_session .list_resources ()
279+ mcp_session_local = session ._mcp_session
280+ if mcp_session_local is None :
281+ raise RuntimeError ("Session not initialized while listing resources" )
282+ resources_response = await mcp_session_local .list_resources ()
280283 resources = resources_response .resources if hasattr (resources_response , "resources" ) else []
281284 logger .debug (f"Session { session .session_id } : Found { len (resources )} MCP resources" )
282285 for resource in resources :
@@ -303,7 +306,10 @@ async def _get_initial_state_from_mcp_resource(self, session: MCPSession) -> Any
303306 f"Session { session .session_id } : Reading initial state from resource: { initial_state_resource .uri } "
304307 )
305308
306- resource_content = await mcp_session .read_resource (initial_state_resource .uri )
309+ mcp_session_for_read = session ._mcp_session
310+ if mcp_session_for_read is None :
311+ raise RuntimeError ("Session not initialized while reading resource" )
312+ resource_content = await mcp_session_for_read .read_resource (initial_state_resource .uri )
307313
308314 # Handle the new ResourceContents format
309315 text_value = getattr (resource_content , "text" , None )
@@ -348,7 +354,10 @@ async def _get_initial_state_from_mcp_resource(self, session: MCPSession) -> Any
348354 f"Session { session .session_id } : About to call mcp_session.read_resource with fallback URI: { first_resource .uri } "
349355 )
350356
351- resource_content = await mcp_session .read_resource (first_resource .uri )
357+ mcp_session_for_fallback_read = session ._mcp_session
358+ if mcp_session_for_fallback_read is None :
359+ raise RuntimeError ("Session not initialized while reading fallback resource" )
360+ resource_content = await mcp_session_for_fallback_read .read_resource (first_resource .uri )
352361
353362 logger .debug (
354363 f"Session { session .session_id } : fallback read_resource returned type: { type (resource_content )} "
0 commit comments