-
Notifications
You must be signed in to change notification settings - Fork 334
Fix MCP CLI logging: apply colors and respect config log-level #3508
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
anushakolan
wants to merge
4
commits into
main
Choose a base branch
from
dev/anushakolan/fix-mcp-cli-logging
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+197
−56
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
fc99545
Fix MCP CLI logging: apply colors and respect config log-level
anushakolan f7ac972
Reset config log-level override flag and add MCP logger tests
anushakolan 8451801
Merge branch 'main' into dev/anushakolan/fix-mcp-cli-logging
anushakolan 9019ecb
Merge branch 'main' into dev/anushakolan/fix-mcp-cli-logging
Aniruddh25 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,14 +28,18 @@ public class CustomConsoleLogger : ILogger | |
| private readonly LogLevel _minimumLogLevel; | ||
|
|
||
| // Minimum LogLevel for CLI output. | ||
| // For MCP mode: use CLI's --LogLevel if specified, otherwise suppress all. | ||
| // For non-MCP mode: always use Information. | ||
| // For MCP mode: prefer CLI's --LogLevel, fall back to config's log-level, otherwise suppress all. | ||
| // For non-MCP mode: always use the level passed to the constructor. | ||
| // Note: --LogLevel is meant for the ENGINE's log level, not CLI's output. | ||
| public CustomConsoleLogger(LogLevel minimumLogLevel = LogLevel.Information) | ||
| { | ||
| _minimumLogLevel = Cli.Utils.IsMcpStdioMode | ||
| ? (Cli.Utils.IsLogLevelOverriddenByCli ? Cli.Utils.CliLogLevel : LogLevel.None) | ||
| : minimumLogLevel; | ||
| ? (Cli.Utils.IsLogLevelOverriddenByCli | ||
| ? Cli.Utils.CliLogLevel | ||
| : Cli.Utils.IsLogLevelOverriddenByConfig | ||
| ? Cli.Utils.ConfigLogLevel | ||
| : LogLevel.None) | ||
| : minimumLogLevel; | ||
| } | ||
|
|
||
| // Color values based on LogLevel | ||
|
|
@@ -89,16 +93,17 @@ public CustomConsoleLogger(LogLevel minimumLogLevel = LogLevel.Information) | |
| /// <summary> | ||
| /// Creates Log message by setting console message color based on LogLevel. | ||
| /// In MCP stdio mode: | ||
| /// - If user explicitly set --LogLevel: write to stderr (colored output) | ||
| /// - If user explicitly set --LogLevel (CLI) or log-level (config): write to stderr (colored output) | ||
| /// - Otherwise: suppress entirely to keep stdout clean for JSON-RPC protocol. | ||
| /// </summary> | ||
| public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter) | ||
| { | ||
| // In MCP stdio mode, only output logs if user explicitly requested a log level. | ||
| // In MCP stdio mode, only output logs if user explicitly requested a log level | ||
| // via either the CLI --LogLevel flag or the runtime config file's log-level. | ||
| // In that case, write to stderr to keep stdout clean for JSON-RPC. | ||
| if (Cli.Utils.IsMcpStdioMode) | ||
| { | ||
| if (!Cli.Utils.IsLogLevelOverriddenByCli) | ||
| if (!Cli.Utils.IsLogLevelOverriddenByCli && !Cli.Utils.IsLogLevelOverriddenByConfig) | ||
| { | ||
| return; // Suppress entirely when no explicit log level | ||
| } | ||
|
anushakolan marked this conversation as resolved.
|
||
|
|
@@ -116,7 +121,15 @@ public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Except | |
|
|
||
| // In MCP stdio mode, stdout is reserved for JSON-RPC protocol messages. | ||
| // Logs must go to stderr to avoid corrupting the MCP communication channel. | ||
| Console.Error.WriteLine($"{mcpAbbreviation}: {formatter(state, exception)}"); | ||
| // Apply colors so the abbreviation matches the visual style of engine logs. | ||
| ConsoleColor mcpOriginalForeGroundColor = Console.ForegroundColor; | ||
| ConsoleColor mcpOriginalBackGroundColor = Console.BackgroundColor; | ||
| Console.ForegroundColor = _logLevelToForeGroundConsoleColorMap.GetValueOrDefault(logLevel, ConsoleColor.White); | ||
| Console.BackgroundColor = _logLevelToBackGroundConsoleColorMap.GetValueOrDefault(logLevel, ConsoleColor.Black); | ||
| Console.Error.Write($"{mcpAbbreviation}:"); | ||
| Console.ForegroundColor = mcpOriginalForeGroundColor; | ||
| Console.BackgroundColor = mcpOriginalBackGroundColor; | ||
| Console.Error.WriteLine($" {formatter(state, exception)}"); | ||
|
Comment on lines
+124
to
+132
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should the original foreground/background color be restored to default after the specific scenario exits? also, what if there is an unknown exception and should the colour be restored back to default? |
||
| return; | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in these tests, does CaptureConsole() capture the colour info or is there a way to add some tests for them?