Skip to content

Fix MCP CLI logging: apply colors and respect config log-level#3508

Open
anushakolan wants to merge 4 commits intomainfrom
dev/anushakolan/fix-mcp-cli-logging
Open

Fix MCP CLI logging: apply colors and respect config log-level#3508
anushakolan wants to merge 4 commits intomainfrom
dev/anushakolan/fix-mcp-cli-logging

Conversation

@anushakolan
Copy link
Copy Markdown
Contributor

Why make this change?

Two bugs were discovered in the CLI logging behavior when running DAB in MCP stdio mode:

  1. Missing colors: When start --mcp-stdio --LogLevel <level> was used, the CLI's own startup logs (e.g., "Setting minimum LogLevel: Information.") appeared on stderr without any colors, while engine logs were properly colored. This created an inconsistent visual experience.
  2. Missing CLI logs with config-only log level: When start --mcp-stdio was used with the runtime.telemetry.log-level set in the config file (and no --LogLevel CLI flag), CLI startup logs were completely suppressed, even though the user had clearly expressed an intent to see logs at that level.

What is this change?

The CLI logger (CustomLoggerProvider) only knew about CLI overrides (--LogLevel). It never learned that the runtime config file can also explicitly request a log level. As a result:

  • It defaulted to LogLevel.None (suppress all) whenever --LogLevel was absent in MCP mode, ignoring the config.
  • The MCP stderr code path also skipped the foreground/background color formatting that the non-MCP path applied.

This change makes the CLI logger aware of both override sources and applies colors consistently in both stdout (non-MCP) and stderr (MCP) paths.

Modified files:

  • src/Cli/Utils.cs — Added IsLogLevelOverriddenByConfig (bool) and ConfigLogLevel (LogLevel) static properties.
  • src/Cli/ConfigGenerator.cs — In TryStartEngineWithOptions(), sets the new properties when RuntimeConfig.HasExplicitLogLevel() is true.
  • src/Cli/CustomLoggerProvider.cs:
    • Constructor: chooses minimum log level from CLI → Config → None.
    • Log(): suppresses only when neither CLI nor config set a level; applies foreground/background colors before writing the abbreviation to stderr in MCP mode.

How was this tested?

Manually tested all combinations of MCP / non-MCP × CLI override / config override / no override:

# Scenario Expected Result
1 MCP, no flags, no config log-level No CLI logs (clean stdout)
2 MCP + --LogLevel Information CLI logs on stderr with green info:
3 MCP + config log-level only CLI logs on stderr with green info: ✅ Bug 2 fixed
4 MCP + --LogLevel Warning + config log-level: Information CLI wins → no info: shown
5 Non-MCP mode Unchanged behavior
6 MCP + --LogLevel Warning (StaticWebApps config) Yellow warn: shown ✅ Bug 1 fixed

Note

This is a follow-up bug fix on top of PR #3419 (MCP Set Log Level) and PR #3484 (MCP notifications/message). It targets main.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes CLI logging behavior when running Data API Builder in MCP stdio mode by (1) ensuring CLI startup logs respect an explicitly configured runtime.telemetry.log-level even when no --LogLevel flag is provided, and (2) applying the same color formatting for CLI log prefixes when writing to stderr in MCP mode.

Changes:

  • Added CLI globals to track whether the runtime config explicitly overrides log level, and what that level is (Utils.IsLogLevelOverriddenByConfig, Utils.ConfigLogLevel).
  • Updated CLI startup/config loading to set these globals when RuntimeConfig.HasExplicitLogLevel() is true.
  • Updated CustomLoggerProvider to use CLI → config → none precedence in MCP stdio mode and to apply color formatting for stderr output.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
src/Cli/Utils.cs Adds static state to track config-based log level overrides for CLI logging behavior.
src/Cli/ConfigGenerator.cs Detects explicit log level in config and sets CLI globals so MCP stdio mode can emit CLI logs accordingly.
src/Cli/CustomLoggerProvider.cs Implements CLI/config precedence for MCP stdio mode and applies colors consistently when logging to stderr.

Comment thread src/Cli/ConfigGenerator.cs
Comment thread src/Cli/CustomLoggerProvider.cs
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.

[Bug] MCP stdio mode: CLI logs lack colors and are suppressed when log level is set via config

4 participants