New in version: 2.0.0
MCP servers can emit log messages to clients. The client can handle these logs through a log handler callback.
Log Handler
Provide alog_handler
function when creating the client. For robust logging, the log messages can be integrated with Python’s standard logging
module.
Handling Structured Logs
Themessage.data
attribute is a dictionary that contains the log payload from the server. This enables structured logging, allowing you to receive rich, contextual information.
The dictionary contains two keys:
msg
: The string log message.extra
: A dictionary containing any extra data sent from the server.
Handler Parameters
Thelog_handler
is called every time a log message is received. It receives a LogMessage
object:
Log Handler Parameters
Default Log Handling
If you don’t provide a customlog_handler
, FastMCP’s default handler routes server logs to the appropriate Python logging levels. The MCP levels are mapped as follows: notice
→ INFO; alert
and emergency
→ CRITICAL. If the server includes a logger name, it is prefixed in the message, and any extra
data is forwarded via the logging extra
parameter.