Skip to content

MCP Specification · 2026-07-28 · zh-TW

記錄

Logging · Deprecated

Server 向 client 傳送 request-scoped structured logs 的既有功能;2026-07-28 已棄用。

已棄用(Deprecated)

Logging 自 MCP 2026-07-28 起已棄用。新實作 SHOULD NOT(不應)採用;既有實作 SHOULD(應該)遷移:stdio transport 改寫到 stderr,結構化 observability 則改用 OpenTelemetry。

既有 MCP Logging 讓 server 向 client 傳送 structured log notifications。Client 以每次 request 的 _meta 控制 verbosity;server 送 severity、logger name 與任意 JSON-serializable data。Protocol 不強制特定 logging UI。

能力宣告(Capabilities)

會送 log notifications 的 server MUST(必須)宣告:

{
  "capabilities": {
    "logging": {}
  }
}

記錄層級(Log Levels)

沿用 RFC 5424 syslog severity:

Level意義
debug詳細 debugging。
info一般資訊。
notice正常但重要事件。
warning警告條件。
error錯誤條件。
critical關鍵故障。
alert需要立即處置。
emergency系統不可用。

每次請求的記錄層級(Per-request Log Level)

Client 若要收到某 request 的 logs,在該 request _meta 加上 io.modelcontextprotocol/logLevel。沒有這個欄位時,server MUST NOT(不得)為該 request 發 notifications/message

有設定時,server MAY(可以)在該 request 的 response stream、final response 之前送出 requested level 或更嚴重的 logs。這些 notifications 是 request-scoped,MUST NOT(不得)送到 subscriptions/listen 或其他 stream。

記錄通知(Log Notification)

{
  "jsonrpc": "2.0",
  "method": "notifications/message",
  "params": {
    "level": "error",
    "logger": "database",
    "data": {
      "error": "Connection failed"
    }
  }
}

錯誤處理(Error Handling)

io.modelcontextprotocol/logLevel 不是已知 log level,server SHOULD(應該)以標準 JSON-RPC error 拒絕,例如 -32602 Invalid params;internal error 使用 -32603 Internal error

實作考量(Implementation Considerations)

Server SHOULD(應該)

  • rate limit log messages。
  • data 中加入與事件相關的 context。
  • 使用一致的 logger names。
  • 移除敏感資訊。

Client MAY(可以)把 logs 顯示在 UI、提供 filtering / search、以視覺方式呈現 severity,或持久化 log messages。

安全考量(Security)

Log messages MUST NOT(不得)包含 credentials、secrets、PII 或可協助攻擊者的 internal system details。

Implementation SHOULD(應該)rate limit messages、驗證所有 data fields、控制 log access,並監控是否意外包含敏感內容。