Back to Fastmcp

base

docs/python-sdk/fastmcp-client-transports-base.mdx

4.0.03.8 KB
Original Source

fastmcp.client.transports.base

Classes

ClientSessionKwargs <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/client/transports/base.py#L24" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>

Keyword arguments for the MCP ClientSession constructor.

TransportOptions <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/client/transports/base.py#L42" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>

How one client wants its connection built.

These belong to the client rather than to the transport, so a transport shared between clients doesn't leak one client's settings to another. Different client layers may own different fields; a layer that adds its settings must preserve the existing options rather than replace the bundle.

Attributes:

  • session_class: The ClientSession class to instantiate. Proxies supply a session that skips output-schema validation, since they relay results rather than consume them.
  • forward_incoming_headers: Whether to forward eligible inbound HTTP headers upstream, including authorization. Hop-specific HTTP headers and MCP transport, routing, and event-stream state are excluded because each backend connection owns that state. Only appropriate for proxies; honored by the HTTP and SSE transports and ignored by the others.
  • backend_mode: The connect mode to give backend clients that a wrapping transport builds on this client's behalf, so a chain of connections speaks one protocol era end to end. None leaves each backend client at its own default. Honored by MCPConfigTransport, whose multi-server form mounts a proxy per configured server and resolves one shared era for the aggregate; ignored by transports that connect to a single backend directly, since those carry the connecting client's own session and era.

ClientTransport <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/client/transports/base.py#L80" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>

Abstract base class for different MCP client transport mechanisms.

A Transport is responsible for establishing and managing connections to an MCP server, and providing a ClientSession within an async context.

Methods:

connect_session <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/client/transports/base.py#L98" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>

python
connect_session(self, **session_kwargs: Unpack[SessionKwargs]) -> AsyncIterator[ClientSession]

Establishes a connection and yields an active ClientSession.

The ClientSession is not expected to be initialized in this context manager.

The session is guaranteed to be valid only within the scope of the async context manager. Connection setup and teardown are handled within this context.

Args:

  • transport_options: How the connecting client wants this connection built. Defaults apply when omitted. A transport that wraps others must pass this along.
  • **session_kwargs: Keyword arguments to pass to the ClientSession constructor (e.g., callbacks, timeouts).

close <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/client/transports/base.py#L130" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>

python
close(self)

Close the transport.

get_session_id <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/client/transports/base.py#L133" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>

python
get_session_id(self) -> str | None

Get the session ID for this transport, if available.