Topic 5 of 5 · 5 min read
Hosting remote MCP servers in the cloud
Goal: Choose a hosting setup for a remote server with given traffic and state needs and name the change a session-based server needs to scale.
Since each request stands alone, a remote MCP server can run as many identical copies behind a load balancer, or as a serverless function, as long as anything that must persist lives in storage you control and is referred to by an explicit handle. Hosting is then a normal web-service question.
Check yourself · 1 of 3
Why can a Streamable HTTP server of revision 2026-07-28 run as several identical instances behind a load balancer?
Why it matters
Earlier revisions tied a conversation to a session id, which pushed teams toward sticky routing or in-memory maps. From 2026-07-28 each POST carries its protocol version, client info and capabilities in _meta, so any instance can answer any call. That is what makes scaling out and scaling to zero practical.
How to choose a setup
- Look at the traffic shape. Spiky or rare calls fit serverless functions, which cost nothing idle. Steady high traffic or long streams fit containers on a managed service with autoscaling.
- Plan for streams. An SSE reply stays open for the length of the tool call. Check the platform's response time limit and idle timeout, and make sure proxies do not buffer (
X-Accel-Buffering: no). A subscription stream needs keep-alive comments. - Make state explicit. If a tool builds a draft, a report or a job, it returns an id such as
draft_8421, and later tools take that id as an argument. Store the record in a database or cache keyed by id and check that it belongs to the authenticated user. The model carries the id in the conversation. - Handle secrets properly. Keep keys, client secrets and database passwords in the platform's secret manager, not in the image or repository. Load them at start-up.
- Terminate TLS at the edge. Serve the endpoint and every authorization endpoint over HTTPS. Keep the Origin allow list and the token audience check in the app, not only at the gateway.
- Use the headers. A gateway can route or rate-limit on
Mcp-MethodandMcp-Namewithout reading bodies.
Before and after
# weak: state in one process, keyed by a session id
carts = {}
def add_item(session_id, item):
carts.setdefault(session_id, []).append(item)
With two instances, the second one has no cart. The fix:
def create_cart(user):
cart_id = db.carts.insert({"owner": user.id, "items": []})
return {"cart_id": cart_id}
def add_item(user, cart_id, item):
cart = db.carts.get(cart_id, owner=user.id) # 404 if not theirs
cart.items.append(item)
What goes wrong
- Sticky sessions added to hide in-memory state, which break at deploy time.
- A serverless time limit shorter than the longest tool call.
- A handle that is guessable and not tied to the user, which exposes other people's data.
Examples
Serverless or containers
A server answers about 200 calls a day, each under two seconds: a serverless function fits and costs nothing idle. A server that streams 10-minute exports to many users: containers on an autoscaling service, with idle timeouts raised and proxy buffering off.
Fixing a session-based server
The old server stored a working draft in a dictionary under the session id. The change: add a create_draft tool that returns draft_id, store drafts in the database with owner, and make edit_draft and publish_draft take draft_id. Remove the dictionary and the session-id reads. Now any instance can serve any call.
Key points
- A stateless remote server can run as many identical instances or as serverless functions behind a load balancer.
- State that must outlive one request lives in a database or cache and is addressed by an explicit handle passed as a tool argument.
- Handles must be checked against the authenticated user, not trusted because they are hard to guess.
- Check platform time limits and proxy buffering for SSE streams before choosing a host.
- Secrets belong in a secret manager, and TLS covers the MCP endpoint and all authorization endpoints.
Common mistakes
- Using sticky routing to keep a session map alive instead of moving the state out.
- Putting secrets in the container image or committed config.
- Choosing serverless without checking its maximum response time against the slowest tool.
- Trusting a handle without checking that it belongs to the caller.
Check yourself · 2 of 3
A tool builds a report in several steps. Where should the half-finished report live on a stateless server?
Check yourself · 3 of 3
Where should a server's database password and client secret be stored in a cloud deployment?
Practice items, not counted towards your mastery.
Start here
- MCP authorization specification (opens in a new tab)DocsFree
The normative OAuth profile: discovery, registration priority, resource parameter, token handling and scope challenges.
- MCP transports specification (opens in a new tab)DocsFree
Streamable HTTP rules: one endpoint, required headers, Origin validation and the removal of sessions.
- Understanding authorization in MCP (tutorial) (opens in a new tab)DocsFree
A step-by-step explanation of the authorization flow for server and client builders.