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

The transport layer is responsible for:
  • Establishing and managing connections

  • Sending and receiving raw messages

  • Handling transport-specific encoding/decoding

  • Managing connection lifecycle

AttributesΒΆ

ClassesΒΆ

HttpTransport

HTTP transport for MCP communication.

MCPTransport

Abstract base class for MCP transports.

SseTransport

Server-Sent Events transport for MCP communication.

StdioTransport

STDIO transport for MCP communication.

WebSocketTransport

WebSocket transport for MCP communication.

Module ContentsΒΆ

class mcp.client.transport.HttpTransport(url: str, headers: Dict[str, str] | None = None, timeout: float = 30.0)ΒΆ

Bases: MCPTransport

HTTP 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"}
)
async connect() NoneΒΆ

Create HTTP session.

async disconnect() NoneΒΆ

Close HTTP session.

async receive_message() Dict[str, Any]ΒΆ

Send pending message and receive response.

async send_message(message: Dict[str, Any]) NoneΒΆ

Send JSON-RPC message via HTTP POST.

Note: For HTTP transport, sending and receiving are combined in a request-response cycle. This method stores the message for the next receive_message call.

headersΒΆ
session: aiohttp.ClientSession | None = NoneΒΆ
urlΒΆ
class mcp.client.transport.MCPTransport(timeout: float = 30.0)ΒΆ

Bases: abc.ABC

Abstract 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

async __aenter__()ΒΆ

Async context manager entry.

async __aexit__(exc_type, exc_val, exc_tb)ΒΆ

Async context manager exit.

abstractmethod connect() NoneΒΆ
Async:

Establish connection to the MCP server.

Raises:
abstractmethod disconnect() NoneΒΆ
Async:

Close connection to the MCP server.

Should be idempotent and safe to call multiple times.

abstractmethod receive_message() Dict[str, Any]ΒΆ
Async:

Receive a message from the server.

Returns:

JSON-RPC message from server

Raises:
abstractmethod send_message(message: Dict[str, Any]) NoneΒΆ
Async:

Send a message to the server.

Parameters:

message – JSON-RPC message to send

Raises:
connected = FalseΒΆ
timeout = 30.0ΒΆ
class mcp.client.transport.SseTransport(sse_url: str, post_url: str, headers: Dict[str, str] | None = None, timeout: float = 30.0)ΒΆ

Bases: MCPTransport

Server-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.

async connect() NoneΒΆ

Establish SSE connection.

async disconnect() NoneΒΆ

Close SSE connection.

async receive_message() Dict[str, Any]ΒΆ

Receive message from SSE stream.

async send_message(message: Dict[str, Any]) NoneΒΆ

Send message via HTTP POST.

headersΒΆ
post_urlΒΆ
session: aiohttp.ClientSession | None = NoneΒΆ
sse_urlΒΆ
class mcp.client.transport.StdioTransport(command: str, args: List[str] | None = None, env: Dict[str, str] | None = None, cwd: str | None = None, timeout: float = 30.0)ΒΆ

Bases: MCPTransport

STDIO 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"
)
async connect() NoneΒΆ

Start the MCP server process and establish stdio communication.

async disconnect() NoneΒΆ

Stop the MCP server process and cleanup resources.

async receive_message() Dict[str, Any]ΒΆ

Receive JSON-RPC message from server via stdout.

async send_message(message: Dict[str, Any]) NoneΒΆ

Send JSON-RPC message to server via stdin.

args = []ΒΆ
commandΒΆ
cwd = NoneΒΆ
env = NoneΒΆ
process: asyncio.subprocess.Process | None = NoneΒΆ
class mcp.client.transport.WebSocketTransport(url: str, headers: Dict[str, str] | None = None, timeout: float = 30.0)ΒΆ

Bases: MCPTransport

WebSocket transport for MCP communication.

This transport provides real-time bidirectional communication over WebSocket. Useful for interactive applications and real-time scenarios.

async connect() NoneΒΆ

Establish WebSocket connection.

async disconnect() NoneΒΆ

Close WebSocket connection.

async receive_message() Dict[str, Any]ΒΆ

Receive message from WebSocket.

async send_message(message: Dict[str, Any]) NoneΒΆ

Send message over WebSocket.

headersΒΆ
session: aiohttp.ClientSession | None = NoneΒΆ
urlΒΆ
websocket: aiohttp.ClientWebSocketResponse | None = NoneΒΆ
mcp.client.transport.loggerΒΆ