
How to host a remote MCP server on your own VPS
American Cloud
American Cloud Team
To host a remote MCP server on a VPS, run the server on 127.0.0.1 over Streamable HTTP, put Caddy in front of it for HTTPS and a token check, and keep it running with systemd. Before any of that works, list your domain in the SDK's own host check: out of the box, the official Python SDK answers every request that comes through a proxy with 421 Misdirected Request.
That 421 is the part that costs an afternoon. The rest is an ordinary small web service.
What is a remote MCP server?
An MCP server gives an AI app like Claude Code a list of tools it can call. A local one runs on your laptop and talks over stdio. A remote one is a web service the app reaches over HTTPS, so you set it up once and every machine and teammate can use it. If you want the longer version, start with what an MCP server is and how one works.
The current spec, version 2026-07-28, makes the remote side simpler than older guides suggest. Each message is one HTTP POST to one endpoint, and the reply is either JSON or a short SSE stream for that request. The spec lists the "removal of protocol-level sessions" in this version, so a plain reverse proxy is enough. No sticky sessions, no session store. Guides that still use an /sse endpoint describe the HTTP+SSE transport, deprecated since 2025.
What do you need?
- A Linux VM with ports 22, 80 and 443 open. One vCPU and 1 GB of RAM is plenty for a server like the one below.
- A domain with an A record pointing a name such as
mcp.example.comat the VM. - Python 3.10 or newer and the official SDK,
mcp2.3.0 on PyPI. - Caddy, which gets and renews the HTTPS certificate on its own.
How do you write the server?
Here's the whole server we ran. It has one tool that reports free disk space on the machine it runs on, which is the kind of thing only a server on your own box can do. Written for mcp 2.3.0, whose server class is MCPServer:
import shutil
from mcp.server import MCPServer
from mcp.server.transport_security import TransportSecuritySettings
mcp = MCPServer("disk-check")
@mcp.tool()
def disk_free(path: str = "/") -> str:
"""Report free disk space on this server, in GB."""
total, used, free = shutil.disk_usage(path)
return f"{free / 1e9:.1f} GB free of {total / 1e9:.1f} GB"
if __name__ == "__main__":
mcp.run(
transport="streamable-http",
host="127.0.0.1",
port=8000,
stateless_http=True,
json_response=True,
transport_security=TransportSecuritySettings(
allowed_hosts=["mcp.example.com"],
allowed_origins=["https://mcp.example.com"],
),
)It listens on 127.0.0.1:8000 at /mcp, which are the SDK's defaults. Binding to localhost means the only way in is through Caddy. stateless_http and json_response match the sessionless spec: every request stands alone and gets a plain JSON answer.
Why does the server answer 421 behind a proxy?
Because the SDK protects you from DNS rebinding, and by default it trusts only localhost. When the server binds to 127.0.0.1 and you pass no transport_security, the SDK accepts the Host header only for 127.0.0.1, localhost and [::1]. Caddy passes your real domain through as the Host header, so the server sees mcp.example.com and refuses.
We hit exactly this. With the default settings, every request through the proxy came back like this in the server log:
The fix is the transport_security block in the server above. List your domain in allowed_hosts and your HTTPS origin in allowed_origins, and the same request returns 200 OK. The check keeps working for everything else: a request carrying a foreign Origin header gets 403 Forbidden. That's the spec's rule, word for word: when the Origin header "is present and invalid, servers MUST respond with HTTP 403 Forbidden."
Two other fixes will also make the 421 go away, and both are worse:
- Rewriting the
Hostheader in Caddy to127.0.0.1makes every request look local, so the rebinding check passes everything. - Binding the server to
0.0.0.0turns off the SDK's localhost default and leaves port 8000 open to anyone who finds it, with no TLS and no token in front.
How do you put HTTPS and a token in front?
Install Caddy from its official package, then replace /etc/caddy/Caddyfile with this:
mcp.example.com {
@unauthorized not header Authorization "Bearer {env.MCP_TOKEN}"
respond @unauthorized 401
reverse_proxy 127.0.0.1:8000
}Naming the site mcp.example.com is all Caddy needs to get a certificate. It "provisions TLS certificates for all your sites and keeps them renewed" and redirects HTTP to HTTPS. The matcher turns away any request without your token before it reaches Python. We ran this same block on our test machine: no token, 401.
Put the token in Caddy's environment rather than the file. Generate one with openssl rand -hex 32, run sudo systemctl edit caddy, and add:
[Service]
Environment=MCP_TOKEN=<your-token>You don't need any streaming settings. Caddy's reverse_proxy flushes text/event-stream responses to the client immediately, so a tool that streams its answer works through it as is.
How do you keep it running?
Give the server a systemd unit so it starts on boot and comes back if it crashes. Save it as /etc/systemd/system/mcp.service, with the server in /opt/mcp and the SDK in a virtualenv there:
[Unit]
Description=MCP server
After=network-online.target
[Service]
User=mcp
WorkingDirectory=/opt/mcp
ExecStart=/opt/mcp/.venv/bin/python server.py
Restart=always
[Install]
WantedBy=multi-user.targetCreate the mcp user first (sudo useradd --system mcp), so the server doesn't run as root. Then sudo systemctl enable --now mcp, and journalctl -u mcp -f follows the server's log.
How do you connect Claude Code?
Claude Code adds a remote server with one command:
claude mcp add --transport http my-server https://mcp.example.com/mcp \
--header "Authorization: Bearer <your-token>"Run claude mcp get my-server to see whether it connected, then ask for the tool by what it does ("how much disk is free on my MCP box?"). Use --transport http, not sse.
To test it from any machine, send a request with no token:
curl -i -X POST https://mcp.example.com/mcpA 401 means Caddy is answering over HTTPS and the token check is on. Send it again with -H "Authorization: Bearer <your-token>": a 421 now means the request got past Caddy but your domain isn't in allowed_hosts yet.
Is a token enough, or do you need OAuth?
A token is enough when the people using the server are you and your team, in clients that let you set a header, like Claude Code. The spec allows it: "Authorization is OPTIONAL for MCP implementations."
Move to OAuth when other people sign in with their own accounts. That's the spec's path for HTTP servers: the server acts as an OAuth 2.1 resource server and publishes Protected Resource Metadata (RFC 9728) so clients can find where to sign in. It's how our own hosted MCP server works: you paste https://mcp.americancloud.com/mcp into your app and sign in to American Cloud in the browser, and no token ever sits in a config file.
What does it cost?
On American Cloud, that 1 vCPU, 1 GB box is our smallest VM: $16.5625 a month with a 25 GB disk. Opening ports 22, 80 and 443 when you create it uses the network's own public IP, so there's no extra charge. There are no egress fees, so a tool that returns large results costs the same as one that returns a line. You get a whole Linux box for that: your server can read files on disk, run other programs and keep long jobs going, and the bill doesn't move with the number of requests.
Cloudflare's guide puts the server on Workers instead. Its paid plan has a $5 monthly minimum, then bills by requests and CPU time past what's included, and your code runs inside Cloudflare's per-request limits rather than on a machine you control.
You can also skip the console. With the American Cloud MCP server connected to Claude Code, one prompt prices the VM, creates it with those three ports open and adds the DNS record, the same way our Deploy with AI recipes set up apps like n8n and Ghost.