feat: reintegrate MCP server with proper session manager lifecycle, nginx /mcp proxy, docker.sock mount
This commit is contained in:
@@ -11,6 +11,7 @@ from app.cache import cache
|
||||
from app.routers import equipment, health, admin, rental_requests, contact
|
||||
from app.services.sync_service import SyncService
|
||||
from app.services.email_service import EmailService
|
||||
from app.mcp_server import get_mcp_app, mcp_session_manager
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
settings = get_settings()
|
||||
@@ -41,6 +42,9 @@ async def lifespan(app: FastAPI):
|
||||
await init_db()
|
||||
await cache.connect()
|
||||
|
||||
_mcp_cm = mcp_session_manager.run()
|
||||
await _mcp_cm.__aenter__()
|
||||
|
||||
scheduler.add_job(run_equipment_sync, trigger="cron", hour="*/6", id="equipment_sync", replace_existing=True)
|
||||
scheduler.add_job(run_email_retry, trigger="cron", minute="*/15", id="email_retry", replace_existing=True)
|
||||
scheduler.start()
|
||||
@@ -49,6 +53,7 @@ async def lifespan(app: FastAPI):
|
||||
yield
|
||||
|
||||
scheduler.shutdown(wait=False)
|
||||
await _mcp_cm.__aexit__(None, None, None)
|
||||
await cache.disconnect()
|
||||
await engine.dispose()
|
||||
|
||||
@@ -63,3 +68,5 @@ app.include_router(health.router)
|
||||
app.include_router(admin.router)
|
||||
app.include_router(rental_requests.router)
|
||||
app.include_router(contact.router)
|
||||
|
||||
app.mount("/mcp", get_mcp_app())
|
||||
|
||||
@@ -122,20 +122,7 @@ _STATIC_COMPONENTS = [
|
||||
# streamable_http_path="/" so that when mounted at /mcp the endpoint is /mcp/
|
||||
# stateless_http=True: each request is independent (no session management)
|
||||
# Disable DNS rebinding protection for Docker deployment
|
||||
try:
|
||||
from mcp.server.fastmcp.settings import TransportSecuritySettings
|
||||
_ts = TransportSecuritySettings(enable_dns_rebinding_protection=False)
|
||||
except Exception:
|
||||
_ts = None
|
||||
|
||||
_mcp_kwargs: dict[str, Any] = {
|
||||
"streamable_http_path": "/",
|
||||
"stateless_http": True,
|
||||
}
|
||||
if _ts is not None:
|
||||
_mcp_kwargs["transport_security"] = _ts
|
||||
|
||||
mcp = FastMCP("hms-cms", **_mcp_kwargs)
|
||||
mcp = FastMCP("hms-cms", streamable_http_path="/", stateless_http=True)
|
||||
|
||||
# ---------------------------------------------------------------------------#
|
||||
# Bearer token authentication wrapper
|
||||
|
||||
@@ -55,6 +55,7 @@ services:
|
||||
volumes:
|
||||
- ./:/data/repo
|
||||
- ./images:/data/images
|
||||
- /var/run/docker.sock:/var/run/docker.sock
|
||||
restart: unless-stopped
|
||||
healthcheck:
|
||||
test:
|
||||
|
||||
@@ -12,6 +12,18 @@ server {
|
||||
proxy_set_header X-Forwarded-Proto $scheme;
|
||||
}
|
||||
|
||||
|
||||
location /mcp {
|
||||
proxy_pass http://backend:8000/mcp;
|
||||
proxy_set_header Host $host;
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||
proxy_set_header X-Forwarded-Proto $scheme;
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Upgrade $http_upgrade;
|
||||
proxy_set_header Connection "upgrade";
|
||||
}
|
||||
|
||||
location / {
|
||||
try_files $uri $uri/ /index.html;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user