import contextlib from fastapi import FastAPI from fastapi.responses import HTMLResponse from echo_server import mcp as echo_mcp from math_server import mcp as math_mcp import os # Create a combined lifespan to manage both session managers @contextlib.asynccontextmanager async def lifespan(app: FastAPI): async with contextlib.AsyncExitStack() as stack: await stack.enter_async_context(echo_mcp.session_manager.run()) await stack.enter_async_context(math_mcp.session_manager.run()) yield app = FastAPI(lifespan=lifespan) @app.get("/", response_class=HTMLResponse) async def index(): return """
This FastAPI app demonstrates how to host multiple Model Context Protocol (MCP) servers on a single server instance.
The following MCP servers are mounted under this app:
Use these routes as sub-apps or as examples for adding more MCP servers.