A template for creating Model Context Protocol (MCP) servers in Python, deployable via Railway. This template provides a basic structure that you can customize by swapping out the functions in src/tools.py and updating references in server.py.
git clone <your-repo-url>
cd mcp-server-template
uv sync # or pip install -e .Replace the example functions in src/tools.py with your own MCP tools:
# Replace this:
async def template_function(input: dict):
"""Generate a template function - a template function for any platform."""
return await utility_function()
# With your own functions:
async def my_custom_tool(input: dict):
"""Description of what your tool does."""
# Your implementation here
passUpdate server.py to register your new tools and customize the server name/instructions:
# Change the server name and instructions
mcp = FastMCP(
name="My-Custom-MCP", # Change this
instructions="""
Description of what your MCP server does.
"""
)
# Update the tool registration
@mcp.tool()
async def my_custom_tool(input: dict): # Update function name
"""Tool description and documentation."""
return await my_custom_function(input) # Update function callUpdate pyproject.toml with your project details:
[project]
name = "my-mcp-server" # Change this
description = "Description of your MCP server" # Change thisInstall dependencies with uv (preferred) or pip:
uv sync
# or
pip install -e .Start the API server with Uvicorn:
uvicorn server:app --host 0.0.0.0 --port 8000And then, if testing via MCP inspector, connect with a proxy via streamable-http at:
http://0.0.0.0:8000/mcp
This template includes Railway configuration for easy deployment (Python-only, no Node.js required):
-
Set up environment variables: Copy
env.exampleto.envand add your API keys:cp env.example .env # Edit .env with your API keys -
Initialize Git repository (if not already done):
git init git add . git commit -m "Initial commit"
-
Push to GitHub: Create a new repository on GitHub and push your code:
git remote add origin https://github.com/yourusername/your-repo-name.git git push -u origin main
-
Deploy on Railway:
- Sign in to Railway and create a new project
- Choose "Deploy from GitHub repo" and select your repository
- Railway will automatically detect your Python application and deploy it
The railway.json configuration includes:
- Automatic dependency installation via
uv sync --frozenusingpyproject.toml - Uvicorn server startup via
uv runon Railway's assigned port - Health check endpoint monitoring at
/health - Automatic restarts on failure (up to 10 retries)
Your MCP server will be available at the Railway-provided URL, and you can connect MCP clients using the Railway domain + /mcp endpoint.