Send log messages back to MCP clients through the context.
This documentation covers MCP client logging - sending messages from your server to MCP clients. For standard server-side logging (e.g., writing to files, console), use fastmcp.utilities.logging.get_logger() or Python’s built-in logging module.
Server logging allows MCP tools to send debug, info, warning, and error messages back to the client. This provides visibility into function execution and helps with debugging during development and operation.
All logging methods (debug, info, warning, error, log) now accept an extra parameter, which is a dictionary of arbitrary data. This allows you to send structured data to the client, which is useful for creating rich, queryable logs.
Use for potentially harmful situations that don’t prevent execution:
Copy
@mcp.toolasync def validate_config(config: dict, ctx: Context) -> dict: """Validate configuration with warnings for deprecated options.""" if "old_api_key" in config: await ctx.warning( "Using deprecated 'old_api_key' field. Please use 'api_key' instead", extra={"deprecated_field": "old_api_key"} ) if config.get("timeout", 30) > 300: await ctx.warning( "Timeout value is very high (>5 minutes), this may cause issues", extra={"timeout_value": config.get("timeout")} ) return {"status": "valid", "warnings": "see logs"}