You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
refactor: address PR review comments for Client class
- Make InMemoryTransport private by moving to _memory.py module
- Improve exception handling in __aenter__: use AsyncExitStack.pop_all()
pattern instead of manual try/except for cleaner resource cleanup
- Add reentry check: raise RuntimeError if Client is entered twice
- Add test for reentry behavior
- Remove low-level transport documentation (InMemoryTransport is now private)
1. If you are using `trio`, you should set `"trio"` as the `anyio_backend`. Check more information in the [anyio documentation](https://anyio.readthedocs.io/en/stable/testing.html#specifying-the-backends-to-run-on).
70
70
71
71
There you go! You can now extend your tests to cover more scenarios.
72
-
73
-
## Advanced: Low-Level Transport Access
74
-
75
-
For tests that need direct access to the underlying `ClientSession`, use `InMemoryTransport`:
76
-
77
-
```python
78
-
from mcp.client.session import ClientSession
79
-
from mcp.client.transports import InMemoryTransport
80
-
81
-
from server import app
82
-
83
-
84
-
asyncdeftest_with_low_level_access():
85
-
transport = InMemoryTransport(app, raise_exceptions=True)
86
-
asyncwith transport.connect() as (read_stream, write_stream):
87
-
asyncwith ClientSession(read_stream, write_stream) as session:
88
-
await session.initialize()
89
-
# Direct access to the full ClientSession API
90
-
result =await session.call_tool("add", {"a": 1, "b": 2})
91
-
```
92
-
93
-
The `Client` class is built on top of `InMemoryTransport` and handles initialization automatically.
94
-
Use `InMemoryTransport` directly only when you need fine-grained control over the session lifecycle.
0 commit comments