Share State via App Context
Pass shared objects into every handler.
Share State via App Context is a free MCP Academy lesson on CoddyKit — lesson 2 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.
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}Prefer a Typed Container
Wrapping state in a dataclass gives you clear field names and editor autocomplete instead of guessing dict keys.
@dataclass
class AppContext:
db: DatabaseBuild State in Startup
Inside the lifespan, construct each shared object and yield the container so it lives for the whole server run.
async def lifespan(server):
db = await Database.connect()
yield AppContext(db=db)One Source of Truth
Shared state means one source of truth: every handler sees the same connection, so you avoid duplicate, leaking pools.
Don't Use Globals
Skip module-level globals for connections. Passing state through context keeps tools testable and avoids hidden coupling.
State Is Per Server, Not Per Call
App context is created once at boot, not rebuilt per request, so its objects persist across many tool calls.
Reach It Through the Request
Handlers do not import the state directly. They receive a context object at call time that exposes the yielded value.
Mix Connections and Config
Your shared container can hold more than clients: stash config like API base URLs or feature flags loaded at startup.
@dataclass
class AppContext:
db: Database
base_url: strKeep It Immutable-ish
Treat shared state as mostly read-only. Mutating it from many concurrent calls invites subtle, hard-to-trace bugs.
Lifespan Owns the Lifecycle
Because the lifespan creates the state, it also owns tearing it down, so creation and cleanup stay in one tidy place.
Quick Check
What becomes the shared application state your handlers can use?
Recap: Sharing State
You build shared objects once in the lifespan, yield a typed container, and let every handler read it. Next: how to reach it. 🧩
Frequently asked questions
Is the “Share State via App Context” lesson free?
Yes — the full text of “Share State via App Context” 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 “Share State via App Context”?
Pass shared objects into every handler. 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 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Share State via App Context” 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