RENDER_DEPLOYMENT.mdโข8.9 kB
# ๐ Deploying MCP Energy Server to Render.com
Complete guide to deploy your MCP Energy Server as a web API on Render.com using GitHub.
## ๐ Prerequisites
- โ
GitHub account
- โ
Render.com account (free tier available)
- โ
MCP Energy Server code (you have it!)
## ๐ฏ What You'll Get
After deployment, you'll have:
- ๐ Live web API accessible via HTTPS
- ๐ Interactive API documentation at `/docs`
- ๐ Automatic deployments from GitHub
- ๐ Health monitoring endpoint
- ๐ Free hosting on Render.com
## ๐ Step-by-Step Deployment
### Step 1: Push to GitHub
First, push your code to GitHub if you haven't already:
```bash
cd /mnt/user-data/outputs/mcp-energy
# Initialize git (if not done)
git init
git add .
git commit -m "Initial commit: MCP Energy Server with web API"
# Add remote (replace YOUR_USERNAME)
git remote add origin https://github.com/YOUR_USERNAME/mcp-energy.git
# Push to GitHub
git branch -M main
git push -u origin main
```
### Step 2: Create Render.com Account
1. Go to https://render.com
2. Click **"Get Started"** or **"Sign Up"**
3. Choose **"Sign up with GitHub"** (recommended)
4. Authorize Render to access your GitHub account
### Step 3: Create New Web Service
1. From your Render dashboard, click **"New +"**
2. Select **"Web Service"**
3. Click **"Connect GitHub"** if not already connected
4. Find and select your **`mcp-energy`** repository
5. Click **"Connect"**
### Step 4: Configure Web Service
Fill in the following settings:
**Basic Settings:**
```
Name: mcp-energy
Region: Oregon (US West) [or your preferred region]
Branch: main
Root Directory: (leave blank)
Runtime: Python 3
```
**Build & Deploy:**
```
Build Command: pip install -r requirements.txt
Start Command: python server_web.py
```
**Instance Type:**
```
Plan: Free ($0/month)
```
### Step 5: Add Environment Variables
Scroll down to **"Environment Variables"** section and add:
```
Key: EIA_API_KEY
Value: BPlYS1rJRCA0cyGaS6c2JP0rl3wDgUv0QPZQf1o0
```
```
Key: PORT
Value: 10000
```
### Step 6: Deploy!
1. Click **"Create Web Service"** at the bottom
2. Render will start building and deploying your service
3. Wait 2-5 minutes for the initial deployment
4. Watch the logs for any errors
### Step 7: Verify Deployment
Once deployed, you'll see:
- โ
Green "Live" status
- ๐ Your service URL (e.g., `https://mcp-energy.onrender.com`)
**Test your deployment:**
1. Open your service URL in a browser
2. You should see the API information page
3. Go to `https://your-service.onrender.com/docs` for interactive API documentation
4. Try the health check: `https://your-service.onrender.com/health`
## ๐งช Testing Your API
### Using the Browser
Visit your service URL + `/docs` to see the interactive Swagger UI:
```
https://your-service.onrender.com/docs
```
### Using curl
```bash
# Health check
curl https://your-service.onrender.com/health
# Root endpoint
curl https://your-service.onrender.com/
# Get electricity data
curl -X POST https://your-service.onrender.com/electricity \
-H "Content-Type: application/json" \
-d '{"frequency": "monthly", "data_type": "generation"}'
# Get natural gas data
curl -X POST https://your-service.onrender.com/natural-gas \
-H "Content-Type: application/json" \
-d '{"frequency": "monthly", "data_type": "consumption"}'
```
### Using Python
```python
import requests
base_url = "https://your-service.onrender.com"
# Get electricity data
response = requests.post(
f"{base_url}/electricity",
json={
"frequency": "monthly",
"data_type": "generation",
"facets": {"stateid": ["CA"]}
}
)
print(response.json())
```
### Using JavaScript/Node.js
```javascript
const baseUrl = "https://your-service.onrender.com";
fetch(`${baseUrl}/electricity`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
frequency: 'monthly',
data_type: 'generation',
facets: { stateid: ['CA'] }
})
})
.then(response => response.json())
.then(data => console.log(data));
```
## ๐ก Available API Endpoints
Your deployed API has these endpoints:
| Endpoint | Method | Description |
|----------|--------|-------------|
| `/` | GET | API information |
| `/health` | GET | Health check |
| `/docs` | GET | Interactive API documentation |
| `/electricity` | POST | Electricity data |
| `/natural-gas` | POST | Natural gas data |
| `/petroleum` | POST | Petroleum data |
| `/coal` | POST | Coal data |
| `/renewable` | POST | Renewable energy data |
| `/co2-emissions` | POST | CO2 emissions data |
| `/steo` | POST | Energy forecasts |
| `/international` | POST | International data |
## ๐ Automatic Deployments
Render automatically deploys when you push to GitHub:
1. Make changes to your code locally
2. Commit and push to GitHub:
```bash
git add .
git commit -m "Your changes"
git push
```
3. Render automatically detects the push and redeploys
4. Wait 2-3 minutes for the new version to go live
## ๐ Monitoring Your Service
### View Logs
1. Go to your service in Render dashboard
2. Click on **"Logs"** tab
3. See real-time logs of your application
### Monitor Health
Render automatically monitors your `/health` endpoint:
- Green = Healthy
- Red = Issues detected
### Check Metrics
On the Render dashboard you can see:
- Request rate
- Response times
- Memory usage
- CPU usage
## โ๏ธ Configuration Options
### Using render.yaml (Recommended)
Your project already includes `render.yaml`. Render will automatically use this config!
Benefits:
- โ
Configuration in code
- โ
Version controlled
- โ
Easy to replicate
- โ
No manual setup needed
### Custom Domain (Optional)
To use your own domain:
1. Go to your service settings
2. Click **"Custom Domain"**
3. Add your domain
4. Follow DNS configuration instructions
### Environment Variables
To update environment variables:
1. Go to **"Environment"** tab in your service
2. Click **"Add Environment Variable"**
3. Add key and value
4. Service will auto-redeploy
## ๐ Troubleshooting
### Deployment Failed
**Check build logs:**
1. Go to your service
2. Click on **"Logs"** or **"Events"**
3. Look for error messages
**Common issues:**
- Missing dependencies: Check `requirements.txt`
- Python version: Ensure Python 3.11+
- Port configuration: Must use `PORT` env variable
### Service Not Responding
**Check health endpoint:**
```bash
curl https://your-service.onrender.com/health
```
**Check logs:**
- Look for startup errors
- Verify API key is set
- Check for timeout issues
### API Returns Errors
**Test locally first:**
```bash
cd /mnt/user-data/outputs/mcp-energy
python server_web.py
```
**Check EIA API:**
- Verify API key is valid
- Check if EIA service is up
- Look at error messages in logs
## ๐ Security Best Practices
### API Key Management
**Current setup:** API key is in environment variables โ
**For production:**
1. Rotate API keys regularly
2. Never commit API keys to GitHub
3. Use Render's environment variables
### Rate Limiting
The free tier has limits:
- 400 build hours/month
- Automatic sleep after 15 min inactivity
- 750 hours/month of runtime
**Consider:**
- Implementing caching
- Adding rate limiting to endpoints
- Upgrading to paid tier for production
## ๐ก Pro Tips
### Keep Service Awake
Free tier services sleep after 15 minutes of inactivity.
**Options:**
1. Use a monitoring service (UptimeRobot, etc.)
2. Upgrade to paid tier ($7/month)
3. Accept the cold start delay
### Optimize Cold Starts
```python
# In server_web.py, add:
import logging
logging.basicConfig(level=logging.INFO)
```
### Add Caching
For better performance, add caching:
```python
from functools import lru_cache
@lru_cache(maxsize=100)
async def cached_fetch(route, params_str):
# Implementation
pass
```
## ๐ Scaling
### When to Upgrade
Consider upgrading when:
- Consistent traffic (avoid sleep)
- Need faster response times
- Require more memory/CPU
- Want custom domains
### Render Plans
- **Free**: $0/month - Perfect for testing
- **Starter**: $7/month - No sleep, better performance
- **Standard**: $25/month - Autoscaling, more resources
## ๐ You're Live!
Your MCP Energy Server is now:
- โ
Deployed on Render.com
- โ
Accessible via HTTPS
- โ
Automatically deploying from GitHub
- โ
Ready to serve energy data
## ๐ Need Help?
- **Render Docs**: https://render.com/docs
- **Your Logs**: Check Render dashboard
- **This Project**: See TROUBLESHOOTING.md
- **GitHub Issues**: Create an issue in your repo
## ๐ Quick Links
- [Your Render Dashboard](https://dashboard.render.com)
- [Render Documentation](https://render.com/docs)
- [EIA API Docs](https://www.eia.gov/opendata/)
---
**Congratulations!** ๐ Your MCP Energy Server is now live on the web!
Share your API URL and start serving energy data to the world! ๐