-
Notifications
You must be signed in to change notification settings - Fork 332
Set default log level to None for MCP in Stdio mode. #3420
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
3
commits into
dev/anushakolan/set-log-level
Choose a base branch
from
dev/anushakolan/set-default-log-level-none
base: dev/anushakolan/set-log-level
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.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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 |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ | |
| using System; | ||
| using System.CommandLine; | ||
| using System.CommandLine.Parsing; | ||
| using System.IO; | ||
| using System.Runtime.InteropServices; | ||
| using System.Text; | ||
| using System.Text.RegularExpressions; | ||
|
|
@@ -63,6 +64,28 @@ public static bool StartEngine(string[] args, bool runMcpStdio, string? mcpRole) | |
| { | ||
| try | ||
| { | ||
| // Initialize log level EARLY, before building the host. | ||
| // This ensures logging filters are effective during the entire host build process. | ||
| LogLevel initialLogLevel = GetLogLevelFromCommandLineArgs(args, runMcpStdio, out bool isCliOverridden, out bool isConfigOverridden); | ||
| LogLevelProvider.SetInitialLogLevel(initialLogLevel, isCliOverridden, isConfigOverridden); | ||
|
Collaborator
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. what is the use of |
||
|
|
||
| // For MCP stdio mode, redirect Console.Out to keep stdout clean for JSON-RPC. | ||
| // MCP SDK uses Console.OpenStandardOutput() which gets the real stdout, unaffected by this redirect. | ||
| if (runMcpStdio) | ||
| { | ||
| // When LogLevel.None, redirect to null stream for ZERO output. | ||
| // Otherwise redirect to stderr so logs don't pollute JSON-RPC. | ||
| if (initialLogLevel == LogLevel.None) | ||
| { | ||
| Console.SetOut(TextWriter.Null); | ||
|
anushakolan marked this conversation as resolved.
|
||
| Console.SetError(TextWriter.Null); | ||
| } | ||
| else | ||
| { | ||
| Console.SetOut(Console.Error); | ||
|
anushakolan marked this conversation as resolved.
|
||
| } | ||
| } | ||
|
|
||
| IHost host = CreateHostBuilder(args, runMcpStdio, mcpRole).Build(); | ||
|
|
||
| if (runMcpStdio) | ||
|
|
@@ -115,13 +138,20 @@ public static IHostBuilder CreateHostBuilder(string[] args, bool runMcpStdio, st | |
| }) | ||
| .ConfigureLogging(logging => | ||
| { | ||
| logging.AddFilter("Microsoft", logLevel => LogLevelProvider.ShouldLog(logLevel)); | ||
| logging.AddFilter("Microsoft.Hosting.Lifetime", logLevel => LogLevelProvider.ShouldLog(logLevel)); | ||
| // Set minimum level at the framework level - this affects all loggers. | ||
| // For MCP stdio mode, Console.Out is redirected to stderr in Main(), | ||
| // so any logging output goes to stderr and doesn't pollute the JSON-RPC channel. | ||
| logging.SetMinimumLevel(LogLevelProvider.CurrentLogLevel); | ||
|
anushakolan marked this conversation as resolved.
|
||
|
|
||
| // Add filter for dynamic log level changes (e.g., via MCP logging/setLevel) | ||
| logging.AddFilter(logLevel => LogLevelProvider.ShouldLog(logLevel)); | ||
| }) | ||
| .ConfigureWebHostDefaults(webBuilder => | ||
| { | ||
| Startup.MinimumLogLevel = GetLogLevelFromCommandLineArgs(args, out Startup.IsLogLevelOverriddenByCli); | ||
| LogLevelProvider.SetInitialLogLevel(Startup.MinimumLogLevel, Startup.IsLogLevelOverriddenByCli); | ||
| // LogLevelProvider was already initialized in StartEngine before CreateHostBuilder. | ||
| // Use the already-set values to avoid re-parsing args. | ||
| Startup.MinimumLogLevel = LogLevelProvider.CurrentLogLevel; | ||
| Startup.IsLogLevelOverriddenByCli = LogLevelProvider.IsCliOverridden; | ||
| ILoggerFactory loggerFactory = GetLoggerFactoryForLogLevel(Startup.MinimumLogLevel, stdio: runMcpStdio); | ||
| ILogger<Startup> startupLogger = loggerFactory.CreateLogger<Startup>(); | ||
| DisableHttpsRedirectionIfNeeded(args); | ||
|
|
@@ -133,19 +163,59 @@ public static IHostBuilder CreateHostBuilder(string[] args, bool runMcpStdio, st | |
| /// Using System.CommandLine Parser to parse args and return | ||
| /// the correct log level. We save if there is a log level in args through | ||
| /// the out param. For log level out of range we throw an exception. | ||
| /// When in MCP stdio mode without explicit --LogLevel, defaults to None without CLI override. | ||
|
anushakolan marked this conversation as resolved.
|
||
| /// </summary> | ||
| /// <param name="args">array that may contain log level information.</param> | ||
| /// <param name="isLogLevelOverridenByCli">sets if log level is found in the args.</param> | ||
| /// <param name="runMcpStdio">whether running in MCP stdio mode.</param> | ||
|
anushakolan marked this conversation as resolved.
|
||
| /// <param name="isLogLevelOverridenByCli">sets if log level is found in the args from CLI.</param> | ||
| /// <param name="isLogLevelOverridenByConfig">sets if log level came from config file.</param> | ||
| /// <returns>Appropriate log level.</returns> | ||
| private static LogLevel GetLogLevelFromCommandLineArgs(string[] args, out bool isLogLevelOverridenByCli) | ||
| private static LogLevel GetLogLevelFromCommandLineArgs(string[] args, bool runMcpStdio, out bool isLogLevelOverridenByCli, out bool isLogLevelOverridenByConfig) | ||
| { | ||
| Command cmd = new(name: "start"); | ||
| Option<LogLevel> logLevelOption = new(name: "--LogLevel"); | ||
| Option<bool> logLevelFromConfigOption = new(name: "--LogLevelFromConfig"); | ||
| cmd.AddOption(logLevelOption); | ||
| cmd.AddOption(logLevelFromConfigOption); | ||
| ParseResult result = GetParseResult(cmd, args); | ||
| bool matchedToken = result.Tokens.Count - result.UnmatchedTokens.Count - result.UnparsedTokens.Count > 1; | ||
| LogLevel logLevel = matchedToken ? result.GetValueForOption(logLevelOption) : LogLevel.Error; | ||
| isLogLevelOverridenByCli = matchedToken; | ||
|
|
||
| // Check if --LogLevelFromConfig flag is present (indicates config override, not CLI) | ||
| bool isFromConfig = result.GetValueForOption(logLevelFromConfigOption); | ||
|
|
||
| LogLevel logLevel; | ||
| if (matchedToken) | ||
| { | ||
| logLevel = result.GetValueForOption(logLevelOption); | ||
|
|
||
| if (isFromConfig) | ||
| { | ||
| // Log level came from config file (passed by CLI with --LogLevelFromConfig marker) | ||
| isLogLevelOverridenByCli = false; | ||
| isLogLevelOverridenByConfig = true; | ||
| } | ||
| else | ||
| { | ||
| // User explicitly set --LogLevel via CLI (highest priority) | ||
| isLogLevelOverridenByCli = true; | ||
| isLogLevelOverridenByConfig = false; | ||
| } | ||
| } | ||
| else if (runMcpStdio) | ||
| { | ||
| // MCP stdio mode without explicit --LogLevel: default to None to keep stdout clean. | ||
| // This is NOT a CLI or config override, so MCP logging/setLevel can still change it. | ||
| logLevel = LogLevel.None; | ||
| isLogLevelOverridenByCli = false; | ||
| isLogLevelOverridenByConfig = false; | ||
| } | ||
| else | ||
| { | ||
| // Normal mode without explicit --LogLevel | ||
| logLevel = LogLevel.Error; | ||
| isLogLevelOverridenByCli = false; | ||
| isLogLevelOverridenByConfig = false; | ||
| } | ||
|
|
||
| if (logLevel is > LogLevel.None or < LogLevel.Trace) | ||
| { | ||
|
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.