How to deploy MCP server to production
Quick answer
Deploy an
MCP server to production by running the mcp.server.stdio.stdio_server or a custom mcp.server.Server implementation on a reliable host with proper process management. Use containerization or systemd for stability, and ensure secure environment variables and logging are configured.PREREQUISITES
Python 3.8+pip install mcpBasic knowledge of systemd or DockerAccess to a Linux server or cloud VM
Setup
Install the mcp Python package and prepare your environment variables. Ensure Python 3.8 or higher is installed on your production server.
pip install mcp Step by step
Run a basic MCP server using the stdio transport. This example shows a minimal server that can be deployed as a systemd service or inside a Docker container.
import os
from mcp.server.stdio import stdio_server
if __name__ == "__main__":
# Optionally configure logging or environment here
stdio_server() output
Server started, listening on stdio for MCP messages...
Common variations
You can implement a custom mcp.server.Server subclass for advanced logic or use alternative transports like SSE. For production, wrap the server in a Docker container or use systemd for process management and auto-restart.
from mcp.server import Server
class CustomMCPServer(Server):
def handle_request(self, request):
# Implement your request handling logic here
return {"result": "ok"}
if __name__ == "__main__":
server = CustomMCPServer()
server.serve() output
Custom MCP server running and handling requests...
Troubleshooting
- If the server stops unexpectedly, use systemd or Docker restart policies to auto-restart.
- Check logs for JSON parsing errors or transport issues.
- Ensure environment variables are securely managed and not exposed.
Key Takeaways
- Use
mcp.server.stdio.stdio_serverfor a simple production-ready MCP server. - Manage the MCP server process with systemd or Docker for reliability.
- Implement custom
mcp.server.Serversubclasses for advanced use cases. - Secure environment variables and logs to protect sensitive data.
- Monitor server logs and set up auto-restart to handle failures gracefully.