[Core] Starlette Wrapper: Health endpoint and ASGI integration #5

Closed
opened 2026-01-01 20:07:05 -06:00 by b3nw · 0 comments
Owner

Overview

Wrap the FastMCP server in a Starlette application per BLUEPRINT.md pattern.

Tasks

  • Create health check endpoint at /health
  • Verify UniFi connection in health check
  • Mount MCP app under root path
  • Configure lifespan management
  • Add uvicorn entry point

Implementation

from starlette.applications import Starlette
from starlette.routing import Route, Mount
from starlette.responses import JSONResponse
import uvicorn

async def health(request):
    try:
        # Verify UniFi connectivity
        await client.ping()
        return JSONResponse({
            "status": "ok",
            "controller": config.host,
            "write_enabled": config.allow_writes
        })
    except Exception as e:
        return JSONResponse(
            {"status": "error", "error": str(e)},
            status_code=503
        )

def create_app():
    mcp_app = mcp.http_app()
    return Starlette(
        routes=[
            Route("/health", health),
            Mount("/", app=mcp_app)
        ],
        lifespan=mcp_app.lifespan
    )

if __name__ == "__main__":
    uvicorn.run(create_app(), host="0.0.0.0", port=8000)

Acceptance Criteria

  • /health returns JSON status
  • /health returns 503 if UniFi unreachable
  • MCP protocol works via SSE/stdio
  • Docker health check works
## Overview Wrap the FastMCP server in a Starlette application per BLUEPRINT.md pattern. ## Tasks - [ ] Create health check endpoint at `/health` - [ ] Verify UniFi connection in health check - [ ] Mount MCP app under root path - [ ] Configure lifespan management - [ ] Add uvicorn entry point ## Implementation ```python from starlette.applications import Starlette from starlette.routing import Route, Mount from starlette.responses import JSONResponse import uvicorn async def health(request): try: # Verify UniFi connectivity await client.ping() return JSONResponse({ "status": "ok", "controller": config.host, "write_enabled": config.allow_writes }) except Exception as e: return JSONResponse( {"status": "error", "error": str(e)}, status_code=503 ) def create_app(): mcp_app = mcp.http_app() return Starlette( routes=[ Route("/health", health), Mount("/", app=mcp_app) ], lifespan=mcp_app.lifespan ) if __name__ == "__main__": uvicorn.run(create_app(), host="0.0.0.0", port=8000) ``` ## Acceptance Criteria - `/health` returns JSON status - `/health` returns 503 if UniFi unreachable - MCP protocol works via SSE/stdio - Docker health check works
b3nw closed this issue 2026-01-01 20:21:24 -06:00
Sign in to join this conversation.
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: b3nw/unifi-mcp-light#5