アプリコンテキストで状態を共有する
共有オブジェクトをすべてのハンドラーに渡します。
「アプリコンテキストで状態を共有する」はCoddyKit上の無料MCP Academyレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはMCP Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 MCP Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
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. 🧩
よくある質問
「アプリコンテキストで状態を共有する」レッスンは無料ですか?
はい。「アプリコンテキストで状態を共有する」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、MCP Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 MCP Academyコースには全4レッスンが含まれています。
「アプリコンテキストで状態を共有する」で何を学びますか?
共有オブジェクトをすべてのハンドラーに渡します。 ブラウザで直接実行するハンズオンコードでMCP Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
MCP Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのMCP Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/4です。
「アプリコンテキストで状態を共有する」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このMCP Academyレッスンでコードを書いて実行できますか?
はい。すべてのMCP Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- Lifespanの起動フック
- アプリコンテキストで状態を共有する
- コンテキストオブジェクトにアクセスする
- 正常な終了とリソース解放