(Note · · 4 min read)

One stray print statement breaks your MCP server

An MCP server that worked a minute ago now fails to connect, and the client says almost nothing useful. Before suspecting the transport or the client, check whether anything in the process wrote to standard output — because over stdio, standard output is not where logs go. It is the protocol.

By Md. Habibur Rahman Shohel, full-stack & AI product engineer in Dhaka

Why this fails so confusingly

With the stdio transport, which is the default for local agent clients, the server and client exchange protocol messages over the process's standard input and output. Anything else written to stdout lands in the middle of that conversation.

The client is reading structured messages, receives something that is not one, and the session breaks. What it reports is a parse or connection failure, which points nowhere near the debug line that caused it — hence an hour lost to the wrong suspect.

Write logs to stderr

Standard error is not part of the protocol stream, so it is the correct destination for everything diagnostic. In most clients it is captured into a log file you can read, which is all you wanted from the print statement in the first place.

The substitution
# corrupts the protocol stream
print(f"fetched {len(rows)} rows")

# safe, and usually captured by the client
import sys
print(f"fetched {len(rows)} rows", file=sys.stderr)

Configure your logging library the same way: a handler pointed at stderr, never the default that may land on stdout.

It is often not your code

The print statement that breaks a server frequently belongs to a dependency — a library announcing a deprecation, a framework printing a banner on start, a progress bar. Anything that writes to stdout anywhere in the process will do it.

If the stream is being corrupted and your own code is clean, silence the library, or redirect stdout to stderr at process start so nothing can reach it accidentally. That redirect is a reasonable default for a stdio server regardless.

The related startup failures

Two others look similar and are worth checking at the same time. Some clients do not pass the parent shell's environment to the server process, so a server that works in your terminal and nowhere else is usually missing a variable it never had — declare them in the client configuration rather than assuming they are inherited.

And blocking work freezes everything: a synchronous call inside one tool stops the server answering anything else until it finishes, which the client eventually reports as a timeout on a tool you were not even calling.

Common questions

Why does my MCP server disconnect as soon as it starts?

Most often because something wrote to standard output. With the stdio transport, stdout carries the protocol itself, so any other text corrupts the message stream and the client fails with a parse or connection error that does not mention the real cause.

Where should an MCP server write its logs?

To standard error, which is not part of the protocol stream and is usually captured into a log file by the client. Point your logging library's handler at stderr explicitly rather than relying on its default.

What if a dependency prints to stdout?

That breaks the server exactly the same way. Silence the library, or redirect stdout to stderr at process start so nothing can reach the protocol stream accidentally — a sensible default for any stdio server.

Why does my MCP server work in the terminal but not in the client?

Usually missing environment variables. Some clients do not pass the parent shell's environment to the server process, so declare the variables the server needs in the client configuration rather than assuming they are inherited.

Need something like this built?