MCP · Lesson 66 of 120
Logging Safely
Never print to stdout.
A common bug in standard IO servers is a stray print. Standard output is reserved for MCP messages. A debug print there can break the client, or get silently dropped.

- A stray print
- stdout is reserved
- Breaks clients
Wrong and right
Print writes to standard output, mixing your text with protocol messages. Print with file equals standard error, or the logging module, goes to standard error, where it's safe.
Wrong
- print('debug')Goes to stdoutMixes with messages
Right
- print(..., file=sys.stderr)logging to stderrProtocol stays clean
Safe logging in a server
We set up logging to standard error. The tool logs each lookup there, instead of printing. With HTTP servers this matters less, but the habit keeps every server safe.
safe_logging.pypython
import logging, sys
from mcp.server.mcpserver import MCPServer
logging.basicConfig(stream=sys.stderr, level=logging.INFO)
log = logging.getLogger("northwind")
mcp = MCPServer("northwind")
@mcp.tool()
def order_status(order_id: int) -> str:
"""Look up an order."""
log.info("looking up order %s", order_id) # stderr, safe
return "shipped"
if __name__ == "__main__":
mcp.run()Summary
Never print to standard output in a standard IO server. Log to standard error instead. Next, streamable HTTP.
Key takeaways- No print()
- stderr
- logging
Next up: Streamable HTTP — servers on the web