14
14
15
15
16
16
class Transport (ABC ):
17
- """Base class for MCP transport implementations."""
17
+ """Base class for MCP transport implementations.
18
+
19
+ This abstract base class defines the interface that all MCP transport
20
+ implementations must follow. It provides the core methods for connection
21
+ management and message exchange.
22
+
23
+ Examples:
24
+ >>> # Transport is abstract and cannot be instantiated directly
25
+ >>> try:
26
+ ... Transport()
27
+ ... except TypeError as e:
28
+ ... print("Cannot instantiate abstract class")
29
+ Cannot instantiate abstract class
30
+
31
+ >>> # Check if Transport is an abstract base class
32
+ >>> from abc import ABC
33
+ >>> issubclass(Transport, ABC)
34
+ True
35
+
36
+ >>> # Verify abstract methods are defined
37
+ >>> hasattr(Transport, 'connect')
38
+ True
39
+ >>> hasattr(Transport, 'disconnect')
40
+ True
41
+ >>> hasattr(Transport, 'send_message')
42
+ True
43
+ >>> hasattr(Transport, 'receive_message')
44
+ True
45
+ >>> hasattr(Transport, 'is_connected')
46
+ True
47
+ """
18
48
19
49
@abstractmethod
20
50
async def connect (self ) -> None :
21
- """Initialize transport connection."""
51
+ """Initialize transport connection.
52
+
53
+ This method should establish the underlying connection for the transport.
54
+ It must be called before sending or receiving messages.
55
+
56
+ Examples:
57
+ >>> # This is an abstract method - implementation required in subclasses
58
+ >>> import inspect
59
+ >>> inspect.ismethod(Transport.connect)
60
+ False
61
+ >>> hasattr(Transport, 'connect')
62
+ True
63
+ """
22
64
23
65
@abstractmethod
24
66
async def disconnect (self ) -> None :
25
- """Close transport connection."""
67
+ """Close transport connection.
68
+
69
+ This method should clean up the underlying connection and any associated
70
+ resources. It should be called when the transport is no longer needed.
71
+
72
+ Examples:
73
+ >>> # This is an abstract method - implementation required in subclasses
74
+ >>> import inspect
75
+ >>> inspect.ismethod(Transport.disconnect)
76
+ False
77
+ >>> hasattr(Transport, 'disconnect')
78
+ True
79
+ """
26
80
27
81
@abstractmethod
28
82
async def send_message (self , message : Dict [str , Any ]) -> None :
29
83
"""Send a message over the transport.
30
84
31
85
Args:
32
86
message: Message to send
87
+
88
+ Examples:
89
+ >>> # This is an abstract method - implementation required in subclasses
90
+ >>> import inspect
91
+ >>> inspect.ismethod(Transport.send_message)
92
+ False
93
+ >>> hasattr(Transport, 'send_message')
94
+ True
33
95
"""
34
96
35
97
@abstractmethod
@@ -38,6 +100,14 @@ async def receive_message(self) -> AsyncGenerator[Dict[str, Any], None]:
38
100
39
101
Yields:
40
102
Received messages
103
+
104
+ Examples:
105
+ >>> # This is an abstract method - implementation required in subclasses
106
+ >>> import inspect
107
+ >>> inspect.ismethod(Transport.receive_message)
108
+ False
109
+ >>> hasattr(Transport, 'receive_message')
110
+ True
41
111
"""
42
112
43
113
@abstractmethod
@@ -46,4 +116,12 @@ async def is_connected(self) -> bool:
46
116
47
117
Returns:
48
118
True if connected
119
+
120
+ Examples:
121
+ >>> # This is an abstract method - implementation required in subclasses
122
+ >>> import inspect
123
+ >>> inspect.ismethod(Transport.is_connected)
124
+ False
125
+ >>> hasattr(Transport, 'is_connected')
126
+ True
49
127
"""
0 commit comments