MCP Academy · Lesson

Share State via App Context

Pass shared objects into every handler.

State That Every Handler Needs

Your tools often need the same objects, like one database pool. You want to create them once and share them everywhere.

The Yielded Value Is the State

Whatever the lifespan yields becomes your shared application state, available to every tool, resource, and prompt.

async def lifespan(server):
    yield {"db": db, "cache": cache}

All lessons in this course

  1. The Lifespan Startup Hook
  2. Share State via App Context
  3. Access the Context Object
  4. Clean Shutdown & Resource Release
← Back to MCP Academy