Inference-Aware Load Balancing for LLMs (part 13): MCP through the BIG-IP, sessions and persistence

How MCP agent sessions behave behind a load balancer: the Model Context Protocol on the wire, why round robin breaks stateful sessions, and how BIG-IP 21.1's MCP profile and MCP persistence are configured.
Hand-drawn: 0 of 6 MCP sessions through a plain virtual server, 6 of 6 with the MCP profile and MCP persistence

On this page

An MCP server has two replicas, each holding its sessions in memory. Six agent sessions of eight tool calls each went through a plain BIG-IP virtual server: 0 of 6 worked. Through a virtual server with the MCP profile and MCP persistence on the same pool, 6 of 6 worked, with every call of a session on one replica. And the session id the client is given grows from the server's 32 characters to a 141-character encrypted wrapper, with 0 persistence records on the BIG-IP. This is part 13 of Inference-Aware Load Balancing for LLMs. It goes with a video on the LevelUP I.T. YouTube channel.

MCP persistence
MCP persistence

What MCP is, to a load balancer

The Model Context Protocol is how an agent (a model with a loop around it) discovers and calls tools. Messages are JSON-RPC 2.0; over the streamable HTTP transport each one is an HTTP POST to one endpoint, and the answer is plain JSON or a server-sent event stream. A session begins with initialize; the server returns an Mcp-Session-Id header and the client sends it on every later request. One session on the wire, exactly as this lab's tools send it:

POST /mcp   Content-Type: application/json   Accept: application/json, text/event-stream
  {"jsonrpc": "2.0", "id": 1, "method": "initialize", "params": {"protocolVersion": "2025-06-18", ...}}
200   Mcp-Session-Id: <the server's id>
POST /mcp   Mcp-Session-Id: <id>   MCP-Protocol-Version: 2025-06-18
  {"jsonrpc": "2.0", "method": "notifications/initialized"}        (no id: no answer)
POST /mcp   Mcp-Session-Id: <id>   {... "method": "tools/list"}   then tools/call, tools/call, ...

A stateful server keeps the session in its own memory, and another replica has never heard of it. To a load balancer, MCP is many short HTTP requests, each possibly on a new TCP connection, that must all reach the same replica; only the header ties them together.

The server and the client

The lab's server (f5lab/mcp:1, built for this series) is the MCP Python SDK's FastMCP on port 8000 with stateless_http=False. Its tools are whoami (which replica answered, and how many calls this session made there), remember / recall (a note in that replica's memory), and order_status / fetch_url for part 14 (two lab-only switches, off here). The client does what an agent does without the model: N sessions, each initializes, stores a note, calls whoami K times and recalls the note. The generated manifest runs 2 replicas, spread over k8s-w1 and k8s-w2; the BIG-IP's pool members are the pod addresses, reached across the fabric because Cilium announces the pod networks over BGP.

The BIG-IP objects

There are two generated scripts. The persistence profile, with the passphrase on standard input only (generated into the lab's secrets, never typed or shown):

read -r P; tmsh create ltm persistence mcp f5lab-mcp-persist { mcp-encryption-passphrase "$P" }; unset P
bigip "$(cat bigip/mcp-persist.sh)" < /root/f5-clos-dc-secrets/lb-f5a-mcp-passphrase

Then the profiles, the pool and two virtual servers on the same pool:

tmsh create ltm profile json f5lab-json
tmsh create ltm profile sse f5lab-sse
tmsh create ltm pool mcp-pool members add { MCP_PODS } monitor tcp load-balancing-mode round-robin
tmsh create ltm virtual vs-mcp-plain destination 10.20.30.163:80 ip-protocol tcp profiles add { tcp http } \
  pool mcp-pool source-address-translation { type automap }
tmsh create ltm virtual vs-mcp destination 10.20.30.162:80 ip-protocol tcp profiles add { tcp http f5lab-json f5lab-sse aimcp } \
  persist replace-all-with { f5lab-mcp-persist { default yes } } pool mcp-pool source-address-translation { type automap }

This post is for paying subscribers only

Subscribe to LevelUp I.T. newsletter and stay updated.

Don't miss anything. Get all the latest posts delivered straight to your inbox. It's free!
Great! Check your inbox and click the link to confirm your subscription.
Error! Please enter a valid email address!