Zustand über den App-Kontext teilen
Übergeben Sie gemeinsame Objekte an jeden Handler.
Zustand über den App-Kontext teilen ist eine kostenlose MCP Academy-Lektion auf CoddyKit. Dies ist Lektion 2 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des MCP Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der MCP Academy-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
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. 🧩
Häufig gestellte Fragen
Ist die Lektion „Zustand über den App-Kontext teilen“ kostenlos?
Ja — der vollständige Text von „Zustand über den App-Kontext teilen“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des MCP Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der MCP Academy-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „Zustand über den App-Kontext teilen“?
Übergeben Sie gemeinsame Objekte an jeden Handler. Du übst MCP Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um MCP Academy zu starten?
Keine Vorkenntnisse erforderlich. MCP Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 2 von 4.
Wie lange dauert die Lektion „Zustand über den App-Kontext teilen“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser MCP Academy-Lektion Code schreiben und ausführen?
Ja. Jede MCP Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- Der Startup-Hook für die Lebensdauer
- Zustand über den App-Kontext teilen
- Auf das Kontextobjekt zugreifen
- Sauberes Herunterfahren und Freigeben von Ressourcen