MCPcopy
hub / github.com/modelcontextprotocol/python-sdk / __aenter__

Method __aenter__

src/mcp/client/client.py:242–264  ·  view source on GitHub ↗

Enter the async context manager.

(self)

Source from the content-addressed store, hash-verified

240 )
241
242 async def __aenter__(self) -> Client:
243 """Enter the async context manager."""
244 if self._entered:
245 raise RuntimeError("Client is already entered; cannot reenter")
246 self._entered = True
247
248 async with AsyncExitStack() as exit_stack:
249 session = await self._build_session(exit_stack)
250 session = await exit_stack.enter_async_context(session)
251
252 if self.mode == "legacy":
253 await session.initialize()
254 elif self.mode == "auto":
255 await negotiate_auto(session)
256 else:
257 session.adopt(self.prior_discover or _synthesize_discover(self.mode))
258
259 # Only publish the session after the handshake succeeds, so `_session is not None`
260 # implies the protocol_version/server_info/server_capabilities are populated. If the
261 # handshake raised above, the local exit_stack unwinds the transport for us.
262 self._session = session
263 self._exit_stack = exit_stack.pop_all()
264 return self
265
266 async def __aexit__(self, exc_type: type[BaseException] | None, exc_val: BaseException | None, exc_tb: Any) -> None:
267 """Exit the async context manager."""

Callers

nothing calls this directly

Calls 5

_build_sessionMethod · 0.95
negotiate_autoFunction · 0.90
_synthesize_discoverFunction · 0.85
initializeMethod · 0.45
adoptMethod · 0.45

Tested by

no test coverage detected