Skip to content

Lifespan wrapper is not found in the 1.2.1 release #219

@danthekishan

Description

@danthekishan

Describe the bug

Adding lifespan configuration to the FastMCP server does not get initialized or called during startup.
And I checked the source code of the cached package and also files in pypi, and it seems like the lifespan_wrapper and setup is missing in the released version. (and I see them in github main branch)

When testing the example (running the query_db tool) below in dev mode, (mcp dev) getting an error, saying,
Error executing tool query_db: 'RequestContext' object has no attribute 'lifespan_context'

To Reproduce

Steps to reproduce the behavior:

  1. Adding a lifespan to mcp app
@asynccontextmanager
async def app_lifespan(server: FastMCP) -> AsyncIterator[AppContext]:
    """Manage application lifecycle with type-safe context"""
    try:
        # Initialize on startup
        await db.connect()
        yield AppContext(db=db)
    finally:
        # Cleanup on shutdown
        await db.disconnect()

# Pass lifespan to server
mcp = FastMCP("My App", lifespan=app_lifespan)
  1. Add tool to use the Appcontext
@mcp.tool()
def query_db(ctx: Context) -> str:
    """Tool that uses initialized resources"""
    data = ctx.request_context.lifespan_context["data"]
    return data

Expected behavior
Expected lifespan_context to be available or to be initialized during startup

Desktop (please complete the following information):

  • OS: macOS m2 pro (15.2)
  • Browser: chrome

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions