Skip to content

Conversation

fidemin
Copy link

@fidemin fidemin commented Jul 26, 2025

Description

Summary

Currently, there is no way applying env vars related to logging when executing kubectl_mcp_tool.mcp_server.
To solve, this, this PR introduces an improved logging configuration for the MCP server by adding a new utility function, set_logger_with_envs, in kubectl_mcp_tool/utils/logging_util.py.
This utility enables logging behaviour (log level, output target) to be controlled via environment variables, making the MCP server easier to debug and operate in various environments.


Changes

  • New Utility:

    • Added set_logger_with_envs(logger) in kubectl_mcp_tool/utils/logging_util.py
    • Log level set by MCP_DEBUG (takes priority) or KUBECTL_MCP_LOG_LEVEL
    • Output target can be changed using MCP_LOG_FILE (otherwise logs to stderr)
    • Prevents duplicate log handlers if logger is initialized multiple times
    • Disables propagation to parent loggers to avoid duplicate output
    • Uses consistent format:
      %(asctime)s - %(levelname)s - %(name)s - %(message)s
      
  • MCP Server:

    • Updated to use set_logger_with_envs(logger) for configuration
    • Removed direct basicConfig usage

Motivation

  • Flexibility:
    Logging can be controlled per environment without code changes.
  • Cleaner Startup:
    Avoids duplicate log output from multiple initializations.
  • Operational Ease:
    Easily redirect logs and adjust levels for debugging or production.
  • Technical Debt:
    Moves logger setup into a utility function, preparing for more robust logging improvements.

Test

  • local run : python -m kubectl_mcp_tool.mcp_server
  • custom mcp client with logging file and logging level setting env variable

@fidemin fidemin changed the title set loggers with env vars for mcp-server # feat(logging): Add environment-based logger configuration and cleanup logging init Jul 26, 2025
@fidemin fidemin changed the title # feat(logging): Add environment-based logger configuration and cleanup logging init feat(logging): Add environment-based logger configuration and cleanup logging init Aug 2, 2025
@fidemin
Copy link
Author

fidemin commented Aug 2, 2025

Does anyone check this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant