Spaces:
Sleeping
Sleeping
File size: 6,452 Bytes
0f6d44d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 | """
EcoMCP Modal Deployment
Deploy MCP server to Modal serverless platform
Run: modal deploy deploy_modal.py
"""
import os
import json
import asyncio
from typing import Dict, Any
try:
import modal
except ImportError:
raise ImportError("Install modal: pip install modal")
# Create Modal app
app = modal.App("ecomcp-server")
# Import the server
from ecomcp_server_refined import EcoMCPServerRefined
# Create server instance
server = None
def get_server():
"""Get or create server instance"""
global server
if server is None:
server = EcoMCPServerRefined()
return server
# ============================================================================
# HTTP API ENDPOINT
# ============================================================================
@app.function(
image=modal.Image.debian_slim().pip_install(
"httpx>=0.25.0",
"python-dotenv>=1.0.0",
"openai>=1.0.0"
),
secrets=[modal.Secret.from_name("openai-api-key")]
)
@modal.web_endpoint(method="POST")
async def process_request(request: Dict[str, Any]) -> Dict:
"""
Process MCP request via HTTP
Example request:
{
"method": "tools/call",
"params": {
"name": "analyze_product",
"arguments": {"name": "Headphones"}
}
}
"""
server = get_server()
# Ensure OPENAI_API_KEY is set from modal secret
if not os.getenv("OPENAI_API_KEY"):
return {"error": "OPENAI_API_KEY not configured"}
try:
# Build JSON-RPC message
message = {
"jsonrpc": "2.0",
"method": request.get("method"),
"params": request.get("params", {}),
"id": 1
}
# Process message
response = await server.process_message(message)
return response
except Exception as e:
return {
"jsonrpc": "2.0",
"error": {"code": -32603, "message": str(e)},
"id": 1
}
# ============================================================================
# INDIVIDUAL TOOL ENDPOINTS
# ============================================================================
@app.function(
image=modal.Image.debian_slim().pip_install(
"httpx>=0.25.0",
"python-dotenv>=1.0.0",
"openai>=1.0.0"
),
secrets=[modal.Secret.from_name("openai-api-key")]
)
@modal.web_endpoint(method="POST")
async def analyze_product(request: Dict[str, Any]) -> Dict:
"""Analyze product endpoint"""
server = get_server()
try:
result = await server.call_tool("analyze_product", request)
return result
except Exception as e:
return {"status": "error", "error": str(e)}
@app.function(
image=modal.Image.debian_slim().pip_install(
"httpx>=0.25.0",
"python-dotenv>=1.0.0",
"openai>=1.0.0"
),
secrets=[modal.Secret.from_name("openai-api-key")]
)
@modal.web_endpoint(method="POST")
async def analyze_reviews(request: Dict[str, Any]) -> Dict:
"""Analyze reviews endpoint"""
server = get_server()
try:
result = await server.call_tool("analyze_reviews", request)
return result
except Exception as e:
return {"status": "error", "error": str(e)}
@app.function(
image=modal.Image.debian_slim().pip_install(
"httpx>=0.25.0",
"python-dotenv>=1.0.0",
"openai>=1.0.0"
),
secrets=[modal.Secret.from_name("openai-api-key")]
)
@modal.web_endpoint(method="POST")
async def generate_listing(request: Dict[str, Any]) -> Dict:
"""Generate listing endpoint"""
server = get_server()
try:
result = await server.call_tool("generate_listing", request)
return result
except Exception as e:
return {"status": "error", "error": str(e)}
@app.function(
image=modal.Image.debian_slim().pip_install(
"httpx>=0.25.0",
"python-dotenv>=1.0.0",
"openai>=1.0.0"
),
secrets=[modal.Secret.from_name("openai-api-key")]
)
@modal.web_endpoint(method="POST")
async def price_recommendation(request: Dict[str, Any]) -> Dict:
"""Price recommendation endpoint"""
server = get_server()
try:
result = await server.call_tool("price_recommendation", request)
return result
except Exception as e:
return {"status": "error", "error": str(e)}
@app.function(
image=modal.Image.debian_slim().pip_install(
"httpx>=0.25.0",
"python-dotenv>=1.0.0",
"openai>=1.0.0"
),
secrets=[modal.Secret.from_name("openai-api-key")]
)
@modal.web_endpoint(method="POST")
async def competitor_analysis(request: Dict[str, Any]) -> Dict:
"""Competitor analysis endpoint"""
server = get_server()
try:
result = await server.call_tool("competitor_analysis", request)
return result
except Exception as e:
return {"status": "error", "error": str(e)}
# ============================================================================
# HEALTH CHECK
# ============================================================================
@app.function(
image=modal.Image.debian_slim().pip_install(
"httpx>=0.25.0",
"python-dotenv>=1.0.0"
)
)
@modal.web_endpoint(method="GET")
async def health() -> Dict:
"""Health check endpoint"""
server = get_server()
return {
"status": "healthy",
"server": "ecomcp-v2.0",
"cache_stats": server.cache.stats(),
"metrics": server.metrics
}
if __name__ == "__main__":
print("""
============================================================
EcoMCP Modal Deployment
============================================================
Deploy to Modal:
$ modal deploy deploy_modal.py
This creates serverless endpoints for:
- /process_request - Main MCP endpoint
- /analyze_product - Product analysis
- /analyze_reviews - Review analysis
- /generate_listing - Listing generation
- /price_recommendation - Pricing strategy
- /competitor_analysis - Competitive analysis
- /health - Health check
Setup:
1. Install Modal: pip install modal
2. Authenticate: modal token new
3. Create secret: modal secret create openai-api-key --value sk-...
4. Deploy: modal deploy deploy_modal.py
============================================================
""")
|