Chapter 8. Connecting Clients and Servers: MCP Transports
The MCP’s transport layer is what links clients and servers together, enabling them to send and receive messages. Understanding the transport layer from the JSON-RPC level where it interacts directly with streaming data up through the SDK abstractions and managing real-life connections and custom transports is crucial to effectively debugging and securing your own MCP projects. This chapter explores the MCP transport layer in depth, covering the JSON-RPC foundations of the layer, the connection lifecycle from the transport’s perspective, and the transport types available in MCP by default as well as custom transports. Moving from the low-level design to the high-level implementation, we will dig into practical issues, like implementing your own custom transport, using security threat modeling and mitigation strategies, and debugging and testing your transports.
Figure 8-1 shows the MCP stack in layers of abstraction, from the top-level application (a server or a client) at the top, which rests upon the MCP itself, under which lie the session objects that manage the correlation between sent requests and received responses, and the plumbing to the transport layer below it. The transport layer interacts directly with the network layer
below it, framing and serializing messages and adding them to streams managed by the session objects. (ClientSession lives for the duration of the host application, while ServerSession lives ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access