Zwracanie wiadomości, nie tylko tekstu
Formatuj wynik promptu jako ustrukturyzowane wiadomości czatu.
Zwracanie wiadomości, nie tylko tekstu to bezpłatna lekcja MCP Academy na CoddyKit. To lekcja 3 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.
Beyond a Single String
A prompt can do more than return one line. It can return a list of messages to seed a richer, multi-turn opener. 🗂️
Messages Have Roles
Each message carries a role, usually user or assistant. Roles let you stage a short back-and-forth before the user even types.
The Message Helpers
The SDK gives you UserMessage and AssistantMessage helpers so you can build chat turns without hand-writing the raw structure.
from mcp.server.fastmcp.prompts import base
@mcp.prompt()
def ask() -> list[base.Message]:
return [base.UserMessage("Help me debug.")]Return a List
To send several turns, return a list of these message objects. The host replays them in order as the conversation's start.
return [
base.UserMessage("Explain this error."),
base.AssistantMessage("Sure, paste it in.")
]A Single String Still Works
Returning a plain string is just shorthand: the SDK wraps it as one user message. Lists give you finer control over the setup.
Stage the Assistant
An AssistantMessage can prime the model's persona or remind it of rules, so the conversation begins already on the right track.
Set Up Context
Use opening messages to plant context, like coding standards or a role, that the model should keep in mind throughout the task.
Mix Arguments In
You can still slot arguments into any message in the list. The user's inputs flow into a structured conversation, not just one line.
@mcp.prompt()
def debug(error: str) -> list[base.Message]:
return [base.UserMessage(f"Fix this error:\n{error}")]Order Matters
The list order is the turn order. Lead with context or a system-style note, then the user's request, so the model reads it naturally.
Why Use Messages
Structured messages shape a better starting point than one blob of text, especially for tasks that need setup before the real ask.
Keep It Lean
A couple of well-placed turns beat many. Each extra message costs tokens, so include only what truly improves the result.
Quick Check
Let's pin down the richer return type.
Recap: Message Output
Well done! Prompts can return a list of role-tagged messages built with UserMessage and AssistantMessage to stage a richer opener. ✅
Często zadawane pytania
Czy lekcja „Zwracanie wiadomości, nie tylko tekstu” jest bezpłatna?
Tak — pełny tekst „Zwracanie wiadomości, nie tylko tekstu” 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 „Zwracanie wiadomości, nie tylko tekstu”?
Formatuj wynik promptu jako ustrukturyzowane wiadomości czatu. Ć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 3 z 4.
Ile czasu zajmuje lekcja „Zwracanie wiadomości, nie tylko tekstu”?
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
- Rejestrowanie pierwszego promptu
- Dodawanie argumentów do promptu
- Zwracanie wiadomości, nie tylko tekstu
- Szablon promptu do przeglądu kodu