diff --git a/docs/servers/server.mdx b/docs/servers/server.mdx index 85891b5d1..8645691f1 100644 --- a/docs/servers/server.mdx +++ b/docs/servers/server.mdx @@ -362,8 +362,8 @@ print(fastmcp.settings.strict_input_validation) # Default: False Common global settings include: - **`log_level`**: Logging level ("DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"), set with `FASTMCP_LOG_LEVEL` -- **`debug`**: Global debug mode that sets log level to DEBUG and enables Starlette debug tracebacks for HTTP/SSE transports, set with `FASTMCP_DEBUG`. Provides a convenient way to enable comprehensive debugging. For granular control, use `log_level` and `starlette_debug` separately -- **`starlette_debug`**: Enable Starlette debug mode for HTTP/SSE transports only, set with `FASTMCP_STARLETTE_DEBUG`. When enabled, detailed error tracebacks are returned in HTTP responses. Only affects HTTP/SSE transports; has no effect on stdio transport +- **`debug`**: Global debug mode that sets log level to DEBUG and enables Starlette debug tracebacks for HTTP/SSE transports, set with `FASTMCP_DEBUG`. Provides a convenient way to enable comprehensive debugging. For granular control, use `log_level` and `starlette_debug` separately. **WARNING**: Only enable in development - debug tracebacks expose detailed internal information +- **`starlette_debug`**: Enable Starlette debug mode for HTTP/SSE transports only, set with `FASTMCP_STARLETTE_DEBUG`. When enabled, detailed error tracebacks are returned in HTTP responses. Only affects HTTP/SSE transports; has no effect on stdio transport. **WARNING**: Only enable in development - exposes detailed internal information in HTTP responses - **`mask_error_details`**: Whether to hide detailed error information from clients, set with `FASTMCP_MASK_ERROR_DETAILS` - **`strict_input_validation`**: Controls tool input validation mode (default: False for flexible coercion), set with `FASTMCP_STRICT_INPUT_VALIDATION`. See [Input Validation Modes](/servers/tools#input-validation-modes) - **`env_file`**: Path to the environment file to load settings from (default: ".env"), set with `FASTMCP_ENV_FILE`. Useful when your project uses a `.env` file with syntax incompatible with python-dotenv diff --git a/src/fastmcp/settings.py b/src/fastmcp/settings.py index fd26cd89b..2e61d47b7 100644 --- a/src/fastmcp/settings.py +++ b/src/fastmcp/settings.py @@ -290,27 +290,29 @@ class Settings(BaseSettings): @field_validator("debug") @classmethod - def _update_log_level_for_debug(cls, v: bool, info) -> bool: + def _update_log_level_for_debug(cls, v: bool) -> bool: """When debug is enabled, set log_level to DEBUG.""" - if v: - # When debug is True, we need to ensure log_level is set to DEBUG - # This is checked in model_post_init - pass return v - def model_post_init(self, __context) -> None: + def model_post_init(self, __context: Any) -> None: """Post-initialization hook to handle debug mode.""" if self.debug and self.log_level != "DEBUG": # When debug is enabled, force log_level to DEBUG self.log_level = "DEBUG" # Reconfigure logging if it's enabled + # Only reconfigure if fastmcp module is fully initialized + # to avoid AttributeError during import if self.log_enabled: - from fastmcp.utilities.logging import configure_logging + import fastmcp - configure_logging( - level=self.log_level, - enable_rich_tracebacks=self.enable_rich_tracebacks, - ) + # Check if fastmcp.settings exists (module fully initialized) + if hasattr(fastmcp, "settings"): + from fastmcp.utilities.logging import configure_logging + + configure_logging( + level=self.log_level, + enable_rich_tracebacks=self.enable_rich_tracebacks, + ) # error handling mask_error_details: Annotated[ diff --git a/tests/test_debug_settings.py b/tests/test_debug_settings.py index 49d024580..6e7111568 100644 --- a/tests/test_debug_settings.py +++ b/tests/test_debug_settings.py @@ -2,9 +2,6 @@ import logging -import pytest - -from fastmcp import settings as global_settings from fastmcp.settings import Settings from fastmcp.utilities.logging import get_logger @@ -47,7 +44,7 @@ class TestDebugSettings: def test_debug_reconfigures_logging(self): """Test that enabling debug reconfigures logging.""" # Create a settings instance with debug enabled - settings = Settings(debug=True, log_enabled=True) + _settings = Settings(debug=True, log_enabled=True) # Verify logging was reconfigured logger = get_logger("test")