Skip to content

Commit b8eff78

Browse files
committed
resolve ruff failures
1 parent 609778f commit b8eff78

File tree

3 files changed

+36
-36
lines changed

3 files changed

+36
-36
lines changed

docs/migration.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,7 @@ The session hierarchy now uses a new **runtime-checkable Protocol** called `Abst
556556

557557
Key characteristics of `AbstractBaseSession`:
558558
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`).
559+
2. **Simplified Type Parameters**: It takes two parameters: `AbstractBaseSession[SendRequestT_contra, SendNotificationT_contra]`. Contravariant variance is used for these parameters to ensure that sessions can be used safely in generic contexts (like `RequestContext`).
560560
3. **BaseSession Implementation**: The concrete implementation logic (state management, response routing) is provided by the `BaseSession` class, which satisfies the protocol.
561561

562562
**Before:**
@@ -580,9 +580,9 @@ class MySession(AbstractBaseSession[...]):
580580
self._my_state = {}
581581
```
582582

583-
#### `SendRequestT` changed to contravariant
583+
#### `SendRequestT` renamed to `SendRequestT_contra`
584584

585-
The `SendRequestT` TypeVar is now defined as **contravariant** to support its use in the `AbstractBaseSession` Protocol.
585+
The `SendRequestT` TypeVar is now defined as **contravariant** to support its use in the `AbstractBaseSession` Protocol and renamed accordingly to follow naming conventions for contravariant type variables.
586586

587587
**Before:**
588588

@@ -593,12 +593,12 @@ SendRequestT = TypeVar("SendRequestT", ClientRequest, ServerRequest)
593593
**After:**
594594

595595
```python
596-
SendRequestT = TypeVar("SendRequestT", ClientRequest, ServerRequest, contravariant=True)
596+
SendRequestT_contra = TypeVar("SendRequestT_contra", ClientRequest, ServerRequest, contravariant=True)
597597
```
598598

599-
#### `SendNotificationT` changed to contravariant
599+
#### `SendNotificationT` renamed to `SendNotificationT_contra`
600600

601-
The `SendNotificationT` TypeVar is now defined as **contravariant** to support its use in the `AbstractBaseSession` Protocol.
601+
The `SendNotificationT` TypeVar is now defined as **contravariant** to support its use in the `AbstractBaseSession` Protocol and renamed accordingly to follow naming conventions for contravariant type variables.
602602

603603
**Before:**
604604

@@ -609,14 +609,14 @@ SendNotificationT = TypeVar("SendNotificationT", ClientNotification, ServerNotif
609609
**After:**
610610

611611
```python
612-
SendNotificationT = TypeVar(
613-
"SendNotificationT", ClientNotification, ServerNotification, contravariant=True
612+
SendNotificationT_contra = TypeVar(
613+
"SendNotificationT_contra", ClientNotification, ServerNotification, contravariant=True
614614
)
615615
```
616616

617-
#### `ReceiveResultT` changed to covariant
617+
#### `ReceiveResultT` renamed to `ReceiveResultT_co`
618618

619-
The `ReceiveResultT` TypeVar is now defined as **covariant** to support its use in the `AbstractBaseSession` Protocol.
619+
The `ReceiveResultT` TypeVar is now defined as **covariant** to support its use in the `AbstractBaseSession` Protocol and renamed accordingly to follow naming conventions for covariant type variables.
620620

621621
**Before:**
622622

@@ -627,7 +627,7 @@ ReceiveResultT = TypeVar("ReceiveResultT", bound=BaseModel)
627627
**After:**
628628

629629
```python
630-
ReceiveResultT = TypeVar("ReceiveResultT", bound=BaseModel, covariant=True)
630+
ReceiveResultT_co = TypeVar("ReceiveResultT_co", bound=BaseModel, covariant=True)
631631
```
632632

633633
#### `BaseClientSession` is now a Protocol

src/mcp/shared/_context.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
"""Request context for MCP handlers."""
22

33
from dataclasses import dataclass
4-
from typing import TYPE_CHECKING, Generic, Any
4+
from typing import TYPE_CHECKING, Any, Generic
5+
56
from typing_extensions import TypeVar
67

78
if TYPE_CHECKING:
89
from mcp.shared.session import AbstractBaseSession
910

1011
from mcp.types import RequestId, RequestParamsMeta
1112

12-
SessionT_co = TypeVar(
13-
"SessionT_co", bound="AbstractBaseSession[Any, Any]", covariant=True
14-
)
13+
SessionT_co = TypeVar("SessionT_co", bound="AbstractBaseSession[Any, Any]", covariant=True)
1514

1615

1716
@dataclass(kw_only=True)

src/mcp/shared/session.py

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@
44
from collections.abc import Callable
55
from contextlib import AsyncExitStack
66
from types import TracebackType
7-
from typing import Any, Generic, Protocol, TypeVar
7+
from typing import Any, Generic, Protocol, Self, TypeVar, runtime_checkable
88

99
import anyio
1010
from anyio.streams.memory import MemoryObjectReceiveStream, MemoryObjectSendStream
1111
from pydantic import BaseModel, TypeAdapter
12-
from typing_extensions import Protocol, Self, runtime_checkable
1312

1413
from mcp.shared.exceptions import MCPError
1514
from mcp.shared.message import MessageMetadata, ServerMessageMetadata, SessionMessage
@@ -35,13 +34,13 @@
3534
ServerResult,
3635
)
3736

38-
SendRequestT = TypeVar("SendRequestT", ClientRequest, ServerRequest, contravariant=True)
37+
SendRequestT_contra = TypeVar("SendRequestT_contra", ClientRequest, ServerRequest, contravariant=True)
3938
SendResultT = TypeVar("SendResultT", ClientResult, ServerResult)
40-
SendNotificationT = TypeVar(
41-
"SendNotificationT", ClientNotification, ServerNotification, contravariant=True
39+
SendNotificationT_contra = TypeVar(
40+
"SendNotificationT_contra", ClientNotification, ServerNotification, contravariant=True
4241
)
4342
ReceiveRequestT = TypeVar("ReceiveRequestT", ClientRequest, ServerRequest)
44-
ReceiveResultT = TypeVar("ReceiveResultT", bound=BaseModel, covariant=True)
43+
ReceiveResultT_co = TypeVar("ReceiveResultT_co", bound=BaseModel, covariant=True)
4544
ReceiveNotificationT = TypeVar("ReceiveNotificationT", ClientNotification, ServerNotification)
4645

4746
RequestId = str | int
@@ -76,7 +75,9 @@ def __init__(
7675
request_id: RequestId,
7776
request_meta: RequestParamsMeta | None,
7877
request: ReceiveRequestT,
79-
session: BaseSession[SendRequestT, SendNotificationT, SendResultT, ReceiveRequestT, ReceiveNotificationT],
78+
session: BaseSession[
79+
SendRequestT_contra, SendNotificationT_contra, SendResultT, ReceiveRequestT, ReceiveNotificationT
80+
],
8081
on_complete: Callable[[RequestResponder[ReceiveRequestT, SendResultT]], Any],
8182
message_metadata: MessageMetadata = None,
8283
) -> None:
@@ -160,8 +161,8 @@ def cancelled(self) -> bool:
160161
class AbstractBaseSession(
161162
Protocol,
162163
Generic[
163-
SendRequestT,
164-
SendNotificationT,
164+
SendRequestT_contra,
165+
SendNotificationT_contra,
165166
],
166167
):
167168
"""Pure abstract interface for MCP sessions.
@@ -172,12 +173,12 @@ class AbstractBaseSession(
172173

173174
async def send_request(
174175
self,
175-
request: SendRequestT,
176-
result_type: type[ReceiveResultT],
176+
request: SendRequestT_contra,
177+
result_type: type[ReceiveResultT_co],
177178
request_read_timeout_seconds: float | None = None,
178179
metadata: MessageMetadata = None,
179180
progress_callback: ProgressFnT | None = None,
180-
) -> ReceiveResultT:
181+
) -> ReceiveResultT_co:
181182
"""Sends a request and wait for a response.
182183
183184
Raises an MCPError if the response contains an error. If a request read timeout is provided, it will take
@@ -189,7 +190,7 @@ async def send_request(
189190

190191
async def send_notification(
191192
self,
192-
notification: SendNotificationT,
193+
notification: SendNotificationT_contra,
193194
related_request_id: RequestId | None = None,
194195
) -> None:
195196
"""Emits a notification, which is a one-way message that does not expect a response."""
@@ -208,12 +209,12 @@ async def send_progress_notification(
208209

209210
class BaseSession(
210211
AbstractBaseSession[
211-
SendRequestT,
212-
SendNotificationT,
212+
SendRequestT_contra,
213+
SendNotificationT_contra,
213214
],
214215
Generic[
215-
SendRequestT,
216-
SendNotificationT,
216+
SendRequestT_contra,
217+
SendNotificationT_contra,
217218
SendResultT,
218219
ReceiveRequestT,
219220
ReceiveNotificationT,
@@ -287,12 +288,12 @@ async def __aexit__(
287288

288289
async def send_request(
289290
self,
290-
request: SendRequestT,
291-
result_type: type[ReceiveResultT],
291+
request: SendRequestT_contra,
292+
result_type: type[ReceiveResultT_co],
292293
request_read_timeout_seconds: float | None = None,
293294
metadata: MessageMetadata = None,
294295
progress_callback: ProgressFnT | None = None,
295-
) -> ReceiveResultT:
296+
) -> ReceiveResultT_co:
296297
"""Sends a request and wait for a response.
297298
298299
Raises an MCPError if the response contains an error. If a request read timeout is provided, it will take
@@ -346,7 +347,7 @@ async def send_request(
346347

347348
async def send_notification(
348349
self,
349-
notification: SendNotificationT,
350+
notification: SendNotificationT_contra,
350351
related_request_id: RequestId | None = None,
351352
) -> None:
352353
"""Emits a notification, which is a one-way message that does not expect a response."""

0 commit comments

Comments
 (0)