A clean, professional template for building MCP (Model Context Protocol) servers using streamable HTTP transport. This template is based on the official simple-streamablehttp example from the MCP Python SDK.
- β Low-level MCP Server implementation - Uses the official MCP Python SDK's low-level server
- β Streamable HTTP transport - Full support for HTTP-based MCP communication
- β Resumability support - Built-in event store for connection resumption
- β Example tools and resources - Ready-to-use examples you can extend
- β Professional structure - Clean, maintainable code organization
- β Development tools - Linting, formatting, and testing setup
- β Easy configuration - Environment-based configuration management
- β CORS support - Ready for browser-based clients
- Python 3.10 or higher
- pip or uv for package management
-
Clone or download this template:
git clone https://github.com/raihan0824/mcp-http-template.git cd mcp-http-template -
Install dependencies:
# Using pip pip install -r requirements.txt # Or using uv (recommended) uv sync
-
Run the server:
python -m mcp_server.main
The server will start on
http://127.0.0.1:3000by default.
This template includes a Makefile for common tasks:
# Install dependencies
make install
# Install development dependencies
make install-dev
# Run the server
make run
# Format code
make format
# Run linting
make lint
# Run type checking
make type-check
# Docker commands
make docker-build # Build Docker image
make docker-run # Run with docker-compose
make docker-dev # Run development version
make docker-stop # Stop containers
make docker-logs # View logs-
Build and run with Docker Compose:
docker-compose up -d
-
Or build and run manually:
docker build -t mcp-http-template . docker run -p 3000:3000 mcp-http-template
- β
Non-root user: Runs as
mcpuser(UID 1000) for security - β Multi-stage build: Optimized image size and security
- β Health checks: Built-in container health monitoring
- β Environment variables: Full configuration via env vars
- β Security hardened: No new privileges, minimal attack surface
All configuration can be done via environment variables:
docker run -p 3000:3000 \
-e MCP_SERVER_NAME="My Custom Server" \
-e MCP_LOG_LEVEL="DEBUG" \
-e MCP_PORT=3000 \
mcp-http-templateFor development with live code reloading:
# Run development container with volume mounts
docker-compose --profile dev up -d mcp-server-dev
# View logs
docker-compose logs -f mcp-server-devpython -m mcp_server.main --help
Options:
--port INTEGER Port to listen on for HTTP (default: 3000)
--log-level TEXT Logging level (DEBUG, INFO, WARNING, ERROR, CRITICAL)
--json-response Enable JSON responses instead of SSE streams
--help Show this message and exit.Copy env.example to .env and modify as needed:
cp env.example .envAvailable environment variables:
MCP_SERVER_NAME- Server name (default: "MCP HTTP Template Server")MCP_HOST- Host to bind to (default: "localhost")MCP_PORT- Port to listen on (default: 8000)MCP_LOG_LEVEL- Logging level (default: "INFO")MCP_ENABLE_TOOLS- Enable tools (default: "true")MCP_ENABLE_RESOURCES- Enable resources (default: "true")MCP_ENABLE_PROMPTS- Enable prompts (default: "true")
The template includes several example tools:
Greet someone by name.
{
"name": "greet",
"arguments": {
"name": "Alice"
}
}Perform basic mathematical calculations.
{
"name": "calculate",
"arguments": {
"operation": "add",
"a": 5,
"b": 3
}
}Get information about the server.
{
"name": "get_server_info",
"arguments": {}
}Send a stream of notifications (demonstrates streaming capabilities).
{
"name": "send_notification",
"arguments": {
"interval": 1.0,
"count": 3,
"caller": "test-client"
}
}Get the current server status.
Get the current server configuration.
-
Test basic connectivity:
curl -X POST http://127.0.0.1:3000/mcp \\ -H "Content-Type: application/json" \\ -d '{"jsonrpc": "2.0", "id": 1, "method": "tools/list"}'
-
Call a tool:
curl -X POST http://127.0.0.1:3000/mcp \\ -H "Content-Type: application/json" \\ -d '{ "jsonrpc": "2.0", "id": 1, "method": "tools/call", "params": { "name": "greet", "arguments": {"name": "World"} } }'
-
List resources:
curl -X POST http://127.0.0.1:3000/mcp \\ -H "Content-Type: application/json" \\ -d '{"jsonrpc": "2.0", "id": 1, "method": "resources/list"}'
This server is compatible with any MCP client that supports the streamable HTTP transport. The server endpoint is:
http://127.0.0.1:3000/mcp
-
Add your tool handler in
mcp_server/main.py:elif name == "your_tool_name": # Your tool logic here result = "Your result" return [types.TextContent(type="text", text=result)]
-
Add the tool definition to
list_tools():types.Tool( name="your_tool_name", description="Description of your tool", inputSchema={ "type": "object", "required": ["param1"], "properties": { "param1": { "type": "string", "description": "Parameter description" } } } )
-
Add the resource to
list_resources():types.Resource( uri=AnyUrl("your://resource"), name="Your Resource", description="Description of your resource", mimeType="text/plain" )
-
Handle the resource in
read_resource():elif str(uri) == "your://resource": return "Your resource content"
This template uses Black and isort for code formatting:
# Format code
make format
# Check formatting
make lintType checking is done with mypy:
make type-checkmcp-http-template/
βββ mcp_server/
β βββ __init__.py # Package initialization
β βββ main.py # Main server implementation
β βββ config.py # Configuration management
βββ tests/
β βββ __init__.py # Tests package
βββ requirements.txt # Python dependencies
βββ pyproject.toml # Project configuration
βββ Makefile # Development commands
βββ env.example # Environment variables template
βββ .gitignore # Git ignore rules
βββ README.md # This file
-
Event Store: The included
InMemoryEventStoreis for demonstration only. For production, implement a persistent event store (Redis, database, etc.). -
Security: Update CORS settings in production:
starlette_app = CORSMiddleware( starlette_app, allow_origins=["https://yourdomain.com"], # Specific origins allow_methods=["GET", "POST", "DELETE"], expose_headers=["Mcp-Session-Id"], )
-
Logging: Configure appropriate logging levels and handlers for production.
-
Error Handling: Add comprehensive error handling for your specific use cases.
This template is provided under the MIT License. See the LICENSE file for details.
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
For questions about MCP:
For issues with this template:
- Open an issue in this repository
- Check the existing issues for similar problems
Happy coding! π