The Lifespan Startup Hook
Open connections once when the server boots.
The Lifespan Startup Hook is a free MCP Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the MCP Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Boot Once, Use Many Times
Some setup should happen once when your server boots, like opening a database connection, not on every single tool call. 🚀
Meet the Lifespan
The lifespan is a hook that runs startup code before requests arrive, then runs teardown code after the server stops.
An Async Context Manager
A lifespan is an async context manager: code before yield is startup, and code after yield is shutdown.
from contextlib import asynccontextmanager
@asynccontextmanager
async def lifespan(server):
# startup
yield
# shutdownWire It into FastMCP
You hand your lifespan to the server with the lifespan argument, so FastMCP knows to run it around its life.
from mcp.server.fastmcp import FastMCP
mcp = FastMCP("demo", lifespan=lifespan)Open Expensive Things Here
The startup block is the right home for expensive setup: database pools, HTTP clients, or loading a model into memory.
Yield the Shared State
Whatever you yield becomes the shared context for every request, so build it once and pass it along.
async def lifespan(server):
db = await connect_db()
yield {"db": db}Startup Runs Before Any Tool
FastMCP guarantees your startup finishes before it accepts the first tool call, so resources are ready in time.
A Typed Context, Optionally
You can yield a small dataclass instead of a dict to get typed, autocompletable access to your shared objects.
from dataclasses import dataclass
@dataclass
class AppCtx:
db: objectKeep Startup Fast
Clients wait for startup, so keep it lean: connect what you need and defer slow optional work until it is first used.
One Lifespan Per Server
A server has exactly one lifespan. Group all your startup logic inside it rather than scattering setup across tools.
Handle Startup Failures
If startup raises an error, the server should refuse to start rather than run half-configured and fail mysteriously later.
Quick Check
Where in a lifespan does startup code go relative to yield?
Recap: The Lifespan Hook
You learned the lifespan: an async context manager that runs setup before yield, yields shared state, and tears down after. Next, share it. 🎯
Frequently asked questions
Is the “The Lifespan Startup Hook” lesson free?
Yes — the full text of “The Lifespan Startup Hook” is free to read here on the web, and the MCP Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the MCP Academy course, upgrade to CoddyKit PRO.
What will I learn in “The Lifespan Startup Hook”?
Open connections once when the server boots. You practise MCP Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start MCP Academy?
No prior experience is required. MCP Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “The Lifespan Startup Hook” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this MCP Academy lesson?
Yes. Every MCP Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- The Lifespan Startup Hook
- Share State via App Context
- Access the Context Object
- Clean Shutdown & Resource Release