Something went wrong. Try again.
mirror of github.com/PrefectHQ/fastmcp
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102---title: lifespansidebarTitle: lifespan---
# `fastmcp.server.lifespan`
Composable lifespans for FastMCP servers.
This module provides a `@lifespan` decorator for creating composable server lifespansthat can be combined using the `|` operator.
Example: ```python from fastmcp import FastMCP from fastmcp.server.lifespan import lifespan
@lifespan async def db_lifespan(server): conn = await connect_db() yield {"db": conn} await conn.close()
@lifespan async def cache_lifespan(server): cache = await connect_cache() yield {"cache": cache} await cache.close()
mcp = FastMCP("server", lifespan=db_lifespan | cache_lifespan) ```
To compose with existing `@asynccontextmanager` lifespans, wrap them explicitly:
```python from contextlib import asynccontextmanager from fastmcp.server.lifespan import lifespan, ContextManagerLifespan
@asynccontextmanager async def legacy_lifespan(server): yield {"legacy": True}
@lifespan async def new_lifespan(server): yield {"new": True}
# Wrap the legacy lifespan explicitly combined = ContextManagerLifespan(legacy_lifespan) | new_lifespan ```
## Functions
### `lifespan` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/lifespan.py#L172" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythonlifespan(fn: LifespanFn) -> Lifespan```
Decorator to create a composable lifespan.
Use this decorator on an async generator function to make it composablewith other lifespans using the `|` operator.
**Args:**- `fn`: An async generator function that takes a FastMCP server and yieldsa dict for the lifespan context.
**Returns:**- A composable Lifespan wrapper.
## Classes
### `Lifespan` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/lifespan.py#L61" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
Composable lifespan wrapper.
Wraps an async generator function and enables composition via the `|` operator.The wrapped function should yield a dict that becomes part of the lifespan context.
### `ContextManagerLifespan` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/lifespan.py#L110" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
Lifespan wrapper for already-wrapped context manager functions.
Use this for functions already decorated with @asynccontextmanager.
### `ComposedLifespan` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/lifespan.py#L137" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
Two lifespans composed together.
Enters the left lifespan first, then the right. Exits in reverse order.Results are shallow-merged into a single dict.