Concept #133Hardextended-ai-concepts

How to create an MCP server and host it in Docker MCP toolkit?

#gen-ai#mcp

Answer

Creating and Hosting an MCP Server in Docker

Docker provides an excellent way to package and deploy MCP servers — ensuring consistent environments, easy distribution, and isolation.

Step 1: Build an MCP Server

python
# mcp_server.py
from mcp.server import Server
from mcp.server.stdio import stdio_server
import mcp.types as types
import asyncio
import os

app = Server("docker-mcp-server")

@app.list_tools()
async def list_tools():
    return [
        types.Tool(
            name="get_env_info",
            description="Get environment information",
            inputSchema={"type": "object", "properties": {}}
        ),
        types.Tool(
            name="echo",
            description="Echo a message back",
            inputSchema={
                "type": "object",
                "properties": {"message": {"type": "string"}},
                "required": ["message"]
            }
        )
    ]

@app.call_tool()
async def call_tool(name: str, arguments: dict):
    if name == "get_env_info":
        return [types.TextContent(type="text",
            text=f"Python version: 3.11, OS: Linux, Container: Docker")]

    if name == "echo":
        return [types.TextContent(type="text",
            text=f"Echo: {arguments['message']}")]

async def main():
    async with stdio_server() as (read_stream, write_stream):
        await app.run(read_stream, write_stream,
                      app.create_initialization_options())

if __name__ == "__main__":
    asyncio.run(main())

Step 2: Create Requirements

text
# requirements.txt
mcp>=1.0.0
anyio>=4.0.0

Step 3: Dockerfile

dockerfile
FROM python:3.11-slim

WORKDIR /app

# Install dependencies
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

# Copy server code
COPY mcp_server.py .

# MCP servers communicate via stdio — no port needed
CMD ["python", "mcp_server.py"]

Step 4: Build Docker Image

bash
docker build -t my-mcp-server:latest .

# Test locally
docker run -i my-mcp-server:latest

Step 5: Docker MCP Toolkit

Docker's official MCP toolkit provides a standardized way to package and run MCP servers:

bash
# Install Docker Desktop (includes MCP support)
# Enable MCP in Docker Desktop settings

# Run an MCP server via Docker MCP toolkit
docker mcp run my-mcp-server:latest

# Docker Desktop automatically discovers MCP servers
# and makes them available to AI clients

Step 6: Configure Claude to Use Docker MCP Server

json
{
  "mcpServers": {
    "my-server": {
      "command": "docker",
      "args": [
        "run",
        "--rm",
        "-i",
        "my-mcp-server:latest"
      ]
    }
  }
}

Docker MCP with Network Access

dockerfile
# For MCP servers that need database or API access
FROM python:3.11-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY mcp_server.py .

# Environment variables for secrets
ENV DATABASE_URL=""
ENV API_KEY=""

CMD ["python", "mcp_server.py"]
bash
# Run with environment variables
docker run -i   -e DATABASE_URL="postgresql://user:pass@host:5432/db"   -e API_KEY="your-api-key"   my-mcp-server:latest

Publishing to Docker Hub

bash
# Tag and push your MCP server image
docker tag my-mcp-server:latest yourusername/my-mcp-server:latest
docker push yourusername/my-mcp-server:latest

# Others can use it with:
# docker run -i yourusername/my-mcp-server:latest

Benefits of Docker for MCP

BenefitDescription
IsolationEach MCP server in its own container
ReproducibilitySame behavior everywhere
Easy distributionShare via Docker Hub
Dependency managementNo system conflicts
VersioningTag specific versions