Skip to content

Logging in lifespan never emmittedΒ #2012

@ion-elgreco

Description

@ion-elgreco

Description

Logging in the lifespan is never emitted. With a fastapi server this doesn't happen.

import logging
from fastmcp import FastMCP
from contextlib import asynccontextmanager


logging.basicConfig(
    level=logging.INFO,
    format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
)
logger = logging.getLogger(__name__)

@asynccontextmanager
async def lifespan(app: FastMCP):  # noqa: ANN201, ARG001
    """Application lifespan handler"""
    logger.info("Starting MCP API...")
    yield
    logger.info("Closing MCP API...")

mcp = FastMCP(
    name="MCP",
    host="0.0.0.0",
    port=8050,
    stateless_http=True,
    lifespan=lifespan,
)

mcp.run(transport="streamable-http", show_banner=False, log_level="INFO")

Example Code

Version Information

FastMCP version:                                                             2.12.4
MCP version:                                                                 1.14.1
Python version:                                                              3.12.9
Platform:                                              macOS-15.6.1-arm64-arm-64bit

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working. Reports of errors, unexpected behavior, or broken functionality.serverRelated to FastMCP server implementation or server-side functionality.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions