haive.mcp.client.transport¶
MCP Transport Layer Implementation.
This module provides transport layer implementations for the MCP protocol. Different transports handle the underlying communication mechanism between the MCP client and server.
- Supported Transports:
STDIO: Communication via stdin/stdout with a subprocess
HTTP: RESTful communication over HTTP
SSE: Server-sent events for streaming
WebSocket: Real-time bidirectional communication
Docker: Communication via stdin/stdout with a Docker container
- The transport layer is responsible for:
Establishing and managing connections
Sending and receiving raw messages
Handling transport-specific encoding/decoding
Managing connection lifecycle
Classes¶
Docker transport for MCP communication. |
|
HTTP transport for MCP communication. |
|
Abstract base class for MCP transports. |
|
Server-Sent Events transport for MCP communication. |
|
STDIO transport for MCP communication. |
|
WebSocket transport for MCP communication. |
Module Contents¶
- class haive.mcp.client.transport.DockerTransport(image, env=None, volumes=None, ports=None, network=None, docker_args=None, timeout=30.0)[source]¶
Bases:
MCPTransportDocker transport for MCP communication.
This transport runs an MCP server inside a Docker container and communicates via stdin/stdout, similar to StdioTransport but using
docker runas the process wrapper. This provides isolation, reproducibility, and simplified dependency management for MCP servers.- The transport handles:
Docker container lifecycle management
Volume mounting for data access
Port mapping for network services
Environment variable passing to the container
Graceful container shutdown with
docker stop
Examples
Basic Docker transport:
transport = DockerTransport( image="mcp/postgres", env={"POSTGRES_HOST": "host.docker.internal"}, )
With volumes and network:
transport = DockerTransport( image="mcp/filesystem", volumes=["/home/user/data:/data:ro"], network="host", env={"ALLOWED_DIRS": "/data"}, )
Initialize Docker transport.
- Parameters:
image (str) – Docker image name (e.g.,
"mcp/postgres")env (Optional[Dict[str, str]]) – Environment variables to pass to the container
volumes (Optional[List[str]]) – Volume mounts (e.g.,
["/host/path:/container/path:ro"])ports (Optional[List[str]]) – Port mappings (e.g.,
["8080:8080"])network (Optional[str]) – Docker network to attach to (e.g.,
"host")docker_args (Optional[List[str]]) – Extra arguments to pass to
docker runtimeout (float) – Timeout for operations in seconds
- async connect()[source]¶
Start the Docker container and establish stdio communication.
- Return type:
None
- class haive.mcp.client.transport.HttpTransport(url, headers=None, timeout=30.0)[source]¶
Bases:
MCPTransportHTTP transport for MCP communication.
This transport communicates with an MCP server over HTTP using JSON-RPC over HTTP POST requests. This is useful for servers that expose HTTP APIs or for remote MCP servers.
Examples
Basic HTTP transport:
transport = HttpTransport("http://localhost:8080/mcp")
With authentication:
transport = HttpTransport( "https://api.example.com/mcp", headers={"Authorization": "Bearer token123"} )
Initialize HTTP transport.
- Parameters:
- class haive.mcp.client.transport.MCPTransport(timeout=30.0)[source]¶
Bases:
abc.ABCAbstract base class for MCP transports.
All MCP transports must implement this interface to provide a consistent API for the protocol layer. The transport handles the low-level communication while the protocol layer handles MCP message semantics.
- The transport is responsible for:
Connection establishment and teardown
Raw message sending and receiving
Transport-specific error handling
Connection state management
Initialize transport with timeout.
- Parameters:
timeout (float) – Default timeout for operations in seconds
- abstractmethod connect()[source]¶
- Async:
- Return type:
None
Establish connection to the MCP server.
- Raises:
MCPConnectionError – If connection fails
MCPTimeoutError – If connection times out
- Return type:
None
- abstractmethod disconnect()[source]¶
- Async:
- Return type:
None
Close connection to the MCP server.
Should be idempotent and safe to call multiple times.
- abstractmethod receive_message()[source]¶
- Async:
- Return type:
Dict[str, Any]
Receive a message from the server.
- Returns:
JSON-RPC message from server
- Raises:
MCPTransportError – If receiving fails
MCPConnectionError – If not connected
MCPTimeoutError – If receive times out
- Return type:
Dict[str, Any]
- abstractmethod send_message(message)[source]¶
- Async:
- Parameters:
message (Dict[str, Any])
- Return type:
None
Send a message to the server.
- Parameters:
message (Dict[str, Any]) – JSON-RPC message to send
- Raises:
MCPTransportError – If sending fails
MCPConnectionError – If not connected
- Return type:
None
- class haive.mcp.client.transport.SseTransport(sse_url, post_url, headers=None, timeout=30.0)[source]¶
Bases:
MCPTransportServer-Sent Events transport for MCP communication.
This transport uses SSE for receiving messages and HTTP POST for sending. Useful for streaming scenarios where the server needs to push messages to the client.
Initialize SSE transport.
- Parameters:
- class haive.mcp.client.transport.StdioTransport(command, args=None, env=None, cwd=None, timeout=30.0)[source]¶
Bases:
MCPTransportSTDIO transport for MCP communication.
This transport communicates with an MCP server via stdin/stdout of a subprocess. This is the most common transport for MCP servers that are designed to run as command-line tools.
- The transport handles:
Process lifecycle management
JSON-RPC message framing over stdio
Process cleanup on disconnection
Error handling for process failures
Examples
Basic usage:
transport = StdioTransport( command="npx", args=["-y", "@modelcontextprotocol/server-filesystem", "/tmp"] )
With environment variables:
transport = StdioTransport( command="python", args=["-m", "my_mcp_server"], env={"API_KEY": "secret"}, cwd="/path/to/server" )
Initialize STDIO transport.
- Parameters:
- async connect()[source]¶
Start the MCP server process and establish stdio communication.
- Return type:
None
- class haive.mcp.client.transport.WebSocketTransport(url, headers=None, timeout=30.0)[source]¶
Bases:
MCPTransportWebSocket transport for MCP communication.
This transport provides real-time bidirectional communication over WebSocket. Useful for interactive applications and real-time scenarios.
Initialize WebSocket transport.
- Parameters: