Strukturiertes Logging an den Client
Geben Sie Protokollmeldungen mit verschiedenen Log-Leveln über das Protokoll aus.
Strukturiertes Logging an den Client ist eine kostenlose MCP Academy-Lektion auf CoddyKit. Dies ist Lektion 3 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.
Logs the Client Can See
Printing to your terminal only helps you. MCP lets the server send log messages over the protocol so the host app can show them too.
The message Notification
Logs travel as a notifications/message notification. It carries a level, optional logger name, and the data you want recorded.
{"method": "notifications/message",
"params": {"level": "info", "data": "Indexing started"}}Use the Context
From inside a tool you reach logging through the Context object, the same handle you used for progress. No extra setup is needed.
from mcp.server.fastmcp import Contextctx.info for Updates
The most common helper is ctx.info. Use it for normal, expected status notes that help a user follow what the tool is doing.
await ctx.info("Loaded 120 records from disk")ctx.debug for Detail
Reach for ctx.debug when you want fine-grained traces, like a parsed value or a chosen branch, that are noisy in everyday use.
await ctx.debug(f"chosen strategy = {strategy}")ctx.warning for Surprises
Use ctx.warning when something is off but recoverable, like a missing optional field that you filled with a default value.
await ctx.warning("timeout missing, using 30s default")ctx.error for Failures
Call ctx.error for genuine problems. It tells the client a step failed, separate from raising an exception that ends the call.
await ctx.error("upstream API returned 503")Standard Severity Levels
MCP follows the familiar syslog scale: debug, info, notice, warning, error, critical, and higher. Clients can sort and color by level.
Name Your Logger
Many helpers accept a logger name so messages can be grouped by component, much like Python's standard logging module does.
await ctx.info("cache miss", logger="db")Never Log Secrets
Log messages leave your process and reach the client, so treat them as visible. Never write secrets like tokens or passwords into a log.
Logs Are Notifications
Like progress, each log is a notification, so it never blocks your tool or expects a reply. You can log freely as the work unfolds.
await ctx.info("step 1 done")
await ctx.info("step 2 done")Quick Check
Pick the right helper for routine status updates.
Recap: Logging
You can now emit leveled logs with ctx.info, debug, warning, and error, name your loggers, and keep secrets out. Great progress!
Häufig gestellte Fragen
Ist die Lektion „Strukturiertes Logging an den Client“ kostenlos?
Ja — der vollständige Text von „Strukturiertes Logging an den Client“ 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 „Strukturiertes Logging an den Client“?
Geben Sie Protokollmeldungen mit verschiedenen Log-Leveln über das Protokoll aus. 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 3 von 4.
Wie lange dauert die Lektion „Strukturiertes Logging an den Client“?
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
- Fortschritt bei langen Aufgaben senden
- Abbruchanfragen verarbeiten
- Strukturiertes Logging an den Client
- Log-Level zur Laufzeit festlegen