Skip to content

Redirect writing errors to stdio to stderr#2559

Merged
conniey merged 4 commits into
microsoft:mainfrom
conniey:json-rpc-issue
May 1, 2026
Merged

Redirect writing errors to stdio to stderr#2559
conniey merged 4 commits into
microsoft:mainfrom
conniey:json-rpc-issue

Conversation

@conniey
Copy link
Copy Markdown
Member

@conniey conniey commented May 1, 2026

What does this PR do?

This redirects writing exceptions to stdio and writes to err instead. Anything written to stdio is considered a response from the MCP server and is interpreted as JSON RPC. Consequently, is failed to be interpreted by Docker gateway.

In Visual Studio Code, it outputs a warning but keeps reading from stdio which succeeds later on.

2026-05-01 12:09:18.285 [warning] Failed to parse message: "Failed to detect X display. Running in headless mode. Exception type: System.Exception\n"
2026-05-01 12:23:03.878 [debug] [server -> editor] {"result":{"content":[{"type":"text","text":"{\"status\":200,\"message\":\"Success\",\"results\":{\"subscriptions\":[{\"subscriptionId\" ...

In Docker Gateway, it stops reading input from stdio:

Error: MPC 0: calling "tools/call": invalid character 'F' looking for beginning of value

GitHub issue number?

[Link to the GitHub issue this PR addresses]

Pre-merge Checklist

  • Required for All PRs
    • Read contribution guidelines
    • PR title clearly describes the change
    • Commit history is clean with descriptive messages (cleanup guide)
    • Added comprehensive tests for new/modified functionality
    • Created a changelog entry if the change falls among the following: new feature, bug fix, UI/UX update, breaking change, or updated dependencies. Follow the changelog entry guide
  • For MCP tool changes:
    • One tool per PR: This PR adds or modifies only one MCP tool for faster review cycles
    • Updated servers/Azure.Mcp.Server/README.md and/or servers/Fabric.Mcp.Server/README.md documentation
    • Validate README.md changes running the script ./eng/scripts/Process-PackageReadMe.ps1. See Package README
    • For new or modified tool descriptions, ran ToolDescriptionEvaluator and obtained a score of 0.4 or more and a top 3 ranking for all related test prompts
    • For tools with new names, including new tools or renamed tools, update consolidated-tools.json
    • For renamed tools, follow the Tool Rename Checklist and tag the PR with the breaking-change label
    • For new tools associated with Azure services or publicly available tools/APIs/products, add URL to documentation in the PR description
  • Extra steps for Azure MCP Server tool changes:
    • Updated command list in servers/Azure.Mcp.Server/docs/azmcp-commands.md
    • Ran ./eng/scripts/Update-AzCommandsMetadata.ps1 to update tool metadata in azmcp-commands.md (required for CI)
    • Updated test prompts in servers/Azure.Mcp.Server/docs/e2eTestPrompts.md
    • 👉 For Community (non-Microsoft team member) PRs:
      • Security review: Reviewed code for security vulnerabilities, malicious code, or suspicious activities before running tests (crypto mining, spam, data exfiltration, etc.)
      • Manual tests run: added comment /azp run mcp - pullrequest - live to run Live Test Pipeline

Copilot AI review requested due to automatic review settings May 1, 2026 19:26
@conniey conniey requested review from a team as code owners May 1, 2026 19:26
@github-project-automation github-project-automation Bot moved this to Untriaged in Azure MCP Server May 1, 2026
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 prevents diagnostic output from being written to standard output (stdout), which is reserved for JSON-RPC responses in the MCP server, by redirecting X11 display detection messages to standard error (stderr).

Changes:

  • Updated Linux X11 display detection logging in WindowHandleProvider to write to Console.Error instead of Console.WriteLine.
  • Simplified the failure-path console output when X11 detection throws.
  • Added an Azure.Mcp.Server changelog entry documenting the bug fix.

Reviewed changes

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

File Description
core/Microsoft.Mcp.Core/src/Services/Azure/Authentication/WindowHandleProvider.cs Redirects X11 detection output from stdout to stderr to avoid JSON-RPC parsing issues.
servers/Azure.Mcp.Server/changelog-entries/1777662676190.yaml Documents the bug fix in the server changelog entry system.

Comment thread core/Microsoft.Mcp.Core/src/Services/Azure/Authentication/WindowHandleProvider.cs Outdated
Comment thread servers/Azure.Mcp.Server/changelog-entries/1777662676190.yaml Outdated
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copy link
Copy Markdown
Contributor

@jongio jongio left a comment

Choose a reason for hiding this comment

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

Clean fix - redirecting X11 display detection diagnostics from stdout to stderr prevents JSON-RPC parsing failures on the stdio transport.

Verified no other Console.WriteLine calls in the server's runtime path need this treatment - the remaining ones in CommandFactory.cs are intentional JSON-RPC output, and CustomHelpAction.cs only runs for --help.

Removing Console.ForegroundColor/Console.ResetColor is also a good call - these set global console state that could leak color codes into stdout on concurrent writes.

@conniey conniey merged commit 129cad2 into microsoft:main May 1, 2026
15 checks passed
@github-project-automation github-project-automation Bot moved this from Untriaged to Done in Azure MCP Server May 1, 2026
@conniey conniey deleted the json-rpc-issue branch May 1, 2026 20:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

4 participants