jonathantoaf/mcp-server-boilerplate
If you are the rightful owner of mcp-server-boilerplate and would like to certify it and/or have it hosted online, please leave a comment on the right or send an email to henry@mcphub.com.
This document provides a comprehensive guide to setting up and customizing a Model Context Protocol (MCP) server using the FastMCP framework.
MCP Server Boilerplate
A simple and generic starting point for building Model Context Protocol (MCP) servers using FastMCP.
Features
- š FastMCP Integration: Built on the FastMCP framework for easy MCP server development
- š Organized Structure: Clean, modular codebase with separation of concerns
- š§ Configuration Management: YAML-based configuration with environment variable support
- š Logging: Structured logging with colored console output
- š³ Docker Ready: Includes Dockerfile for containerization
- š ļø Developer Tools: Pre-configured with Ruff for linting and formatting
- š¦ UV Package Manager: Fast dependency management with uv
Quick Start
Prerequisites
- Python 3.12+
- uv (recommended) or pip
Installation
-
Clone the repository
git clone <your-repo-url> cd mcp-server-boilerplate/mcp-server
-
Install dependencies
# Using uv (recommended) uv sync # Or using pip pip install -e .
-
Configure your server
# Copy the example environment file cp .env.example .env # Edit the .env file with your settings vim .env
-
Run the server
# Using uv uv run python main.py # Or using python directly (after activating venv) python main.py
Project Structure
mcp-server/
āāā main.py # Entry point
āāā pyproject.toml # Project configuration
āāā dockerfile # Docker configuration
āāā setup.py # Development setup script
āāā .env.example # Example environment variables
āāā mcp_server/
ā āāā config.yaml # Server configuration
ā āāā mcp_server.py # Main MCP server class
ā āāā data_models/ # Pydantic models
ā ā āāā base.py # Base model with common config
ā ā āāā example.py # Example tool request/response models
ā āāā utils/ # Utility modules
ā āāā __init__.py # Package init
ā āāā config.py # Configuration loader
ā āāā logger.py # Logging setup
ā āāā request_wrapper.py # Error handling decorators
Creating Your MCP Server
1. Define Your Tools
Create request/response models in mcp_server/data_models/example.py
or create new model files:
from mcp_server.data_models.base import SharedBaseModel
from pydantic import Field
class MyToolRequest(SharedBaseModel):
input_text: str = Field(description="Text to process")
class MyToolResponse(SharedBaseModel):
result: str = Field(description="Processed result")
2. Implement Your Server
Modify mcp_server/mcp_server.py
to add your tools to the MCPServer
class:
@self.mcp.tool(
name="my_tool",
description="Description of what your tool does"
)
def my_tool(request: MyToolRequest) -> MyToolResponse:
# Your tool logic here
result = process_text(request.input_text)
return MyToolResponse(result=result)
3. Configure Your Server
Update .env
file with your specific settings:
SERVER_NAME="Your MCP Server"
SERVER_INSTRUCTIONS="Description of your server's capabilities"
HOST=0.0.0.0
PORT=5000
Configuration
The server uses environment variables that can be set in .env
file:
HOST
: Server host (default: "0.0.0.0")PORT
: Server port (default: "5000")TRANSPORT
: Transport type (default: "streamable-http")LOG_LEVEL
: Logging level (default: "DEBUG")SERVER_NAME
: Name of your MCP serverSERVER_INSTRUCTIONS
: Description of your server's capabilitiesEXTERNAL_API_URL
: URL for external API calls (if needed)
Docker Deployment
Build and run with Docker:
# Build the image
docker build -t my-mcp-server .
# Run the container
docker run -p 5000:5000 --env-file .env my-mcp-server
Development
Setup Development Environment
# Run the automated setup
python setup.py
Code Quality
# Format code
uv run ruff format
# Lint code
uv run ruff check
# Fix linting issues
uv run ruff check --fix
Inspector
This project includes an Inspector tool for debugging and introspection. For more details, see the .
Example Tools
The boilerplate includes example tools that demonstrate:
- Simple Tool:
example_tool
- Sorts an array of integers - External API Tool:
external_tool
- Calls an external API to sort an array
You can find these in mcp_server/mcp_server.py
and use them as templates for your own tools.
Customization Guide
- Rename the project: Update
pyproject.toml
name and description - Add your tools: Implement tools in the
MCPServer
class inmcp_server.py
- Configure environment: Update
.env
with your specific settings - Add dependencies: Update
pyproject.toml
dependencies section - Customize models: Create new models in
data_models/
or modify existing ones
Error Handling
The boilerplate includes error handling decorators:
from mcp_server.utils.request_wrapper import wrap_request_with_error_handling
@wrap_request_with_error_handling
def my_api_tool(request: MyRequest) -> MyResponse:
# Your API call logic here
pass
Contributing
- Fork the repository
- Create a feature branch
- Make your changes
- Submit a pull request