Tworzenie zawartości podczas odczytu
Obliczaj treść zasobu w chwili żądania.
Tworzenie zawartości podczas odczytu to bezpłatna lekcja MCP Academy na CoddyKit. To lekcja 2 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.
Lazy, Not Eager
A dynamic resource doesn't store a fixed body. Its content is computed on demand, only when a client actually asks to read it. ⚡
The Read Triggers the Work
Your resource function runs at the moment of a read request. Nothing happens until a client reads it, so you never waste effort building content no one wants.
Return the Body
Whatever your function returns becomes the resource's content. Return a string and the client receives that exact text as the resource body.
@mcp.resource("time://now")
def now() -> str:
return datetime.now().isoformat()Fresh Every Read
Because the body is rebuilt each time, the model always sees current data. Read the clock twice and you get two different timestamps.
Use the URI Variables
Combine read-time building with a template. The captured parameters shape what you compute, so the same function serves tailored content per URI.
@mcp.resource("greeting://{name}")
def greet(name: str) -> str:
return f"Hello, {name}!"Pull From Anywhere
At read time you can do real work: query a database, call an API, or read a file. The result of that work is what you return as the body.
@mcp.resource("stats://summary")
def summary() -> str:
return db.fetch_summary()Async When You Wait
If building content means waiting on I/O, make the function async. The server can serve other reads while yours awaits a slow network call.
@mcp.resource("feed://latest")
async def latest() -> str:
return await fetch_feed()Shape the Returned Text
You decide the format of the body. Returning JSON text is common when the model needs structured data it can reason over cleanly.
import json
def report() -> str:
return json.dumps({"ok": True})Keep It Quick
A read should feel snappy. If the work is heavy, cache the result so repeated reads don't redo expensive computation every single time.
Handle Missing Data
What if the requested thing doesn't exist? Raise an error from the function so the client learns the resource couldn't be read, rather than returning junk.
def get_doc(doc_id: str) -> str:
if doc_id not in store:
raise ValueError("not found")
return store[doc_id]Why This Matters
Read-time building turns a static idea into a live window on your data. The model always reads what is true right now, not a stale snapshot. 🎯
Quick Check
When does the body of a dynamic resource get produced?
Recap
You saw how returning a value from a resource function builds content on read, giving the model fresh, computed data every time. Next: listed vs templated resources. 🚀
Często zadawane pytania
Czy lekcja „Tworzenie zawartości podczas odczytu” jest bezpłatna?
Tak — pełny tekst „Tworzenie zawartości podczas odczytu” 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 „Tworzenie zawartości podczas odczytu”?
Obliczaj treść zasobu w chwili żądania. Ć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 2 z 4.
Ile czasu zajmuje lekcja „Tworzenie zawartości podczas odczytu”?
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
- Parametryzowane identyfikatory URI zasobów
- Tworzenie zawartości podczas odczytu
- Zasoby wyliczane a szablonowe
- Powiadamianie klientów o aktualizacjach