Coverage for mcpgateway/transports/base.py: 100%

13 statements  

« prev     ^ index     » next       coverage.py v7.9.1, created at 2025-06-22 12:53 +0100

1# -*- coding: utf-8 -*- 

2"""Base Transport Interface. 

3 

4Copyright 2025 

5SPDX-License-Identifier: Apache-2.0 

6Authors: Mihai Criveti 

7 

8This module defines the base protocol for MCP transports. 

9""" 

10 

11from abc import ABC, abstractmethod 

12from typing import Any, AsyncGenerator, Dict 

13 

14 

15class Transport(ABC): 

16 """Base class for MCP transport implementations.""" 

17 

18 @abstractmethod 

19 async def connect(self) -> None: 

20 """Initialize transport connection.""" 

21 

22 @abstractmethod 

23 async def disconnect(self) -> None: 

24 """Close transport connection.""" 

25 

26 @abstractmethod 

27 async def send_message(self, message: Dict[str, Any]) -> None: 

28 """Send a message over the transport. 

29 

30 Args: 

31 message: Message to send 

32 """ 

33 

34 @abstractmethod 

35 async def receive_message(self) -> AsyncGenerator[Dict[str, Any], None]: 

36 """Receive messages from the transport. 

37 

38 Yields: 

39 Received messages 

40 """ 

41 

42 @abstractmethod 

43 async def is_connected(self) -> bool: 

44 """Check if transport is connected. 

45 

46 Returns: 

47 True if connected 

48 """