Streamable HTTP MCP Server
Build & Deploy MCP server with Streamable HTTP protocol using a pre-built template with deployment to Render

Streamable HTTP MCP Server
This template provides a FastMCP 2.0 implementation of a Model Context Protocol (MCP) server that uses Streamable HTTP protocol to communicate with clients. The MCP Server is designed to be deployed to Render.com. The MCP server exposes tools that can be used by AI assistants through the open MCP standard, with PostgreSQL database integration.
Project Structure
.
├── joke_admin_app/ # Flask web app for database management
│ ├── app.py # Flask application with authentication
│ └── templates/ # HTML templates for web interface
├── data/ # Sample data files
├── .env.sample # Sample environment variables
├── .gitignore # Git ignore file
├── database.py # Async PostgreSQL database configuration
├── main.py # FastMCP 2.0 server implementation
├── models.py # SQLAlchemy database models
├── render.yaml # Render deployment configuration
├── requirements.txt # Python dependencies
└── README.md # Project documentation
Core Components
1. FastMCP 2.0 Server Implementation (main.py
)
This is a complete implementation of the Model Context Protocol using FastMCP 2.0. It supports:
- Streamable HTTP transport protocol
- API key authentication middleware
- Database integration with PostgreSQL
- Tool execution with async database operations
- Automatic database table creation
The server includes a sample tool (tell_joke
) that retrieves random jokes from a PostgreSQL database.
2. Database Layer (database.py
& models.py
)
- database.py: Configures async SQLAlchemy engine with PostgreSQL support
- models.py: Defines database models using SQLAlchemy ORM
- Supports both local development and Render deployment database URLs
3. Flask Admin Web App (joke_admin_app/
)
A Flask web application with authentication for managing database content:
- Flask-Login authentication system
- CRUD operations for database entities
- Web interface for content management
Getting Started
Development Prerequisites
- Python 3.8 or later
- PostgreSQL database (local or cloud)
- pip or uv for package management
Local Development
- Clone this repository
- Copy environment variables:
cp .env.sample .env
- Update
.env
with your database credentials and API key - Install dependencies:
pip install -r requirements.txt
- Start the MCP server:
python main.py
The MCP server will be available locally at http://localhost:8000/mcp/
Testing Your MCP Server
Using Visual Studio Code
The recommended way to test your MCP server is using Visual Studio Code with MCP support:
Enable MCP Support: Follow the official VS Code MCP documentation to enable MCP support in VS Code.
Configure Your Server: In your VS Code workspace, create a
.vscode/mcp.json
file with the following configuration:For local testing:
{ "servers": { "joke-server": { "url": "http://localhost:8000/mcp/", "headers": { "X-API-Key": "your-api-key" } } } }
For deployed server:
{ "servers": { "joke-server": { "url": "https://your-service-name.onrender.com/mcp/", "headers": { "X-API-Key": "your-api-key" } } } }
Test the Connection:
- Open the CHAT window in VS Code
- Select 'Agent' mode
- Type something like
"tell a joke"
to test the tool functionality
Deployment
Deploying to Render.com
Option 1: Using render.yaml (Recommended)
- Push your repository to GitHub
- Connect your repository to Render
- Render will automatically detect the
render.yaml
file and create:- PostgreSQL database
- FastMCP server web service
- Flask admin web app
Option 2: Manual Setup
- Create a PostgreSQL database on Render
- Create a new web service with:
- Environment: Python 3
- Build Command:
pip install -r requirements.txt
- Start Command:
python main.py
- Set environment variables:
DATABASE_URL
: Connection string from your Render PostgreSQLJOKE_MCP_SERVER_API_KEY
: Your API key for authentication
After deployment, your MCP server will be available at https://your-service-name.onrender.com/mcp
Extending the Template
Adding New Tools
To add a new tool, use the FastMCP decorator in main.py
:
@mcp_server.tool(
name="your_new_tool",
description="Description of what your tool does"
)
async def your_new_tool(param1: str, param2: int = 10) -> str:
"""Your tool implementation here."""
async with AsyncSession(engine, expire_on_commit=False) as db_session:
# Database operations
result = await db_session.execute(select(YourModel))
# Process and return results
return "Tool result"
Adding New Database Models
To add a new database model in models.py
:
class YourNewModel(Base):
__tablename__ = "your_table"
id = sqlalchemy.Column(sqlalchemy.Integer, primary_key=True, index=True, autoincrement=True)
name = sqlalchemy.Column(sqlalchemy.String, nullable=False)
# Add more fields as needed
Adding External API Integrations
To integrate with external APIs:
- Add necessary packages to
requirements.txt
- Import and configure clients in your tool functions
- Make API calls within the tool handler
- Return processed results
Remember to handle authentication securely using environment variables.
Environment Variables
Required environment variables (see .env.sample
):
DATABASE_URL
: PostgreSQL connection stringJOKE_MCP_SERVER_API_KEY
: API key for MCP server authenticationHOST
: Server host (default: 0.0.0.0)PORT
: Server port (default: 8000)
For Flask admin app:
FLASK_SECRET_KEY
: Secret key for Flask sessionsADMIN_EMAIL
: Admin login emailADMIN_PASSWORD
: Admin login password