0Pricing
MCP Academy · レッスン

クライアントへの構造化ログ

プロトコル経由で、レベル付きのログメッセージを出力します。

「クライアントへの構造化ログ」はCoddyKit上の無料MCP Academyレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはMCP Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 MCP Academyコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

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 Context

ctx.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!

よくある質問

「クライアントへの構造化ログ」レッスンは無料ですか?

はい。「クライアントへの構造化ログ」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、MCP Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 MCP Academyコースには全4レッスンが含まれています。

「クライアントへの構造化ログ」で何を学びますか?

プロトコル経由で、レベル付きのログメッセージを出力します。 ブラウザで直接実行するハンズオンコードでMCP Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

MCP Academyを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのMCP Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン3/4です。

「クライアントへの構造化ログ」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このMCP Academyレッスンでコードを書いて実行できますか?

はい。すべてのMCP Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. 長時間タスクの進捗を送信する
  2. キャンセルリクエストを処理する
  3. クライアントへの構造化ログ
  4. 実行時にログレベルを設定する
← MCP Academyに戻る