-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Expand file tree
/
Copy pathbase.py
More file actions
47 lines (37 loc) · 1.3 KB
/
base.py
File metadata and controls
47 lines (37 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
"""Base transport protocol for MCP clients."""
from __future__ import annotations
from collections.abc import AsyncIterator
from contextlib import asynccontextmanager
from typing import Protocol, runtime_checkable
from anyio.streams.memory import MemoryObjectReceiveStream, MemoryObjectSendStream
from mcp.shared.message import SessionMessage
@runtime_checkable
class Transport(Protocol):
"""Protocol for MCP client transports.
All transports must implement a connect() async context manager that yields
a tuple of (read_stream, write_stream) for bidirectional communication.
Example:
```python
class MyTransport:
@asynccontextmanager
async def connect(self):
# Set up connection...
yield read_stream, write_stream
# Clean up...
```
"""
@asynccontextmanager
async def connect(
self,
) -> AsyncIterator[
tuple[
MemoryObjectReceiveStream[SessionMessage | Exception],
MemoryObjectSendStream[SessionMessage],
]
]:
"""Connect to the server and yield streams for communication.
Yields:
A tuple of (read_stream, write_stream) for bidirectional communication.
"""
...
yield # type: ignore[misc]