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
The session hierarchy has been refactored to support pluggable transport implementations. This introduces several breaking changes:
477
+
478
+
#### `ClientRequestContext` type changed
479
+
480
+
`ClientRequestContext` is now `RequestContext[BaseClientSession]` instead of `RequestContext[ClientSession]`. This means callbacks receive the more general `BaseClientSession` type, which may not have all methods available on `ClientSession`.
481
+
482
+
**Before:**
483
+
484
+
```python
485
+
from mcp.client.context import ClientRequestContext
In `mcp.shared._context` and `mcp.shared.progress`, the `SessionT` TypeVar has been renamed to `SessionT_co` to follow naming conventions for covariant type variables.
540
+
541
+
**Before:**
542
+
543
+
```python
544
+
from mcp.shared._context import SessionT
545
+
```
546
+
547
+
**After:**
548
+
549
+
```python
550
+
from mcp.shared._context import SessionT_co
551
+
```
552
+
553
+
#### New `AbstractBaseSession` structural interface
554
+
555
+
The session hierarchy now uses a new **runtime-checkable Protocol** called `AbstractBaseSession` to define the shared contract for all MCP sessions. This protocol enables structural subtyping, allowing different transport implementations to be used interchangeably without requiring rigid inheritance.
556
+
557
+
Key characteristics of `AbstractBaseSession`:
558
+
1.**Pure Interface**: It is a structural protocol with no implementation state or `__init__` method.
559
+
2.**Simplified Type Parameters**: It takes two parameters: `AbstractBaseSession[SendRequestT, SendNotificationT]`. Contravariant variance is used for these parameters to ensure that sessions can be used safely in generic contexts (like `RequestContext`).
560
+
3.**BaseSession Implementation**: The concrete implementation logic (state management, response routing) is provided by the `BaseSession` class, which satisfies the protocol.
561
+
562
+
**Before:**
563
+
564
+
```python
565
+
from mcp.shared.session import AbstractBaseSession
`BaseClientSession` is now a `typing.Protocol` (structural subtyping) instead of an abstract base class. It no longer inherits from `AbstractBaseSession` and requires no inheritance to satisfy.
636
+
637
+
**Before:**
638
+
639
+
```python
640
+
from mcp.client.base_client_session import BaseClientSession
641
+
642
+
classMyClientSession(BaseClientSession):
643
+
asyncdefinitialize(self) -> InitializeResult:
644
+
...
645
+
```
646
+
647
+
**After:**
648
+
649
+
```python
650
+
from mcp.client.base_client_session import BaseClientSession
651
+
652
+
classMyClientSession:
653
+
# Just implement the methods - no inheritance needed
0 commit comments