0Pricing
MCP Academy · 课时

向客户端发送结构化日志

通过协议发出分级日志消息。

向客户端发送结构化日志 是 CoddyKit 上的免费 MCP Academy 课时。 这是第 3 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 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!

常见问题解答

「向客户端发送结构化日志」课时是免费的吗?

是的 — 「向客户端发送结构化日志」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 MCP Academy 课程的其余内容,请升级到 CoddyKit PRO。 MCP Academy 课程共包含 4 节课。

「向客户端发送结构化日志」这节课中我会学到什么?

通过协议发出分级日志消息。 你通过在浏览器中直接运行的动手代码来练习 MCP Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 MCP Academy 需要有经验吗?

无需任何先前经验。CoddyKit 上的 MCP Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 4 节。

「向客户端发送结构化日志」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 MCP Academy 课中编写并运行代码吗?

能。每节 MCP Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 为长任务发送进度
  2. 处理取消请求
  3. 向客户端发送结构化日志
  4. 在运行时设置日志级别
← 返回 MCP Academy