Skip to main content
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 a log_handler function when creating the client. For robust logging, the log messages can be integrated with Python’s standard logging module.

Handling Structured Logs

The message.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.
This structure is preserved even when logs are forwarded through a FastMCP proxy, making it a powerful tool for debugging complex, multi-server applications.

Handler Parameters

The log_handler is called every time a log message is received. It receives a LogMessage object:

Log Handler Parameters

LogMessage
Log Message Object

Default Log Handling

If you don’t provide a custom log_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.