-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Closed
Description
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:
- 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)- 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 dataExpected 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
Labels
No labels