Host a remote MCP server on your VPS: Python SDK, systemd, Caddy HTTPS and a bearer token

Print
  • 0

In short: if you want Claude or another AI assistant to call tools on your own server from anywhere, the MCP server has to stay online all day and be reachable over HTTPS. This guide builds exactly that on an Ubuntu or Debian VPS: a small Python program made with the MCP SDK, kept alive by systemd, published through Caddy with a free certificate and guarded by a secret token. Plan for roughly half an hour.

Before you start

How the pieces fit together

ComponentIts job in this setup
MCP SDK for PythonRuns your tools and speaks the MCP protocol, only on the internal address
systemdStarts the program at boot and brings it back if it fails
CaddyObtains and renews the free SSL certificate, checks the token, forwards valid calls
ufwBlocks every port except SSH, HTTP and HTTPS

1. Install the packages and a dedicated system account

Run these commands as root:

apt update && apt install -y python3-venv caddy ufw
useradd -r -m -d /opt/mcp -s /usr/sbin/nologin mcp
runuser -u mcp -- python3 -m venv /opt/mcp/venv
runuser -u mcp -- /opt/mcp/venv/bin/pip install "mcp[cli]>=1.20,<2"

Besides Python tooling, the first line brings in Caddy and the ufw firewall. The account called mcp has no login shell and no administrator rights, and runuser executes the next two commands under that account, so the virtual environment belongs to it. Whoever manages to misuse the service still cannot alter the operating system.

2. Write a first tool

Save the following as /opt/mcp/server.py. It offers one tool, and that tool only reads: it reports how full the main disk is.

import shutil
from mcp.server.fastmcp import FastMCP

mcp = FastMCP("vps-tools", host="127.0.0.1", port=8000)

@mcp.tool()
def disk_usage() -> str:
    """Shows the used space on the main disk."""
    total, used, free = shutil.disk_usage("/")
    return f"Used {used // 2**30} GB of {total // 2**30} GB"

if __name__ == "__main__":
    mcp.run(transport="streamable-http")

This example targets the 1.x line of the official Python MCP SDK. Because it binds to 127.0.0.1, nobody on the internet can talk to it directly. Only programs on the VPS itself reach http://127.0.0.1:8000/mcp.

3. Keep it running with systemd

Describe the service in /etc/systemd/system/mcp.service:

[Unit]
Description=MCP server
After=network.target

[Service]
User=mcp
WorkingDirectory=/opt/mcp
ExecStart=/opt/mcp/venv/bin/python /opt/mcp/server.py
Restart=on-failure

[Install]
WantedBy=multi-user.target

Load and activate it with systemctl daemon-reload && systemctl enable --now mcp. From now on the program comes back by itself whenever the VPS reboots. To follow its output live, type journalctl -u mcp -f.

4. Publish it over HTTPS behind a token

Create a random secret with openssl rand -hex 32, which prints 64 hexadecimal characters. Place it in /etc/caddy/Caddyfile like this:

mcp.your-domain.md {
    @authorized header Authorization "Bearer YOUR_TOKEN"
    handle @authorized {
        reverse_proxy 127.0.0.1:8000 {
            header_up Host 127.0.0.1:8000
        }
    }
    respond "Access denied" 401
}

Keep the header_up Host 127.0.0.1:8000 line. An MCP server bound to 127.0.0.1 defends itself by refusing calls whose Host header names a different address. Without the rewrite, Caddy would forward your public subdomain and the SDK would reply with error 421.

Apply the change with systemctl reload caddy. Caddy fetches the free certificate on its own and renews it later without your help. A call that lacks the exact token is answered with 401 by Caddy and never touches your Python code.

5. Close everything else with ufw

ufw allow 22/tcp
ufw allow 80/tcp
ufw allow 443/tcp
ufw enable

Check which port your SSH daemon listens on before enabling the firewall. If it is not 22, allow that number in the first line instead, or you will be locked out of the VPS. Port 8000 is never opened, since only Caddy talks to it, locally.

6. Add the server to Claude Code

claude mcp add --transport http vps https://mcp.your-domain.md/mcp --header "Authorization: Bearer YOUR_TOKEN"

Web connectors in Claude and in ChatGPT generally expect OAuth rather than a fixed token, which means running an OAuth provider next to this server. The MCP SDK can handle that, but it is a separate project not covered here.

Troubleshooting

SymptomLikely causeFix
Error 421The Host header was not rewrittenRestore the header_up line and reload Caddy
Error 401Token missing, or not identical to the one in the CaddyfileCompare both values character by character
No answer at allThe Python service stopped or crashedRead the log with journalctl -u mcp -f

Questions people ask

Is Python the only option?

No, an official TypeScript SDK exists as well. The systemd unit, the Caddy setup and the firewall rules work the same way with a Node.js program.

What is safe to expose to the assistant?

Begin with read-only tools: whether services are up, free disk space, recent errors from the logs. Never write a tool that executes whatever command it is given, because that hands the whole server to the assistant.

How do I replace an old or leaked token?

Create a fresh one, swap it into the Caddyfile, run systemctl reload caddy and paste it into the AI app. Caddy rejects the previous value immediately.

Will this work on a shared web hosting plan?

No. Shared hosting gives no root access, so Caddy cannot be installed and long-running services cannot be started. To pick a suitable VPS type, see Managed VPS Pro or Unmanaged VPS: which one fits you.

Useful articles

Was this answer helpful?

Need help?

Write to us in the chat, by e-mail or open a ticket from your account.

Live chatOpen the chat Call us+373 795-800-80 Write to ussupport@tophost.md
Copyright © 2026 TopHost.MD. All Rights Reserved.