Repository navigation
Add headless MCP stdio mode and centralized output policy #239
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
Mike Krüger (mkrueger)
wants to merge
19
commits into
main
Choose a base branch
from
dev/mkrueger/mcp_stdio
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.
Open
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
9e6800e
Add headless MCP stdio mode
mkrueger d85ad03
Disable shell history in MCP stdio mode
mkrueger f1b84b7
Centralize shell output through a category-based policy
mkrueger 3ee1474
Update Copilot instructions for MCP stdio and output policy
mkrueger 49523cb
Keep MCP stdio sign-in non-interactive
mkrueger 51b010e
Bound MCP stdio startup and handle request cancellation
mkrueger 26d45da
Merge remote-tracking branch 'origin/main' into dev/mkrueger/mcp_stdio
mkrueger eaa80c1
Preserve machine output contracts
mkrueger 6e5d07d
Harden stdio startup validation
mkrueger ef31a1f
Use safe process test path joins
mkrueger 4f8ff03
Disable broker authentication in stdio mode
mkrueger 14d9dfd
Document stdio LSP incompatibility
mkrueger 13d5e3e
Suppress strict theme warnings in machine mode
mkrueger 387368c
Harden stdio startup buffering
mkrueger a621198
Validate direct stdio option values
mkrueger 9132828
Merge main and preserve successful theme validation warnings
mkrueger 6dda05c
Merge main and fix stdio startup spool cleanup
mkrueger b4658ee
Require bounded watch results in machine mode
mkrueger 0abc080
Bound MCP stdio startup buffering
mkrueger 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 |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| // ------------------------------------------------------------ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // ------------------------------------------------------------ | ||
|
|
||
| using Microsoft.CodeAnalysis; | ||
| using Microsoft.CodeAnalysis.CSharp; | ||
| using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
| using Microsoft.CodeAnalysis.Diagnostics; | ||
| using System.Collections.Immutable; | ||
|
|
||
| namespace CosmosShell.Analyzer | ||
| { | ||
| /// <summary> | ||
| /// Requires user-facing console output to go through ShellOutput, which applies the | ||
| /// quiet, machine-mode, and MCP stdio presentation policies. Direct writes bypass those | ||
| /// policies and can corrupt structured stdout or the MCP protocol stream. | ||
| /// </summary> | ||
| [DiagnosticAnalyzer(LanguageNames.CSharp)] | ||
| public class DirectConsoleOutputAnalyzer : DiagnosticAnalyzer | ||
| { | ||
| public const string DiagnosticId = "CZ0003"; | ||
|
|
||
| private const string Category = "Usage"; | ||
| private const string AllowedType = "Azure.Data.Cosmos.Shell.Core.ShellOutput"; | ||
|
|
||
| private static readonly DiagnosticDescriptor Rule = new DiagnosticDescriptor( | ||
| DiagnosticId, | ||
| "Direct console output", | ||
| "'{0}' bypasses the shell output policy; write through ShellOutput instead", | ||
| Category, | ||
| DiagnosticSeverity.Error, | ||
| isEnabledByDefault: true, | ||
| description: "User-facing output must go through ShellOutput so --quiet, machine mode, and MCP stdio are honored."); | ||
|
|
||
| public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get { return ImmutableArray.Create(Rule); } } | ||
|
|
||
| public override void Initialize(AnalysisContext context) | ||
| { | ||
| context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); | ||
| context.EnableConcurrentExecution(); | ||
| context.RegisterSyntaxNodeAction(Analyze, SyntaxKind.InvocationExpression); | ||
| } | ||
|
|
||
| private static void Analyze(SyntaxNodeAnalysisContext context) | ||
| { | ||
| var invocation = (InvocationExpressionSyntax)context.Node; | ||
| if (!(context.SemanticModel.GetSymbolInfo(invocation, context.CancellationToken).Symbol is IMethodSymbol method)) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| if (!IsDirectOutput(context.SemanticModel, invocation, method, context)) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| var containingType = context.ContainingSymbol?.ContainingType; | ||
| while (containingType != null) | ||
| { | ||
| if (containingType.ToDisplayString() == AllowedType) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| containingType = containingType.ContainingType; | ||
| } | ||
|
|
||
| context.ReportDiagnostic(Diagnostic.Create(Rule, invocation.GetLocation(), method.ContainingType.Name + "." + method.Name)); | ||
| } | ||
|
|
||
| private static bool IsDirectOutput(SemanticModel model, InvocationExpressionSyntax invocation, IMethodSymbol method, SyntaxNodeAnalysisContext context) | ||
| { | ||
| var type = method.ContainingType?.ToDisplayString(); | ||
|
|
||
| // Console.Write / Console.WriteLine. | ||
| if (method.IsStatic && type == "System.Console" && method.Name.StartsWith("Write")) | ||
| { | ||
| return true; | ||
| } | ||
|
|
||
| // Every static AnsiConsole call except Create renders to the global console. | ||
| if (method.IsStatic && type == "Spectre.Console.AnsiConsole" && method.Name != "Create") | ||
| { | ||
| return true; | ||
| } | ||
|
|
||
| // Console.Out.Write*, Console.Error.Write*, and AnsiConsole.Console.<extension>. | ||
| if (invocation.Expression is MemberAccessExpressionSyntax access | ||
| && model.GetSymbolInfo(access.Expression, context.CancellationToken).Symbol is IPropertySymbol receiver | ||
| && receiver.IsStatic) | ||
| { | ||
| var owner = receiver.ContainingType?.ToDisplayString(); | ||
| if (owner == "System.Console" && (receiver.Name == "Out" || receiver.Name == "Error") && method.Name.StartsWith("Write")) | ||
| { | ||
| return true; | ||
| } | ||
|
|
||
| if (owner == "Spectre.Console.AnsiConsole" && receiver.Name == "Console") | ||
| { | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
| } | ||
| } | ||
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
Oops, something went wrong.
Oops, something went wrong.
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.