0Pricing
MCP Academy · Lekcja

Mechanizm uruchamiania Lifespan

Otwieraj połączenia jednokrotnie podczas uruchamiania serwera.

Mechanizm uruchamiania Lifespan to bezpłatna lekcja MCP Academy na CoddyKit. To lekcja 1 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej MCP Academy, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs MCP Academy zawiera 4 lekcji w sumie.

Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.

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
    # shutdown

Wire 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: object

Keep 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. 🎯

Często zadawane pytania

Czy lekcja „Mechanizm uruchamiania Lifespan” jest bezpłatna?

Tak — pełny tekst „Mechanizm uruchamiania Lifespan” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu MCP Academy, przejdź na CoddyKit PRO. Kurs MCP Academy zawiera 4 lekcji w sumie.

Co nauczysz się w „Mechanizm uruchamiania Lifespan”?

Otwieraj połączenia jednokrotnie podczas uruchamiania serwera. Ćwiczysz MCP Academy z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.

Czy potrzebuję doświadczenia, aby zacząć MCP Academy?

Nie wymagamy żadnego doświadczenia. MCP Academy w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 1 z 4.

Ile czasu zajmuje lekcja „Mechanizm uruchamiania Lifespan”?

Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.

Czy mogę pisać i uruchamiać kod w tej lekcji MCP Academy?

Tak. Każda lekcja MCP Academy zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.

Wszystkie lekcje w tym kursie

  1. Mechanizm uruchamiania Lifespan
  2. Udostępnianie stanu przez kontekst aplikacji
  3. Uzyskiwanie dostępu do obiektu kontekstu
  4. Czyste zamykanie i zwalnianie zasobów
← Powrót do MCP Academy