Skip to content

fix: log startup messages to stderr instead of stdout#4

Open
nazmulidris wants to merge 1 commit into
dexwritescode:mainfrom
nazmulidris:fix-stdio-logging
Open

fix: log startup messages to stderr instead of stdout#4
nazmulidris wants to merge 1 commit into
dexwritescode:mainfrom
nazmulidris:fix-stdio-logging

Conversation

@nazmulidris

Copy link
Copy Markdown

Problem

When configuring the rust-refactor MCP server with an MCP client (in my case Antigravity CLI agy client), the client fails to initialize and throws the following parser error:

error: Error: connection closed: initialize notification: calling "initialize": invalid character 'S' looking for beginning of value

Here's a screenshot from agy:
image

Cause

This occurs because the server prints non-JSON startup log messages directly to stdout:

Starting Rust MCP Server
Server running on stdio transport...
  1. Since the stdio transport protocol uses stdout exclusively for JSON-RPC frame delivery, printing raw text to stdout corrupts the transport stream.
  2. The client-side JSON parser attempts to parse Starting Rust MCP Server as a JSON object, failing on the very first character ('S').

Expected Behavior

The server should initialize and communicate with the client successfully without polluting stdout. All startup info, telemetry, and debugging logs must be directed to stderr (using eprintln!), leaving stdout dedicated exclusively to raw JSON-RPC frames.

Repro steps

  1. Configure an MCP host (such as agy in .agents/mcp_config.json) to execute the rustmcp server binary via the standard stdio transport. For example:
{
  "mcpServers": {
    "rust-refactor": {
      "command": "/home/nazmul/.cargo/bin/rustmcp",
      "args": []
    }
}
  1. Launch the host application (agy).
  2. The host will attempt to send an initialize request, but will immediately encounter a JSON parsing error (invalid character 'S' looking for beginning of value) upon reading the server's stdout, causing the connection to abort.

Fix

We changed the startup logs in src/main.rs to write to stderr instead of stdout by replacing println! with eprintln!:

-    println!("Starting Rust MCP Server");
-    println!("Server running on stdio transport...");
+    eprintln!("Starting Rust MCP Server");
+    eprintln!("Server running on stdio transport...");

By redirecting these messages to stderr, the JSON-RPC communication channel on stdout remains completely clean, preventing stream corruption and allowing standard stdio-based MCP clients to successfully complete the handshake.

nazmulidris added a commit to r3bl-org/r3bl-open-core that referenced this pull request Jun 2, 2026
Will remove this vendored crate if/when this PR is merged by the
author here:
dexwritescode/rust-mcp#4
nazmulidris added a commit to r3bl-org/r3bl-open-core that referenced this pull request Jun 3, 2026
Will remove this vendored crate if/when this PR is merged by the
author here:
dexwritescode/rust-mcp#4
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.

1 participant