Guide / MCP hosting
Host a remote MCP server from a Dockerfile
Host your own remote Model Context Protocol (MCP) server as a Resilis service. This example builds a small Python server from a Dockerfile and verifies its tool with a local authenticated MCP client.
What you need
A Resilis account with permission to deploy a service, a GitHub repository, Docker for the local check, and Python 3.12 for the client. Running the service is subject to your plan, resource limits and billing. This example uses a shared bearer token for a trusted client; it is not a multi-user authorization system.
Create the MCP server
Create server.py. It exposes a harmless greeting tool over Streamable HTTP at /mcp. The application requires a nonempty MCP_TOKEN at startup, checks it on every HTTP request and rejects requests with an Origin header; browser clients are therefore not supported.
server.py
import hmac
import os
import uvicorn
from mcp.server.fastmcp import FastMCP
secret = os.environ["MCP_TOKEN"]
if not secret:
raise RuntimeError("MCP_TOKEN must not be empty")
mcp = FastMCP("example", host="0.0.0.0", port=int(os.environ.get("PORT", "8000")), stateless_http=True)
@mcp.tool()
def greet(name: str) -> str:
"""Return a greeting."""
return f"Hello, {name}!"
app = mcp.streamable_http_app()
async def protected(scope, receive, send):
if scope["type"] == "http":
headers = scope["headers"]
origins = [value for key, value in headers if key == b"origin"]
credentials = [value for key, value in headers if key == b"authorization"]
expected = f"Bearer {secret}".encode()
if origins or len(credentials) != 1 or not hmac.compare_digest(credentials[0], expected):
await send({"type": "http.response.start", "status": 401, "headers": [(b"content-type", b"text/plain")]})
await send({"type": "http.response.body", "body": b"Unauthorized"})
return
await app(scope, receive, send)
if __name__ == "__main__":
uvicorn.run(protected, host="0.0.0.0", port=int(os.environ.get("PORT", "8000"))) Create this Dockerfile in the same directory. The image installs the MCP Python SDK and runs the server on PORT, or 8000 when PORT is unset.
Dockerfile
FROM python:3.12-slim
WORKDIR /app
RUN pip install --no-cache-dir 'mcp==1.26.0' 'uvicorn==0.40.0'
COPY server.py .
CMD ["python", "server.py"] Test the container locally
Build the image, then enter a long random token privately when prompted. Keep it out of source control, shell history, screenshots and logs. The published port binds only to your computer’s loopback interface.
Terminal
docker build -t example-mcp .
read -rs MCP_TOKEN; export MCP_TOKEN
docker run --rm --name example-mcp -p 127.0.0.1:8000:8000 -e MCP_TOKEN example-mcp In a second terminal, set MCP_TOKEN to the same value privately. Install the same SDK in a virtual environment, then use this real MCP client to initialize a session, list tools and call greet.
Terminal
python3 -m venv .venv
. .venv/bin/activate
pip install 'mcp==1.26.0'
read -rs MCP_TOKEN; export MCP_TOKEN
python client.py client.py
import asyncio
import os
import httpx
from mcp import ClientSession
from mcp.client.streamable_http import streamable_http_client
async def main():
headers = {"Authorization": f"Bearer {os.environ['MCP_TOKEN']}"}
async with httpx.AsyncClient(headers=headers, follow_redirects=False) as http_client:
async with streamable_http_client(os.environ.get("MCP_URL", "http://127.0.0.1:8000/mcp"), http_client=http_client) as (read, write, _):
async with ClientSession(read, write) as session:
await session.initialize()
tools = await session.list_tools()
result = await session.call_tool("greet", {"name": "Resilis"})
print([tool.name for tool in tools.tools])
print(result.content[0].text)
asyncio.run(main()) The client should print a list containing greet and then Hello, Resilis!. Confirm a request without the token is rejected: the HTTP status should be 401.
Terminal
curl -s -o /dev/null -w '%{http_code}\n' http://127.0.0.1:8000/mcp Stop the container with Ctrl+C. Do not commit the token or virtual environment.
Deploy your server
Commit server.py and Dockerfile to your repository, without the token. In the Resilis dashboard, create a service from that repository and select the Dockerfile build. Set MCP_TOKEN as an environment variable with the Secret option enabled. Set PORT=8000 and the app deployment port to 8000, then deploy. Follow the Dockerfile deployment guide for the existing dashboard workflow.
Use the service’s HTTPS URL ending in /mcp with an MCP client that can send an Authorization: Bearer token header on every request. Set MCP_URL to that HTTPS URL when using the example client, and supply MCP_TOKEN privately. Never send the token over plain HTTP to a remote host. The local container check does not establish that a Resilis deployment or remote HTTPS connection has succeeded; check the deployed service and client yourself.
Use a separate strong token for each deployment and rotate it if exposed. This example has no per-user identity, permissions, rate limiting, or token rotation workflow. Add controls suited to your users and tools before handling sensitive data. A browser-based client requires a separately designed origin policy.
How this differs from Resilis MCP
This guide hosts your own MCP server as an application. The separate Resilis MCP server at https://mcp.resilis.io exposes Resilis operations and uses its own OAuth sign-in and permissions. The shared token in this example does not grant access to Resilis operations.