|
3 | 3 | from __future__ import annotations |
4 | 4 |
|
5 | 5 | from contextlib import AbstractAsyncContextManager |
6 | | -from types import TracebackType |
7 | | -from typing import Protocol, TypeVar, runtime_checkable |
8 | | - |
9 | | -from typing_extensions import Self |
| 6 | +from typing import Protocol |
10 | 7 |
|
| 8 | +from mcp.shared._stream_protocols import ReadStream, WriteStream |
11 | 9 | from mcp.shared.message import SessionMessage |
12 | 10 |
|
13 | | -T_co = TypeVar("T_co", covariant=True) |
14 | | -T_contra = TypeVar("T_contra", contravariant=True) |
15 | | - |
16 | | - |
17 | | -@runtime_checkable |
18 | | -class ReadStream(Protocol[T_co]): # pragma: no branch |
19 | | - """Protocol for reading items from a stream. |
20 | | -
|
21 | | - Both ``MemoryObjectReceiveStream`` and ``ContextReceiveStream`` satisfy |
22 | | - this protocol. Consumers that need the sender's context should use |
23 | | - ``getattr(stream, 'last_context', None)``. |
24 | | - """ |
25 | | - |
26 | | - async def receive(self) -> T_co: ... # pragma: no branch |
27 | | - async def aclose(self) -> None: ... # pragma: no branch |
28 | | - def __aiter__(self) -> ReadStream[T_co]: ... # pragma: no branch |
29 | | - async def __anext__(self) -> T_co: ... # pragma: no branch |
30 | | - async def __aenter__(self) -> Self: ... # pragma: no branch |
31 | | - async def __aexit__( # pragma: no branch |
32 | | - self, |
33 | | - exc_type: type[BaseException] | None, |
34 | | - exc_val: BaseException | None, |
35 | | - exc_tb: TracebackType | None, |
36 | | - ) -> bool | None: ... |
37 | | - |
38 | | - |
39 | | -@runtime_checkable |
40 | | -class WriteStream(Protocol[T_contra]): # pragma: no branch |
41 | | - """Protocol for writing items to a stream. |
42 | | -
|
43 | | - Both ``MemoryObjectSendStream`` and ``ContextSendStream`` satisfy |
44 | | - this protocol. |
45 | | - """ |
46 | | - |
47 | | - async def send(self, item: T_contra, /) -> None: ... # pragma: no branch |
48 | | - async def aclose(self) -> None: ... # pragma: no branch |
49 | | - async def __aenter__(self) -> Self: ... # pragma: no branch |
50 | | - async def __aexit__( # pragma: no branch |
51 | | - self, |
52 | | - exc_type: type[BaseException] | None, |
53 | | - exc_val: BaseException | None, |
54 | | - exc_tb: TracebackType | None, |
55 | | - ) -> bool | None: ... |
56 | | - |
| 11 | +__all__ = ["ReadStream", "WriteStream", "Transport", "TransportStreams"] |
57 | 12 |
|
58 | 13 | TransportStreams = tuple[ReadStream[SessionMessage | Exception], WriteStream[SessionMessage]] |
59 | 14 |
|
|
0 commit comments