Files
mcp-template-python/src/mcp_template_python/server.py
2025-09-18 11:13:23 +08:00

49 lines
1.1 KiB
Python

import contextlib
from fastapi import FastAPI, Response
from .__about__ import __version__
from .app import MCP_MAP
from .config import settings
from .routers.helpers import router as helpers_router
@contextlib.asynccontextmanager
async def lifespan(app: FastAPI):
async with contextlib.AsyncExitStack() as stack:
for mcp in MCP_MAP.values():
await stack.enter_async_context(mcp.session_manager.run())
yield
app = FastAPI(
title=settings.app_title,
description=settings.app_description,
version=__version__,
lifespan=lifespan,
)
@app.get("/")
async def root():
"""Root endpoint."""
return Response("<script>location.href='/docs'</script>")
@app.get("/health")
async def health():
"""Check the health of the server and list available tools."""
return {
"status": "healthy",
}
if settings.enable_helpers_router:
app.include_router(helpers_router)
for name, mcp in MCP_MAP.items():
if settings.enable_sse:
app.mount(f"/{name}/compatible", mcp.sse_app())
if settings.enable_streamable_http:
app.mount(f"/{name}", mcp.streamable_http_app())