Skip to content

Lifespan enters and exits for each request when stateless_http=TrueΒ #1304

@savvaki

Description

@savvaki

Initial Checks

Description

When stateless_http=True, the lifespan of the MCP server enters and exits with each request. Surely this is unintended behavior?

Image

Example Code

from mcp.server.fastmcp import Context, FastMCP
from contextlib import asynccontextmanager

@asynccontextmanager
async def tmp_lifespan(server: FastMCP):
    print(f"Starting lifespan...")
    yield
    print(f"Ending lifespan...")

mcp = FastMCP("localmcp", host="127.0.0.1", lifespan=tmp_lifespan, port=8000, stateless_http=True)


# Add an addition tool
@mcp.tool()
async def add(a: int, b: int) -> int:
    """Add two numbers"""
    return a + b

if __name__ == "__main__":
    print(f"Starting MCP server...")
    mcp.run(transport="streamable-http")

Python & MCP Python SDK

1.13.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestv2Ideas, requests and plans for v2 of the SDK which will incorporate major changes and fixes

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions